首页 > 解决方案 > libgit2 - 为 ssh 键设置回调

问题描述

我正在尝试使用此代码推送到远程仓库

    void repository::push() {
        char *refspec = strdup(this->current_branch().name().c_str());
        const git_strarray refspecs = {
                &refspec,
                1
        };

        git_push_options options;
        git_remote* remote = nullptr;

        lg2_error(git_remote_lookup(&remote, repo_, "origin" ), "Unable to lookup remote", "");

        lg2_error(git_push_init_options(&options, GIT_PUSH_OPTIONS_VERSION ), "Error initializing push", "");

        lg2_error(git_remote_push(remote, &refspecs, &options), "Error pushing", "");

        printf("pushed\n");
    }

它主要来自 libgit2push.c示例,但我得到了错误

Error pushing [-1] - authentication required but no callback set

我想通过 ssh 密钥进行身份验证,但我无法弄清楚我必须在git_push_options回调中设置哪些选项才能做到这一点。我已经看到线程是人们读出.ssh目录而没有什么,但我希望有一个更简单的方法。或者最好只使用在 shell 上exec运行命令git push并停止使用 libgit2 的推送。

标签: c++libgit2

解决方案


推荐阅读