librelist archives

« back to archive

Newbie commit help required

Newbie commit help required

From:
Dan Petitt
Date:
2013-03-20 @ 22:32
Hi

I am trying to write a GIT management function for my Web Site Editor and 
am having some confusion/issues with commiting a stage.

I have got libgit to stage my modifications, but when I come to use 
git_commit_create nothing gets committed but the return code is 0.

If i specify "HEAD" as the 'update ref' then it works but if I look at the
history using GitGui then my history tree has disappeared.

This is the process I am doing:

git_repository_open - opening my repo
git_repository_index - get the repo index
git_index_read - read the index
git_index_add_bypath - add my changed file to the index
git_index_write - write my updated index to disk
git_index_write_tree - write index of changes to tree to get my tree OID
git_tree_lookup - lookup my tree object from the OID
error = git_commit_create_v(
						&commit_id, /* out id */
						open_repo,
						"", /* do not update the HEAD */
						author,
						comitter,
						NULL, /* use default message encoding */
						"Test commit message",
						tree,
						0 );

Is this correct, wrong or whatever and what is the 'update_ref' value 
supposed to be. I have trying searching google as much as I can but can 
find no help other than example 'test' code using hard coded values which 
dont really help me trying to understand what I should do in a real-world 
situation.

Thanks in advance,
Dan

Re: [libgit2] Newbie commit help required

From:
Sascha Cunz
Date:
2013-03-20 @ 22:54
Hi Dan,
Am Mittwoch, 20. März 2013, 22:32:40 schrieb Dan Petitt:
> Hi
> 
> I am trying to write a GIT management function for my Web Site Editor and am
> having some confusion/issues with commiting a stage.
> 
> I have got libgit to stage my modifications, but when I come to use
> git_commit_create nothing gets committed but the return code is 0.
> 
> If i specify "HEAD" as the 'update ref' then it works but if I look at the
> history using GitGui then my history tree has disappeared.
> 
> This is the process I am doing:
> 
> git_repository_open - opening my repo
> git_repository_index - get the repo index
> git_index_read - read the index
> git_index_add_bypath - add my changed file to the index
> git_index_write - write my updated index to disk
> git_index_write_tree - write index of changes to tree to get my tree OID
> git_tree_lookup - lookup my tree object from the OID
> error = git_commit_create_v(
> 						&commit_id, /* out id */
> 						open_repo,
> 						"", /* do not update the HEAD */
> 						author,
> 						comitter,
> 						NULL, /* use default message encoding */
> 						"Test commit message",
> 						tree,
> 						0 );
> 
> Is this correct, wrong or whatever and what is the 'update_ref' value
> supposed to be. I have trying searching google as much as I can but can
> find no help other than example 'test' code using hard coded values which
> dont really help me trying to understand what I should do in a real-world
> situation.

I don't see a real problem with that flow. Just a few notes:

- When you're using git_repository_index, you probably won't need to call
  git_index_read explicitly, since the index has already been read for you.

- You don't link your new commit to a parent, which is why updating the HEAD
  ref with this commit will truncate your history.

To create such a link, you have to:

1. Lookup the commit that HEAD points to before you create your new commit.
   i.e. something along these lines:

    git_commit* old_head = NULL;
    error = git_revparse_single( (git_object**) &old_head,
                                 open_repo, "HEAD" );

2. Give it as a parent to your new commit:

    error = git_commit_create_v(
  						&commit_id, /* out id */
  						open_repo,
  						"", /* do not update the HEAD */
  						author,
  						comitter,
  						NULL, /* use default message encoding */
  						"Test commit message",
  						tree,
  						1, old_head ); /* <-- HERE */

Since HEAD is usually a symbolic reference to a branch, you probably don't 
want to update HEAD directly, but the reference where it points to, i.e. the 
master branch. I don't know exactly off of my head, but this has to be 
specified either as "refs/heads/master" (probably) or just "master" (unlikely) 
to git_commit_create_v.

Hope this helps,
Sascha

Re: [libgit2] Newbie commit help required

From:
Dan Petitt
Date:
2013-03-21 @ 20:38
> - When you're using git_repository_index, you probably won't need to call
> git_index_read explicitly, since the index has already been read for you.

Thanks


> - You don't link your new commit to a parent, which is why updating the HEAD
> ref with this commit will truncate your history.

Thank you so much Sascha, that worked perfectly when I committed using a 
ref of "HEAD" instead of leaving it as an empty string (which failed with 
code -13).

Cheers
Dan

Re: [libgit2] Newbie commit help required

From:
Xavier L.
Date:
2013-03-21 @ 00:01
On Mar 20, 2013 6:54 PM, "Sascha Cunz" <sascha-ml@babbelbox.org> wrote:
>
> Hi Dan,
> Am Mittwoch, 20. März 2013, 22:32:40 schrieb Dan Petitt:
> > Hi
> >
> > I am trying to write a GIT management function for my Web Site Editor
and am
> > having some confusion/issues with commiting a stage.
> >
> > I have got libgit to stage my modifications, but when I come to use
> > git_commit_create nothing gets committed but the return code is 0.
> >
> > If i specify "HEAD" as the 'update ref' then it works but if I look at
the
> > history using GitGui then my history tree has disappeared.
> >
> > This is the process I am doing:
> >
> > git_repository_open - opening my repo
> > git_repository_index - get the repo index
> > git_index_read - read the index
> > git_index_add_bypath - add my changed file to the index
> > git_index_write - write my updated index to disk
> > git_index_write_tree - write index of changes to tree to get my tree OID
> > git_tree_lookup - lookup my tree object from the OID
> > error = git_commit_create_v(
> >                                               &commit_id, /* out id */
> >                                               open_repo,
> >                                               "", /* do not update the
HEAD */
> >                                               author,
> >                                               comitter,
> >                                               NULL, /* use default
message encoding */
> >                                               "Test commit message",
> >                                               tree,
> >                                               0 );
> >
> > Is this correct, wrong or whatever and what is the 'update_ref' value
> > supposed to be. I have trying searching google as much as I can but can
> > find no help other than example 'test' code using hard coded values
which
> > dont really help me trying to understand what I should do in a
real-world
> > situation.
>
> I don't see a real problem with that flow. Just a few notes:
>
> - When you're using git_repository_index, you probably won't need to call
>   git_index_read explicitly, since the index has already been read for
you.
>
> - You don't link your new commit to a parent, which is why updating the
HEAD
>   ref with this commit will truncate your history.
>
> To create such a link, you have to:
>
> 1. Lookup the commit that HEAD points to before you create your new
commit.
>    i.e. something along these lines:
>
>     git_commit* old_head = NULL;
>     error = git_revparse_single( (git_object**) &old_head,
>                                  open_repo, "HEAD" );
>
> 2. Give it as a parent to your new commit:
>
>     error = git_commit_create_v(
>                                                 &commit_id, /* out id */
>                                                 open_repo,
>                                                 "", /* do not update the
HEAD */
>                                                 author,
>                                                 comitter,
>                                                 NULL, /* use default
message encoding */
>                                                 "Test commit message",
>                                                 tree,
>                                                 1, old_head ); /* <--
HERE */
>
> Since HEAD is usually a symbolic reference to a branch, you probably don't
> want to update HEAD directly, but the reference where it points to, i.e.
the
> master branch. I don't know exactly off of my head, but this has to be
> specified either as "refs/heads/master" (probably) or just "master"
(unlikely)
> to git_commit_create_v.
>
> Hope this helps,
> Sascha
>
If I remember correctly, there is a function that will resolve symbolic
references, such as HEAD, to their real value. It might not be available in
v0.17 though.

X