我正在尝试解决这个问题。https:/github.comgitaheadgitaheadissues380
问题是,模型中使用的树不包含任何未跟踪的文件,因此视图没有任何东西可以显示。当我对文件进行分期时,它就会显示出来。
有没有办法在树中也跟踪未跟踪的文件?
我创建了一个小的测试程序来发现这个问题。当一个文件被暂存时,计数不等于零,否则它总是零。
测试设置
d
#include <git2.h>
#include <stdio.h>
int main() {
git_libgit2_init();
git_repository *repo = NULL;
int error = git_repository_open(&repo, "/TestRepository");
if (error < 0) {
const git_error *e = git_error_last();
printf("Error %d/%d: %s\n", error, e->klass, e->message);
exit(error);
}
git_tree *tree = nullptr;
git_index* idx = nullptr;
git_repository_index(&idx, repo);
git_oid id;
if (git_index_write_tree(&id, idx)) {
const git_error *e = git_error_last();
printf("Error %d/%d: %s\n", error, e->klass, e->message);
exit(error);
}
git_tree_lookup(&tree, repo, &id);
int count = git_tree_entrycount(tree);
printf("%d", count);
git_repository_free(repo);
printf("SUCCESS");
return 0;
}
如果我没理解错的话,你所看到的是正常的:由于文件是未跟踪的new,索引对它一无所知,所以如果你问索引,它没有 "分期 "的变化可以比较,因此没有差异。
如果你想要一个尚未被跟踪的文件的diff,你必须以另一种方式提供它,通常是通过询问 git_diff
来完成比较工作树版本和 /dev/null
、空的blob等。
既然你想要libgit2的解决方案,我在GitX中的做法是通过 git_status_list_new
API,它提供了一种独立于文件系统的方式来生成可查看的差异(staged & unstaged)。git_patch_from_blobs
git_patch_from_blobs_and_buffer
. 现在回想起来,也许应该把它放在图书馆里,作为... git_status_entry_generate_patch
还是什么...