使用libgit2列出分支中的所有提交

pen*_*tix 5 c git libgit2

如何使用libgit2遍历分支的所有提交?

我已经有了下面的代码,但它没有编译.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <git2.h>

int main(int argc, char *argv[]){
    git_repository *repo;
    git_repository_open(&repo, ".");

    git_odb *obj_db;
    obj_db = git_repository_database(repo);

    git_object commit;
    git_revparse_single(&commit, repo, "HEAD");


    git_repository_free(repo);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

GCC报告:

log.c: In function ‘main’:
log.c:11:9: warning: assignment makes pointer from integer without a cast [enabled by default]
log.c:13:13: error: storage size of ‘commit’ isn’t known
Run Code Online (Sandbox Code Playgroud)

我用-lgit2旗子编译.从root-commit开始,是否有可能快速完成所有提交?

更新 新代码如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <git2.h>

int main(int argc, char *argv[]){
    git_repository *repo;
    git_repository_open(&repo, ".");

    git_odb *obj_db;
    obj_db = git_repository_database(repo);

    git_object *commit;
    git_revparse_single(&commit, repo, "HEAD");


    git_repository_free(repo);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

log.c:11: undefined reference to `git_repository_database'
log.c:14: undefined reference to `git_revparse_single'
Run Code Online (Sandbox Code Playgroud)

pen*_*tix 4

最后,我使用 libgit2 创建了一个工作版本。Carlos Mart\xc3\xadn Nieto 指出了正确的方向,以下示例与 libgit2 0.16配合得很好。我花了一些时间来研究我在github上的 libgit2-examples 存储库中找到的generic.cgit revwalk正是我所寻找的。

\n\n

我注意到 git 在我的提交消息末尾添加了一个换行符,可能是因为我总是用来nano编写它们,所以我不会打印出示例代码中的最后一个字符。

\n\n

如果有人读到这篇文章并遇到与我相同的问题,这里是工作代码:

\n\n
#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <git2.h>\n\n#define REPO ".git"\n\nint main(void){\n    git_repository *repo;\n    if(git_repository_open(&repo, REPO) != GIT_SUCCESS){\n        fprintf(stderr, "Failed opening repository: \'%s\'\\n", REPO);\n        return 1;\n    }\n\n    // Read HEAD on master\n    char head_filepath[512];\n    FILE *head_fileptr;\n    char head_rev[41];\n\n    strcpy(head_filepath, REPO);\n\n    if(strrchr(REPO, \'/\') != (REPO+strlen(REPO)))\n        strcat(head_filepath, "/refs/heads/master");\n    else\n        strcat(head_filepath, "refs/heads/master");\n\n\n    if((head_fileptr = fopen(head_filepath, "r")) == NULL){\n        fprintf(stderr, "Error opening \'%s\'\\n", head_filepath);\n        return 1;\n    }\n\n    if(fread(head_rev, 40, 1, head_fileptr) != 1){\n        fprintf(stderr, "Error reading from \'%s\'\\n", head_filepath);\n        fclose(head_fileptr);\n        return 1;\n    }   \n\n    fclose(head_fileptr);\n\n\n    git_oid oid;\n    git_revwalk *walker;\n    git_commit *commit;\n\n    if(git_oid_fromstr(&oid, head_rev) != GIT_SUCCESS){\n        fprintf(stderr, "Invalid git object: \'%s\'\\n", head_rev);\n        return 1;\n    }\n\n    git_revwalk_new(&walker, repo);\n    git_revwalk_sorting(walker, GIT_SORT_TOPOLOGICAL);\n    git_revwalk_push(walker, &oid);\n\n    const char *commit_message;\n    const git_signature *commit_author;\n\n    while(git_revwalk_next(&oid, walker) == GIT_SUCCESS) {\n        if(git_commit_lookup(&commit, repo, &oid)){\n            fprintf(stderr, "Failed to lookup the next object\\n");\n            return 1;\n        }\n\n        commit_message  = git_commit_message(commit);\n        commit_author = git_commit_committer(commit);\n\n        // Don\'t print the \\n in the commit_message \n        printf("\'%.*s\' by %s <%s>\\n", strlen(commit_message)-1, commit_message, commit_author->name, commit_author->email);\n\n        git_commit_free(commit);\n    }\n\n    git_revwalk_free(walker);\n\n    return 0;\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

谢谢!

\n

  • 你好,我刚刚发现了你的这个列表,我想对你说声非常感谢,因为我也遇到了完全相同的问题。谢谢分享,好人! (2认同)