我试图用旧版本libgit2(无checkout.h)实施结账一样的功能。如何在使用libgit2签出分支时更新工作树?
首先,我对分支A,它看起来像:
Branch:
A A0 --- A1
/
Master M
,每次提交创建一个文件具有相同的名称,例如,提交标记A1创建一个文件A1。如果我在这一点上看gitk,一切看起来都是我想要的。
现在我创建一个新的分支,B
,我想添加一个承诺是:
Branch:
A A0 --- A1
/
Master M
\
B B0
然而,当我用我的代码为“结帐” B,它使A0和A1未跟踪,而不是删除它们,因为我所期待的:
(B)$ git status
# On branch B
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# A0
# A1
nothing added to commit but untracked files present (use "git add" to track)
所以,我觉得有什么东西跟我结账的代码,这是本丢失:
void Checkout(const char *branch_name, git_commit *branch_tip) {
// Update index & tree
git_tree *tree;
git_commit_tree(&tree, branch_tip);
git_index_read_tree(index_, tree);
git_index_write(index_);
git_tree_free(tree);
// Reset head
string branch_ref = string("refs/heads/") + branch_name;
git_reference *head;
git_reference_lookup(&head, repo_, kGitHeadFile);
git_reference_set_target(head, branch_ref.c_str());
git_reference_free(head);
}
(请注意,我实际上是在每一行中检查实际代码中的返回代码,并且所有代码都返回0,只是不想在这里混淆)。
据我所知,此代码与git的文档描述git checkout <branch>
:
To prepare for working on <branch>, switch to it by updating
the index and the files in the working tree, and by pointing
HEAD at the branch.
有一些...“更新工作树”命令我需要运行?
我忘了提及该代码也不会将CRLF过滤应用于blob数据,但如果您不在最近的libgit2上,那么您可能不太会用正确的方式处理它。希望你在一个不需要它的平台上。 – arrbee