4

这个问题是我之前的问题的演变或解决方案:Clone a git repo (indepth)我认为在这种情况下创建一个新问题是最好的做法,但我可能错了。

这很简单:我将如何做git checkout masterlibgit2相当的事情

一年前似乎不可能:https ://github.com/libgit2/libgit2/issues/247 据此,至少 5 个月前可以进行克隆。但我从未见过任何关于如何做到这一点的代码文档示例。(编辑)我的意思是我没有看到任何关于包含完整克隆的信息git checkout,也没有看到任何关于结帐的代码/文档。

4

2 回答 2

5

According to this a clone was possible at least 5 months ago. But I have never seen any code, documentation or examples about how to do it.

The clone operation is basically made of four steps:

  • Initialize a new repository
  • Add a remote with a fetch refspec
  • Fetch the packfile from the remote and update your local references
  • Update the content of the workdir from the commit tree of the HEAD

Current version of libgit2 (v0.17.0) allows one to perform the three first steps.

The source code contains some examples. There's a "fetch.c" one as well.

how would I do something equivalent of git checkout master with libgit2

Checkout is not implemented yet. However, the following should help you go forward.

  • git_reference_name_to_oid() to retrieve the oid of the master branch
  • git_commit_lookup() to retreive a commit from an oid
  • git_commit_tree() to retrieve the tree of a commit
  • git_iterator_for_tree() to recursively browse all the leafs of the tree (and its subtrees)

Update

The clone feature has just been merged into the libgit2 repository.

As part of the pull request, the author took care of providing the users with a checkout implementation as well.

于 2012-05-30T19:02:45.567 回答
3

您可以创建一个 HEAD 符号,然后结帐到头部,例如

git_reference_create_symbolic(&head, repo, GIT_HEAD_FILE, branchname, 1);
git_checkout_head(repo, opts, stat);
于 2012-11-26T15:04:41.873 回答