4

我正在尝试获取足够的信息来使用 libgit2 创建历史图表,但我不确定如何获得适当的提交分支名称等内容。

我想重现使用以下 git 命令生成的类似内容:

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

这是我正在使用的代码:

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

我可以遍历提交,但上面的代码有几个问题:

  • 我只是得到主主分支的提交,如果我迭代代码来更改每个分支名称的 revparse_single,那么我会得到所有提交,但有些提交显然是重复的,其中提交位于多个分支下
  • 我只是得到提交消息,作者和时间,那么我如何找出分支名称是什么?
  • 我做对了吗?
4

1 回答 1

3

使用一次步行并推送您想要使用的任何提示,push_glob如果您愿意,您可以使用它来推送所有分支。

如果你想构建一个图表,那么你需要查看提交有哪些父级,并使用该信息构建你的图表。

至于分支名称,如果您指的是 git-log--decorate选项的作用,您只需记住每个分支提示指向的提交,然后在打印出该信息时将其绘制在旁边。

于 2013-04-16T13:54:30.140 回答