Dan
Dan

Reputation: 1571

How do I create a history graph

I am trying to get enough information to create a history graph using libgit2 but am unsure how I get things like branch names for commits as appropriate.

I want to reproduce something like that which is produced with the following git command:

git log --oneline --graph --decorate --all

This is the code I am using:

git_commit* old_head = NULL;
error = git_revparse_single( (git_object**) &old_head, open_repo, "refs/heads/master" );
if( error != 0 )
{
  return SG_PLUGIN_OK;
}


const git_oid *head_oid = git_object_id( (git_object*)old_head );

git_revwalk *walk;

git_revwalk_new( &walk, open_repo );
git_revwalk_sorting( walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_TIME );
git_revwalk_push( walk, head_oid );

const git_signature *cauth;
const char *cmsg;
git_time_t ctime;

git_oid newoid;
while( ( git_revwalk_next( &newoid, walk ) ) == 0 )
{
  git_commit *wcommit;
  error = git_commit_lookup( &wcommit, open_repo, &newoid );
  if( error != 0 )
  {
    return SG_PLUGIN_OK;
  }

  ctime = git_commit_time( wcommit );
  cmsg  = git_commit_message( wcommit );
  cauth = git_commit_author( wcommit );

  TRACE("\t%s (%s at %d)\n", cmsg, cauth->email, ctime );

  git_commit_free( wcommit );
}

git_revwalk_free( walk );

I can traverse the commits but i have several issues with the code above:

Upvotes: 4

Views: 849

Answers (1)

Carlos Martín Nieto
Carlos Martín Nieto

Reputation: 5277

Use a single walk and push whatever tips you want to use, you can use push_glob to push all branches if you like.

If you want to build a graph, then you need to look at what parents a commit has and build up your graph using that information.

As for branch names, if you're referring to what git-log's --decorate option does, you just have to remember what commit each branch tip points to and then draw it next to it whenever you're printing out that information.

Upvotes: 3

Related Questions