librelist archives

« back to archive

initing a repository, adding files to the index and committing

initing a repository, adding files to the index and committing

From:
Andrew
Date:
2011-02-19 @ 09:34
Hi,

Im attempting to use the libgit2 library with Nokia QT, Ive got everyting 
compiled and working fine, and I can open ot init a repository but I 
having trouble understanding how the individual calls in the api should 
work together.

Ive looked through the usage guide, the API and the tests, but Im still 
not much clearer, can anyone give me a very quick overview of which calls 
need to be used to add files to the index and then commit them?


Also, Im hoping to use this with some pretty large files, are there any 
tricks that will increase the speed of the processing time for files that 
are around 1-2GB in size?


Thanks for any help.


- Andrew Bush

Re: [libgit2] initing a repository, adding files to the index and committing

From:
Vicent Marti
Date:
2011-02-21 @ 10:05
Oh, and regarding the pretty large files: libgit2 is pretty tuned for
speed, and I've just rolled out a new IO interface that uses
double-buffering with a worker thread, so there are no specific tricks
to make it faster, because it should just work (TM) pretty fast for
all file sizes.

Obviously, if you find any API calls to be unreasonably slow, open a
bug report and I'll look into that.

Cheers,
Vicent Marti



On Sat, Feb 19, 2011 at 11:34 AM, Andrew <andrew@bushsoftware.com> wrote:
> Hi,
>
> Im attempting to use the libgit2 library with Nokia QT, Ive got 
everyting compiled and working fine, and I can open ot init a repository 
but I having trouble understanding how the individual calls in the api 
should work together.
>
> Ive looked through the usage guide, the API and the tests, but Im still 
not much clearer, can anyone give me a very quick overview of which calls 
need to be used to add files to the index and then commit them?
>
>
> Also, Im hoping to use this with some pretty large files, are there any 
tricks that will increase the speed of the processing time for files that 
are around 1-2GB in size?
>
>
> Thanks for any help.
>
>
> - Andrew Bush
>

Re: [libgit2] initing a repository, adding files to the index and committing

From:
Vicent Marti
Date:
2011-02-21 @ 10:02
Hey Andrew,

the calls for creating a commit are exactly the same as the plumbing
calls that `git commit` porcelain does, since right now we only
implement plumbing commands.

1. Open the repository and the index. You cannot open the index alone,
since you need a backing repository to write the new blob, tree and
commit.
    git_repository_open()
    git_repository_index()

2. Refresh the index from disk to load the entries that may already be staged
    git_index_read()

3. Stage all the files you need on the index. This will add the files
to the index, and create a new blob on the Object Database
representing each file.
    git_index_add()

4. Write the changes on the index to disk. This fixes the files as staged.
    git_index_write()

5. Now, to create the commit, we need to convert the index into a tree
on the ODB.
    git_index_write_tree() // Unfortunately, this API call is not
ready yet :( -- it's being worked on, though

6. Next, you have to create a new commit in memory and fill it with
all the relevant information
    git_commit_new()
    git_commit_set_tree(tree) // the tree we've just written
    git_commit_set_author()
    git_commit_set_ // fill all the required fields

7. Lastly, just write the commit to the ODB.
    git_object_write(commit);

Depending on what you are doing, you may want to update the symbolic
reference to the HEAD to point to the new commit, using the brand new
references API.

To sum it up: follow the same plumbing process that `git commit` does.
However, we still cannot turn an index into a tree object, so creating
commits from scratch is not possible right now. I know this is an
important thing, but we've got many things going now -- hopefully in
~2 weeks we'll have that functionality, maybe earlier.

I hope this was helpful. Even if you cannot create commits from
scratch, now you know exactly how does the commit creation process
work.

Cheers,
Vicent

On Sat, Feb 19, 2011 at 11:34 AM, Andrew <andrew@bushsoftware.com> wrote:
> Hi,
>
> Im attempting to use the libgit2 library with Nokia QT, Ive got 
everyting compiled and working fine, and I can open ot init a repository 
but I having trouble understanding how the individual calls in the api 
should work together.
>
> Ive looked through the usage guide, the API and the tests, but Im still 
not much clearer, can anyone give me a very quick overview of which calls 
need to be used to add files to the index and then commit them?
>
>
> Also, Im hoping to use this with some pretty large files, are there any 
tricks that will increase the speed of the processing time for files that 
are around 1-2GB in size?
>
>
> Thanks for any help.
>
>
> - Andrew Bush
>


Cheers,
Vicent Marti

Re: [libgit2] initing a repository, adding files to the index and committing

From:
Scott Chacon
Date:
2011-02-21 @ 17:19
Hey,

On Mon, Feb 21, 2011 at 2:02 AM, Vicent Marti <tanoku@gmail.com> wrote:
> 2. Refresh the index from disk to load the entries that may already be staged
>    git_index_read()
>
> 3. Stage all the files you need on the index. This will add the files
> to the index, and create a new blob on the Object Database
> representing each file.
>    git_index_add()
>
> 4. Write the changes on the index to disk. This fixes the files as staged.
>    git_index_write()
>
> 5. Now, to create the commit, we need to convert the index into a tree
> on the ODB.
>    git_index_write_tree() // Unfortunately, this API call is not
> ready yet :( -- it's being worked on, though
>

Technically, you don't have to use the index at all.  If you want to
write commit snapshots of files in your working directory, you can
simply emulate this functionality in memory until the
git_index_write_tree() call is completed.  Create blob and tree
objects by reading in files into blob objects and adding them as tree
entries using the tree api.  Then the top level tree SHA can be used
as the tree entry for a new commit object using the commit api that
Vicent explained.  The index would be an easy way of doing that, but
you can also do it just using the blob, tree and commit apis.

Scott

Re: [libgit2] initing a repository, adding files to the index and committing

From:
Andrew
Date:
2011-02-21 @ 10:42
Hi Vicent,

Thanks for your reply, it has helped a lot.

One thing I am still unclear on,

I have created a new repository:

git_repository_init()

and now I want to get an index for it.

calling 

> git_repository_index()

returns -15 (EBARE)


so where do I go from here?

git_index_open_bare() seems like a possibility, but I am unclear on with 
the path I pass in should be, and how I then associate that index with the
ew repository.

could you give me the process for creating a brand new repository & index 
and moving forward from there?

Thanks again for your help, and the work you are putting into this.


- Andrew Bush



On 21/02/2011, at 11:02 PM, Vicent Marti wrote:

> Hey Andrew,
> 
> the calls for creating a commit are exactly the same as the plumbing
> calls that `git commit` porcelain does, since right now we only
> implement plumbing commands.
> 
> 1. Open the repository and the index. You cannot open the index alone,
> since you need a backing repository to write the new blob, tree and
> commit.
>    git_repository_open()
>    git_repository_index()
> 
> 2. Refresh the index from disk to load the entries that may already be staged
>    git_index_read()
> 
> 3. Stage all the files you need on the index. This will add the files
> to the index, and create a new blob on the Object Database
> representing each file.
>    git_index_add()
> 
> 4. Write the changes on the index to disk. This fixes the files as staged.
>    git_index_write()
> 
> 5. Now, to create the commit, we need to convert the index into a tree
> on the ODB.
>    git_index_write_tree() // Unfortunately, this API call is not
> ready yet :( -- it's being worked on, though
> 
> 6. Next, you have to create a new commit in memory and fill it with
> all the relevant information
>    git_commit_new()
>    git_commit_set_tree(tree) // the tree we've just written
>    git_commit_set_author()
>    git_commit_set_ // fill all the required fields
> 
> 7. Lastly, just write the commit to the ODB.
>    git_object_write(commit);
> 
> Depending on what you are doing, you may want to update the symbolic
> reference to the HEAD to point to the new commit, using the brand new
> references API.
> 
> To sum it up: follow the same plumbing process that `git commit` does.
> However, we still cannot turn an index into a tree object, so creating
> commits from scratch is not possible right now. I know this is an
> important thing, but we've got many things going now -- hopefully in
> ~2 weeks we'll have that functionality, maybe earlier.
> 
> I hope this was helpful. Even if you cannot create commits from
> scratch, now you know exactly how does the commit creation process
> work.
> 
> Cheers,
> Vicent
> 
> On Sat, Feb 19, 2011 at 11:34 AM, Andrew <andrew@bushsoftware.com> wrote:
>> Hi,
>> 
>> Im attempting to use the libgit2 library with Nokia QT, Ive got 
everyting compiled and working fine, and I can open ot init a repository 
but I having trouble understanding how the individual calls in the api 
should work together.
>> 
>> Ive looked through the usage guide, the API and the tests, but Im still
not much clearer, can anyone give me a very quick overview of which calls 
need to be used to add files to the index and then commit them?
>> 
>> 
>> Also, Im hoping to use this with some pretty large files, are there any
tricks that will increase the speed of the processing time for files that 
are around 1-2GB in size?
>> 
>> 
>> Thanks for any help.
>> 
>> 
>> - Andrew Bush
>> 
> 
> 
> Cheers,
> Vicent Marti
>