连接到远程存储库并获取数据
Connect to remote repository and fetch data
我想连接到远程存储库并从他那里获取数据。我包含并使用了 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)
如何解决?两种方式中哪一种更正确?
您要连接的遥控器要求您进行身份验证。您需要使用 git_remote_connect
的 callbacks
参数来传递 git_cred_acquire_cb
(struct, for the callback 的文档)。
这将在请求身份验证以及您需要提供的身份验证 "type" 时被调用。有关详细信息(支持密码或 SSH 方法),请参阅文档的 cred 部分,以及用于构建回调应该 return.[=18= 的 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
将导致建立连接,如果成功,将进行提取。
我想连接到远程存储库并从他那里获取数据。我包含并使用了 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)
如何解决?两种方式中哪一种更正确?
您要连接的遥控器要求您进行身份验证。您需要使用 git_remote_connect
的 callbacks
参数来传递 git_cred_acquire_cb
(struct, for the callback 的文档)。
这将在请求身份验证以及您需要提供的身份验证 "type" 时被调用。有关详细信息(支持密码或 SSH 方法),请参阅文档的 cred 部分,以及用于构建回调应该 return.[=18= 的 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
将导致建立连接,如果成功,将进行提取。