如何在 C++ 中使用 libgit2 和 ssh 推送到远程存储库?

问题描述 投票:0回答:0

我想用 libgit2 (v1.4.2) 重现“git push”命令,我的 ide 是 Visual Studio 2022,我的包管理器是 vcpkg。

我已经尝试过文档中的方法“push.c” 在 git_remote_push 上失败(错误 -1,“%sunsupported URL 协议”)。

我也测试了这段代码:

int push(git_repository* repository) {
    // get the remote.
    git_remote* remote = NULL;
    cout << "remote lookup " << git_remote_lookup(&remote, repository, "origin") << endl;

    // vérification
    cout << "remote user : " << git_remote_name(remote) << endl;
    cout << "remote user : " << git_remote_url(remote) << endl;

    // connect to remote
    git_proxy_options opt = GIT_PROXY_OPTIONS_INIT;
    git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
    callbacks.credentials = &creds;
    cout << "remote connect " << git_remote_connect(remote, GIT_DIRECTION_PUSH, &callbacks, &opt, NULL) << endl;

    // track error
    const git_error* e = giterr_last();
    cout << "connect wrong!\n" << "message: %s" << e->message << endl;

    // add a push refspec
    cout << "remote add push " << git_remote_add_push(repository, "origin", "refs/heads/master:refs/heads/master") << endl;

    // configure options
    git_push_options options;
    cout << "push init options " << git_push_init_options(&options, GIT_PUSH_OPTIONS_VERSION) << endl;

    // do the push
    cout << "remote upload " << git_remote_upload(remote, NULL, &options) << endl;

    git_remote_free(remote);
    return 0;
}

如我所料,git_remote_connect 总是返回 -1 并且错误消息是 %sunsupported URL 协议。

c++ visual-studio ssh libgit2 vcpkg
© www.soinside.com 2019 - 2024. All rights reserved.