Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a branch with libgit2

Tags:

branch

libgit2

How do I create a branch using the libgit2 library, the API has reference to a commit target, but what should I use for this parameter?

This is the code I have got so far, based on example code in tests, but they use many hard coded references so its difficult to discover what you should use in a real-world scenario like what i should pass as 'target' in this instance.

git_reference *branch = NULL, *head = NULL;

/* Create the branch */
git_branch_create( &branch, open_repo, "MyNewBranch", target, 0 );

/* Make HEAD point to this branch */
git_reference_symbolic_create( &head, open_repo, "HEAD", git_reference_name( branch ), 1 );
git_reference_free( head );
git_reference_free( branch );
like image 827
Dan Avatar asked May 01 '26 08:05

Dan


1 Answers

what i should pass as 'target' in this instance.

The resolved commit that you'd like your new branch to point to.

As stated in the header

@param target Object to which this branch should point. This object must belong to the given repo and can either be a git_commit or a git_tag. When a git_tag is being passed, it should be dereferencable to a git_commit which oid will be used as the target of the branch.

Note: Surprisingly, the header is not in sync with the code. The header makes the reader think that passing a git_object would be fine. However, only a git_commit is accepted.

Update

And how would i get 'the resolved commit';

  • Provided you know the oid of the commit, git_commit_lookup() would be the way to go.
  • If you're already having a hold on a git_object which happens to be a commit (git_object_type() returns GIT_OBJ_COMMIT), you could just cast it and pass (git_commit *)my_object to the function.
like image 137
nulltoken Avatar answered May 03 '26 22:05

nulltoken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!