You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用libgit2获取待合并远程变更列表失败的问题排查

如何用libgit2获取待合并的远程变更列表

我使用libgit2管理程序脚本的安装与更新,已基于官方示例实现了git pull功能(本质是执行git fetch后再执行git merge)。现在需要实现一个“手动更新”按钮,核心需求是:在用户确认接受更新前,展示本地仓库即将合并的远程变更列表,方便用户备份本地可能修改过的常用脚本。

我参考libgit2的status.c示例进行开发,但未达到预期效果,甚至不确定git_status_list_new()是否能满足需求。

测试环境情况

我搭建了测试环境:两个本地脚本仓库克隆版本,其中程序使用的工作仓库落后远程仓库(origin/main)一个提交,已执行fetch但未merge。命令行执行git status显示:

$ git status
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)

nothing to commit, working tree clean

代码问题

运行我的代码时,git_status_list_new()未返回任何结果,git_status_list条目数为0,与预期不符。相关代码如下:

结构体定义

struct status_opts {
    git_status_options statusopt;
    char *repodir;
    char *pathspec[MAX_PATHSPEC];
    int npaths;
    int format;
    int zterm;
    int showbranch;
    int showsubmod;
    int repeat;
};

核心函数preview_update()

int preview_update() {
    // Initialize libgit2
    git_libgit2_init();

    // Local directory where the repository will be cloned
    const gchar *local_path = get_scripts_repo_path();

    git_repository *repo = NULL;
    int error = git_repository_open(&repo, local_path);
    if (error < 0) {
        log_color_message(_("Error opening repository: %s\n"), "red", giterr_last()->message);
        return 1;
    }

    // Fetch changes
    lg2_fetch(repo);

    git_status_list *status;
    struct status_opts o;
    git_status_options_init(&o.statusopt, GIT_STATUS_OPTIONS_VERSION);
    o.repodir = g_strdup(local_path);

    o.format = FORMAT_LONG;

    if (git_repository_is_bare(repo)) {
        log_color_message(_("Cannot report status on bare repository"), "red",
            git_repository_path(repo));
        git_repository_free(repo);
        git_libgit2_shutdown();
    }

    /**
     * Run status on the repository
     *
     * We use `git_status_list_new()` to generate a list of status
     * information which lets us iterate over it at our
     * convenience and extract the data we want to show out of
     * each entry.
     */
    check_lg2(git_status_list_new(&status, repo, &o.statusopt),
        _("libgit2: could not get status"), NULL);

    if (o.showbranch)
        show_branch(repo, o.format);

    print_long(status); // <---- here is where the status should print the uncommitted changes

    git_status_list_free(status);
    g_free(o.repodir);
    debug_print("%s\n", git_pending_commit_buffer->str);
    log_message(_("Completed checking pending commits to scripts repository...\n"));
    if (!git_pending_commit_buffer)
        log_color_message(_("Local repository is up to date.\n"), "green");
    git_repository_free(repo);
    git_libgit2_shutdown();
    return 0;
}
size_t i, maxi = git_status_list_entrycount(status);
printf("status items: %lu\n", maxi);

执行后printf输出为status items: 0,没有获取到预期的变更信息。我确认lg2_fetch()功能正常(基于该函数的pull功能可正常运行),可能git_status_list_new()并非所需接口,或我对示例理解有误,希望得到获取待合并提交变更列表的方法指导。


内容的提问来源于stack exchange,提问作者Adrian K-B.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 19:40:53