如何使用 libgit2 库创建分支,API 引用了提交目标,但我应该为该参数使用什么?
这是我目前得到的代码,基于测试中的示例代码,但它们使用了许多硬编码引用,因此很难发现您应该在真实场景中使用什么,比如我应该作为“目标”传递的' 在这种情况下。
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 );
最佳答案
what i should pass as 'target' in this instance.
您希望新分支指向的已解决提交。
如 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.
注意:令人惊讶的是, header 与代码不同步。 header 让读者认为传递 git_object
就可以了。但是,只接受 git_commit
。
更新
And how would i get 'the resolved commit';
- 如果您知道提交的 oid,
git_commit_lookup()
将是可行的方法。 - 如果您已经持有
git_object
而恰好是一个提交(git_object_type()
返回GIT_OBJ_COMMIT
),您可以直接转换它并将(git_commit *)my_object
传递给函数。
关于branch - 如何使用 libgit2 创建分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15750160/