Skip to content

Instantly share code, notes, and snippets.

@Quintus
Created October 9, 2012 14:57
Show Gist options
  • Save Quintus/3859349 to your computer and use it in GitHub Desktop.
Save Quintus/3859349 to your computer and use it in GitHub Desktop.
libgit2 segfault on git_checkout_tree()
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <git2.h>
int main(int argc, char* argv[])
{
git_repository* p_repo;
git_reference* p_ref;
git_reference* p_head;
const git_oid* p_oid;
git_commit* p_commit;
git_checkout_opts opts;
git_strarray ary;
char hash[41];
memset(hash, 0, 41);
ary.count = 0;
opts.checkout_strategy = GIT_CHECKOUT_DEFAULT; /* GIT_CHECKOUT_OVERWRITE_MODIFIED | GIT_CHECKOUT_CREATE_MISSING | GIT_CHECKOUT_REMOVE_UNTRACKED; */
opts.disable_filters = 0;
opts.dir_mode = 0755;
opts.file_mode = 0644;
opts.file_open_flags = O_CREAT | O_TRUNC | O_WRONLY;
opts.notify_payload = NULL;
opts.paths = ary;
printf("Opening repository... ");
git_repository_open(&p_repo, "foo/.git");
printf("Done.\n");
printf("Looking up refs/heads/master... ");
git_reference_lookup(&p_ref, p_repo, "refs/heads/master");
p_oid = git_reference_oid(p_ref);
git_oid_fmt(hash, p_oid);
printf("Resolves to %s\n", hash);
git_commit_lookup(&p_commit, p_repo, p_oid);
printf("Updating working tree... ");
git_checkout_tree(p_repo, (git_object*)p_commit, &opts, NULL);
printf("Done.\n");
printf("Adjusting HEAD... ");
git_reference_lookup(&p_head, p_repo, "HEAD");
git_reference_set_target(p_head, "refs/heads/master");
printf("Now points to %s.\n", git_reference_target(p_head));
printf("Freeing stuff.\n");
git_reference_free(p_head);
git_commit_free(p_commit);
git_reference_free(p_ref);
git_repository_free(p_repo);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment