我试过了
string path = "path/to/my/repo";
git_libgit2_init();
const char * REPO_PATH = path.c_str();
git_repository * repo = nullptr;
git_repository_open(&repo, REPO_PATH);
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff;
diffopts.flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
git_diff_index_to_workdir(&diff, repo, NULL, &diffopts);
git_diff_format_t format = GIT_DIFF_FORMAT_NAME_ONLY;
if (0!=git_diff_print(diff, format, NULL,NULL)) cerr << "git_diff_print() failed" << endl;
git_diff_free(diff);
git_repository_free(repo);
git_libgit2_shutdown();
但我不知道将什么作为函数git_diff_print()的第3和第4个参数发送,有些想法吗?
在libgit2 API中,有此函数的声明
git_diff_print(git_diff *diff, git_diff_format_t format, git_diff_line_cb print_cb, void *payload);
但我不知道最后两个参数是什么以及如何将它们发送到此函数
当我尝试这个例子时:
https://libgit2.github.com/libgit2/ex/HEAD/diff.html#git_diff_print-9
,对我不起作用
最佳答案
最后,我以另一种方式获得了信息,这是我的解决方案:
git_libgit2_init();
const char * REPO_PATH = path.c_str();
git_repository * repo = nullptr;
git_repository_open(&repo, REPO_PATH);
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff *diff;
diffopts.flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
git_diff_index_to_workdir(&diff, repo, NULL, &diffopts);
size_t num_deltas = git_diff_num_deltas(diff);
if (num_deltas != 0){
const git_diff_delta *delta = git_diff_get_delta(diff, 0);
int i = 0;
cerr << "Your local changes to the following files would be overwritten by checkout : " << endl;
while (i<num_deltas) {
delta = git_diff_get_delta(diff, i);
git_diff_file file = delta->new_file;
cerr << "\t" << file.path << endl;
i++;
}
cerr << "Please commit your changes before you switch branches. " << endl;
}
else cout << "All files OK, can checkout now" << endl;
git_diff_free(diff);
git_repository_free(repo);
git_libgit2_shutdown();
关于c++ - 如何在libgit2中打印差异文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43752123/