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

Connect to remote repository and fetch data

本文关键字:获取 数据 存储 程存储 连接      更新时间:2023-10-16

我想连接到远程存储库并从他那里获取数据。我包含并使用 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函数中使用此回调选项。

UPDATE2

我试图走另一条路并做了这个:

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)

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

您连接到的遥控器要求自己进行身份验证。您需要使用git_remote_connectcallbacks参数来传递git_cred_acquire_cb(结构的文档,回调的文档(。

这将在请求身份验证时调用,以及您需要提供的身份验证"类型"。有关详细信息(支持密码或 SSH 方法(,以及用于构建回调应返回的git_cred对象的详细信息,请参阅文档的 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将导致建立连接,如果成功,将发生获取。