Skip to content

Instantly share code, notes, and snippets.

@nathan-osman
Last active December 14, 2015 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-osman/5164101 to your computer and use it in GitHub Desktop.
Save nathan-osman/5164101 to your computer and use it in GitHub Desktop.
Walks through the commits in the specified Git repository.
#include <git2.h>
#include <stdio.h>
#define REPOSITORY_DIR "Repository"
void print_error()
{
const git_error * last_error = giterr_last();
printf("Error: %s\n", last_error->message);
}
int main (int argc, char ** argv)
{
git_repository * repo = NULL;
git_revwalk * revwalk = NULL;
git_oid oid;
if(git_repository_open(&repo, REPOSITORY_DIR) ||
git_revwalk_new(&revwalk, repo) ||
git_revwalk_push_head(revwalk))
goto error;
while(!git_revwalk_next(&oid, revwalk))
{
git_commit * commit;
if(!git_commit_lookup(&commit, repo, &oid))
{
printf("- %s", git_commit_message(commit));
git_commit_free(commit);
}
else
printf("** Error looking up commit **\n");
}
goto done;
error:
print_error();
done:
if(revwalk) git_revwalk_free(revwalk);
if(repo) git_repository_free(repo);
return 0;
}
CC=gcc
git2-walk: git2-walk.o
$(CC) git2-walk.o -lgit2 -o git2-walk
git2-walk.o: git2-walk.c
$(CC) -c git2-walk.c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment