branch - 如何使用 libgit2 创建分支

标签 branch libgit2

如何使用 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/

相关文章:

tfs - 在两个现有分支之间插入一个新分支

git - 如何强制 git 始终使用 master 分支?

带有 SUBREPOS 的 Mercurial 分支存储库

python - pygit2 Blob 历史

libgit2 - 如何使用libgit2从git获取?

branch - TFS 历史记录丢失

branch - Perforce:如何在多个分支机构之间进行集成?

git - LibGit2 实现 'git pull'

ruby - 如何在内存中创建一个git仓库?

git - Git 和 libgit2 的区别