c - git notes 迭代的例子

标签 c libgit2

我正在使用 libgit2 编写应用程序并坚持使用迭代 git-notes。

使用带有附加说明的单个提交创建 GIT 存储库示例:

$ mkdir git-repo
$ cd git-repo
$ git init
$ echo "file" > file
$ git commit file -m "file"
$ echo "notes" > notes
$ git notes add -F notes 32bc2d65241d3b34fb55758362d5bcb4483f36b9
$ git notes list                                                  
cf88e040a4be8874952acfaa44f627718bcfd547 32bc2d65241d3b34fb55758362d5bcb4483f36b9
$ git notes show 32bc2d65241d3b34fb55758362d5bcb4483f36b9
notes
$

用代码创建一个源文件:

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

int main (int argc, char** argv)     
{                                                    
        const char* notes_ref = "refs/notes/commits";              
        const char* repo_path = "./git-test";                                                             
        if (!(git_repository_open_ext(   
        NULL, repo_path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) == 0)) {
        exit(-1);                                    
        }                                            
        printf("%s is a GIT repository\n", repo_path);
        git_libgit2_init();
        git_repository *repo = NULL;
        int rc = 0;
        rc = git_repository_open(&repo, repo_path);
        if (rc < 0) {
                const git_error *e = giterr_last();
                printf("Error %d/%d: %s\n", rc, e->klass, e->message);
                exit(rc);
        }
        git_oid *annotated_id, *note_id;
        git_note_iterator *iter;
        rc = git_note_iterator_new(&iter, repo, notes_ref);
        if (rc < 0) {
                const git_error *e = giterr_last();
                printf("Error git_note_iterator_new() %d/%d: %s\n", rc, e->klass, e->message);
                exit(rc);
        }
        while(git_note_next(note_id, annotated_id, iter) != GIT_ITEROVER) {
                const char *note_msg;
                git_note *git_note;
                git_note_read(&git_note, repo, notes_ref, note_id);
                note_msg = git_note_message(git_note);
                printf("%s\n", note_msg);
        }
        git_note_iterator_free(iter);
        git_libgit2_shutdown();
}

将源代码编译为二进制:

gcc notes.c -g3 -O0 -o notes -L/usr/local/lib -lgit2 -I/usr/local/include

当我运行它时出现应用程序段错误:

$ gdb ./notes
(gdb) run
./git-test is a GIT repository

Program received signal SIGSEGV, Segmentation fault.
0x00000d0e8cb21f06 in git_oid_fromstrn () from /usr/local/lib/libgit2.so.11.0
Current language:  auto; currently minimal
(gdb) bt
#0  0x00000d0e8cb21f06 in git_oid_fromstrn () from /usr/local/lib/libgit2.so.11.0
#1  0x00000d0e8cb22117 in git_oid_fromstr () from /usr/local/lib/libgit2.so.11.0
#2  0x00000d0e8cb13f97 in process_entry_path () from /usr/local/lib/libgit2.so.11.0
#3  0x00000d0e8cb13c0a in git_note_next () from /usr/local/lib/libgit2.so.11.0
#4  0x00000d0c2cab249a in main (argc=1, argv=0x7f7fffff0218) at notes.c:31
(gdb)

例子有什么问题?

最佳答案

我什至不知道 gitlib API,但这不可能是正确的:

git_oid *annotated_id, *note_id;
[...]
while (git_note_next(note_id, annotated_id, iter) != GIT_ITEROVER) { ... }

变量是未初始化的指针,将按值传递给函数。它们尽可能地无用;如果 git_note_next 是一个对其参数有副作用的邪恶宏,它们将工作。

也许该 API 旨在与

git_oid annotated_id, note_id;
[...]
while (git_note_next(&note_id, &annotated_id, iter) != GIT_ITEROVER) { ... }

事实上你的错误似乎是没有一直使用-Wall:

gittest.c: In function ‘main’:
gittest.c:33:17: warning: ‘note_id’ may be used uninitialized in this function [-Wmaybe-uninitialized]
                 git_note_read(&git_note, repo, notes_ref, note_id);
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gittest.c:30:15: warning: ‘annotated_id’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         while(git_note_next(note_id, annotated_id, iter) != GIT_ITEROVER) {
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

关于c - git notes 迭代的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57132354/

相关文章:

C执行管道: execlp works while execvp doesn't

c# - Git 仓库内部格式解释

c# - 如何使用个人访问 token 进行 Libgit2sharp 身份验证

c - 为什么 7%-5 给出 2 而 -7%5 给出 -2?在这两种情况下都不应该是 -2 吗?

c - 不确定 'control may reach end of non-void function' 错误消息的含义

c - x86 addl 与 subl

ruby - 来自原点的坚固合并提交不会更新工作树

head - 使用 libgit2 获取远程 HEAD?

c++ - 使用 libgit2 在特定目录上调用 git diff

c - 如何检查 Linux 进程及其所有子进程何时退出?