简体   繁体   中英

how to print diff files in libgit2?

I tried this

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();

but I don't know what to send as a 3th and 4th parameter to function git_diff_print(), some ideas?

in libgit2 API, there is such declaration of this function

git_diff_print(git_diff *diff, git_diff_format_t format, git_diff_line_cb print_cb, void *payload);

but I don't know what are that last 2 parameters and how to send them to this function

When I try this example :

https://libgit2.github.com/libgit2/ex/HEAD/diff.html#git_diff_print-9

,it's not working for me

Finally, I got the information another way, there is my solution :

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM