Skip to content

Instantly share code, notes, and snippets.

@staktrace
Created February 28, 2021 20:17
Show Gist options
  • Save staktrace/c592da01ca8d49830c3ef4311dd39f4d to your computer and use it in GitHub Desktop.
Save staktrace/c592da01ca8d49830c3ef4311dd39f4d to your computer and use it in GitHub Desktop.
libgit2-bug-find-similar
#include "stdio.h"
#include "git2.h"
int check_err(int code, const char* description) {
fprintf(stderr, "%s failure %d\n", description, code);
const git_error* err = git_error_last();
fprintf(stderr, "libgit2 error was `%s` (klass=%d)", err->message, err->klass);
return code;
}
int main(int argc, char** argv) {
git_libgit2_init();
git_repository* repo;
int err = git_repository_open(&repo, "./minimal-git-repo");
if (err != 0) return check_err(err, "git_repository_open");
git_oid head;
err = git_oid_fromstr(&head, "9ec10ec05ab48f65baf3bfb4a5fc61fd503a17f4");
if (err != 0) return check_err(err, "git_oid_fromstr head");
git_oid base;
err = git_oid_fromstr(&base, "fb2f4327bee0557f6ebe21e9f97e2f8d03a36168");
if (err != 0) return check_err(err, "git_oid_fromstr base");
git_commit* head_commit;
err = git_commit_lookup(&head_commit, repo, &head);
if (err != 0) return check_err(err, "git_commit_lookup head");
git_commit* base_commit;
err = git_commit_lookup(&base_commit, repo, &base);
if (err != 0) return check_err(err, "git_commit_lookup base");
git_tree* head_tree;
err = git_commit_tree(&head_tree, head_commit);
if (err != 0) return check_err(err, "git_commit_tree head");
git_tree* base_tree;
err = git_commit_tree(&base_tree, base_commit);
if (err != 0) return check_err(err, "git_commit_tree base");
git_diff* diff;
err = git_diff_tree_to_tree(&diff, repo, base_tree, head_tree, NULL);
if (err != 0) return check_err(err, "git_diff_tree_to_tree");
git_diff_find_options options;
err = git_diff_find_options_init(&options, GIT_DIFF_FIND_OPTIONS_VERSION);
if (err != 0) return check_err(err, "git_diff_find_options_init");
options.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES | GIT_DIFF_BREAK_REWRITES | GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY;
options.rename_threshold = 30;
options.copy_threshold = 30;
options.rename_limit = 1000000;
err = git_diff_find_similar(diff, &options);
if (err != 0) return check_err(err, "git_diff_find_similar");
// skip proper cleanup, this is just a testcase
return 0;
}
libgit2:
git clone https://github.com/libgit2/libgit2
mkdir libgit2-build
cd libgit2-build && cmake ../libgit2 && cmake --build .
testcase:
gcc -I libgit2/include/ -L libgit2-build/ main.c -l git2 -o main
LD_LIBRARY_PATH=libgit2-build/ ./main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment