如何使用libgit2从git存储库中获取HEAD的最后一次提交?

时间:2013-03-30 10:43:48

标签: c++ c libgit2

由于没有使用libgit2进行最后一次提交的复制粘贴示例,我想我应该添加一个。 libgit2中的示例广泛使用git_oid_fromstr() ...

不要忘记libgit2目前处于全面开发阶段(2013年3月),因此请查看官方文档和源代码,因为每天都会添加新功能:

1 个答案:

答案 0 :(得分:5)

git_commit * getLastCommit ( git_repository * repo )
{
  int rc;
  git_commit * commit = NULL; /* the result */
  git_oid oid_parent_commit;  /* the SHA1 for last commit */

  /* resolve HEAD into a SHA1 */
  rc = git_reference_name_to_id( &oid_parent_commit, repo, "HEAD" );
  if ( rc == 0 )
  {
    /* get the actual commit structure */
    rc = git_commit_lookup( &commit, repo, &oid_parent_commit );
    if ( rc == 0 )
    {
      return commit;
    }
  }
  return NULL;
}

完成后,您需要致电git_commit_free()

相关问题