gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:29:14 25 4
gpt4 key购买 nike

我想连接到远程存储库并从他那里获取数据。我包含并使用 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();

我不确定我是否做对了。现在,我遇到了三个错误:

problem with git_remote_connect, error message : 'authentication required but no callback set'
problem with git_remote_connected, error message : 'authentication required but no callback set'
problem with git_remote_lookup, error message : 'remote 'origin' does not exist'
problem with git_remote_fetch, error message : 'authentication required but no callback set'

我不明白如何正确连接到远程存储库并解决这些问题。因此,如果您提供有关如何解决这些问题的建议或分享链接,我将不胜感激。

更新

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

int cred_acquire_cb(git_cred** out,
const char* url,
const char* username_from_url,
unsigned int allowed_types,
void* payload)
{
return git_cred_userpass_plaintext_new(out, "user", "pass");
}

在主代码中,我写道:

git_remote_callbacks callback = GIT_REMOTE_CALLBACKS_INIT;
callback.credentials = cred_acquire_cb;

在互联网上,我找到了一些旧库版本的示例。现在我不明白如何在 git_remote_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;
}

但是编译器告诉我下一个错误:

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)

如何解决?两种方式哪一种更正确?

最佳答案

您正在连接的 Remote 正在要求您自己进行身份验证。您需要使用 git_remote_connectcallbacks 参数来传递 git_cred_acquire_cb ( struct 的文档, callback 的文档)。

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

这是一个最小的示例(有意出现编译错误):

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);
}

请注意,您不需要执行所有步骤(即连接、检查连接),调用 git_remote_fetch 会导致建立连接,如果成功,则会进行提取.

关于c++ - 连接到远程存储库并获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51957383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com