Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 连接到远程存储库并获取数据_C++_C_Git_Libgit2 - Fatal编程技术网

C++ 连接到远程存储库并获取数据

C++ 连接到远程存储库并获取数据,c++,c,git,libgit2,C++,C,Git,Libgit2,我想连接到远程存储库并从他那里获取数据。我包括并使用libgit2库。这是我的代码: const char* url = "http://address.to.repository"; git_libgit2_init(); git_repository* repo = nullptr; git_remote* newremote = nullptr; git_buf* buf; git_strarray remotes = {0}; if (git_repository_init(&am

我想连接到远程存储库并从他那里获取数据。我包括并使用libgit2库。这是我的代码:

const char* url = "http://address.to.repository";
git_libgit2_init();

git_repository* repo = nullptr;
git_remote* newremote = nullptr;
git_buf* buf;
git_strarray remotes = {0};


if (git_repository_init(&repo, "/tmp/gittest", false) != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_repository_init, error message : '" << lastError->message <<"'"<< std::endl;
}

if (git_remote_create_anonymous(&newremote, repo, url) != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_remote_create_anonymous, error message : '" << lastError->message <<"'"<< std::endl;
}


if (git_remote_connect(newremote, GIT_DIRECTION_FETCH, nullptr, nullptr, nullptr) != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_remote_connect, error message : '" << lastError->message <<"'"<< std::endl;
}

if (git_remote_connected(newremote) != 1)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_remote_connected, error message : '" << lastError->message <<"'"<< std::endl;
}

git_remote_add_fetch(repo, "origin", "refs/heads/master:refs/heads/master");


if (git_remote_lookup(&newremote, repo, "origin") != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_remote_lookup, error message : '" << lastError->message <<"'"<< std::endl;
}

if (git_remote_fetch(newremote, nullptr, nullptr, nullptr) != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_remote_fetch, error message : '" << lastError->message <<"'"<< std::endl;
}

git_remote_free(newremote);
git_repository_free(repo);
git_libgit2_shutdown();
我不明白如何正确连接到远程存储库并解决这些问题。因此,如果您能就如何解决这些问题提供建议或分享链接,我将不胜感激

更新

我已经阅读了文档并查看了讨论libgit2不同问题的主题。我创建了一个可以解决我的问题的函数(可能:)

在主代码中,我写:

git_remote_callbacks callback = GIT_REMOTE_CALLBACKS_INIT;
callback.credentials = cred_acquire_cb;
在网上,我找到了一些图书馆旧版本的例子。现在我不明白如何在
git\u remote\u connect
函数中使用这个回调选项

更新2

我试着走另一条路,做了这样一件事:

git_strarray remotes = {0};
git_cred* cred = nullptr;
git_proxy_options opt = GIT_PROXY_OPTIONS_INIT;

if (git_cred_userpass_plaintext_new(&cred, "user", "pass") != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_cred_userpass_plaintext_new, error message : '" << lastError->message <<"'"<< std::endl;
}

if (git_remote_connect(newremote, GIT_DIRECTION_FETCH, git_cred_acquire_cb(&cred, url, "username@bitbucket.org", 1, nullptr), opt, remotes) != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_remote_connect, error message : '" << lastError->message <<"'"<< std::endl;
}

如何修复它?两种方式中哪一种更正确?

您连接的远程设备要求您自己进行身份验证。您需要使用
git\u remote\u connect
callbacks
参数来传递
git\u cred\u acquire\u cb
(用于的文档,用于的文档)

当请求身份验证时,将调用该函数,以及需要提供的身份验证的“类型”。有关详细信息(支持密码或SSH方法),以及用于构建回调应返回的
git\u cred
对象的详细信息,请参阅文档部分

下面是一个最小的示例(预期的编译错误):


请注意,您不需要完成所有步骤(即连接、检查连接),调用
git\u remote\u fetch
将导致建立连接,如果成功,将进行提取。

我已更新了我的主题。请向上看。也许你有一些建议。你的例子很好!现在我想知道如何从fetchdata提交消息、ssh和author中获取数据?我必须使用哪个函数?另外,有趣的是如何获得远程分支的名称?我试着使用
git\u branch\u remote\u name
,但我不明白这个函数中的第三个参数
canonical\u branch\u name
是什么意思。现在您的抓取已经完成,这意味着来自远程的提交在您的存储库中,所以应该使用标准存储库函数(例如,git_revwalk将允许您列出提交,其中将包含消息和作者)。我不明白“ssh”指的是什么,因为它只是您的远程设备正在/可能正在使用的底层传输,在功能上并不重要,所以请查看它的URL。对于
git\u branch\u remote\u name
,第三个参数似乎必须是引用名称,它将返回其分支短名称。为了澄清,
“refs/remotes/origin/master”
返回
“master”
,我敢打赌
“refs/remotes/origin/fix/404 error”将返回
“fix/404 error”`。只是指出您在最初的示例中做了一些奇怪的事情:“匿名”“远程设备仅在内存中,永远不会在存储库中创建。因此,您的创建/连接将始终失败,因为没有配置的URL。您的
git\u remote\u add\u fetch
的目标是现有的git远程设备(它可能正在创建它),但随后您将再次加载它并获取此文件,因此您的初始匿名远程从未使用过(并且可能实际上总是无法执行任何操作,因为它没有配置URL)。@tiennou如果我创建了一个匿名远程存储库,我无法连接并从他那里获取数据?或者我误解了您的意思?请忽略我刚才所说的;-)。你在创建遥控器时设置了URL,我在阅读文档时忽略了这一点。这只留下了一点,即您实际上是在践踏“origin”的URL,并从中取回。
git_remote_callbacks callback = GIT_REMOTE_CALLBACKS_INIT;
callback.credentials = cred_acquire_cb;
git_strarray remotes = {0};
git_cred* cred = nullptr;
git_proxy_options opt = GIT_PROXY_OPTIONS_INIT;

if (git_cred_userpass_plaintext_new(&cred, "user", "pass") != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_cred_userpass_plaintext_new, error message : '" << lastError->message <<"'"<< std::endl;
}

if (git_remote_connect(newremote, GIT_DIRECTION_FETCH, git_cred_acquire_cb(&cred, url, "username@bitbucket.org", 1, nullptr), opt, remotes) != 0)
{
    const git_error *lastError = giterr_last();
    std::cerr << "problem with git_remote_connect, error message : '" << lastError->message <<"'"<< std::endl;
}
error: expression list treated as compound expression in functional cast [-fpermissive]
     if (git_remote_connect(newremote, GIT_DIRECTION_FETCH, git_cred_acquire_cb(&cred, url, "ableigdev@bitbucket.org", 1, nullptr), opt, remotes) != 0)

error: cannot convert ‘git_cred_acquire_cb {aka int (*)(git_cred**, const char*, const char*, unsigned int, void*)}’ to ‘const git_remote_callbacks*’ for argument ‘3’ to ‘int git_remote_connect(git_remote*, git_direction, const git_remote_callbacks*, const git_proxy_options*, const git_strarray*)’
     if (git_remote_connect(newremote, GIT_DIRECTION_FETCH, git_cred_acquire_cb(&cred, url, "ableigdev@bitbucket.org", 1, nullptr), opt, remotes) != 0)
int my_git_cred_cb(git_cred **cred, const char *url, const char *username_from_url, unsigned int allowed_types, void *payload)
{
    if ((allowed_type & GIT_CREDTYPE_USERPASS_PLAINTEXT) != 0) {
        return git_cred_userpass_plaintext_new(&cred, "user", "pass");
    } else {
        /* Some other kind of authentication was requested */
        return -1;
    }

    return 1; /* unable to provide authentication data */
}

void perform_fetch(git_remote *remote)
{
    git_fetch_options opts = GIT_FETCH_OPTIONS_INIT;

    opts.callbacks.credentials = my_git_cred_cb;
    opts.callbacks.payload = NULL; /* you'll get that pointer back as the payload parameter */

    return git_remote_fetch(remote, NULL, &opts, NULL);
}