2013-03-30 57 views
1

由于没有复制粘贴示例获取libgit2的最后一次提交,我以为我应该添加一个。 libgit2中的示例大量使用了git_oid_fromstr() ...如何使用libgit2从git仓库中获得HEAD的最后一次提交?

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

回答

4
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()一旦你用它做。

相关问题