我正在尝试使用 libgit2 获取存储库的 blob:
#include <git2.h>
#include <stdio.h>
int main() {
git_libgit2_init();
git_repository *repo = NULL;
int error = git_repository_open(&repo, "/home/martin/Dokumente/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_diff *diff = NULL;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
opts.flags |= GIT_DIFF_IGNORE_WHITESPACE;
opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
error = git_diff_index_to_workdir(&diff, repo, NULL, &opts);
if (error < 0) {
const git_error *e = git_error_last();
printf("Error %d/%d: %s\n", error, e->klass, e->message);
exit(error);
}
git_patch* patch = nullptr;
git_patch_from_diff(&patch, diff, 0);
bool oldFile = false;
const git_diff_delta *dd = git_patch_get_delta(patch);
const git_oid &id = (!oldFile) ? dd->new_file.id : dd->old_file.id;
git_object *obj = nullptr;
git_object_lookup(&obj, repo, &id, GIT_OBJECT_ANY);
git_blob* blob = reinterpret_cast<git_blob *>(obj);
const char* pointer = (const char*)git_blob_rawcontent(blob);
// cleanup
git_object_free(obj);
git_patch_free(patch);
git_diff_free(diff);
git_repository_free(repo);
return 0;
}
存储库
创建一个新的存储库
提交如下文件:
1 2 3 4
再次删除 4,但不要提交
让程序运行
预期:程序运行良好。
观察到:执行 git_object_lookup() 后 obj 仍然是 nullptr
将变量 oldFile 设置为 true 时,程序运行良好,并且指针“pointer”包含原始 blob。
有人知道为什么我没有从 git_object_lookup() 返回有效对象吗?