创建基于 ssh 的远程路径
Create ssh based remote path
如何设置 url 以将基于 ssh
的 remote
路径添加到已初始化的存储库?我试过
git_remote_create(&remote, repo, "origin", "ssh://git@github.com/libgit2/TestGitRepository");
但它没有 fetch
正确并且 git_remote_stats
returns 0 收到对象。如果 ssh
替换为 https
,git_fetch
将按预期工作。 相同的 url 与 git_clone
配合使用效果很好(见评论):
git_clone(&repo, "ssh://git@github.com/libgit2/TestGitRepository", "foo", &clone_opts);
我也试过设置ssh://git@github.com:libgit2/TestGitRepository
,但是也没用
检查是否git_remote_create_with_opts
would work better, as in this test
git_remote *remote;
git_strarray array;
git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
opts.name = "test-new";
opts.repository = _repo;
opts.fetchspec = "+refs/*:refs/*";
cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
问题是因为我没有使用凭据回调。
来自 libgit2 中的回调部分 documentation:
For a credentials example, check out the fetch example, which uses an interactive credential callback. See also the built-in credential helpers.
下面是一个使用 ssh 初始化+获取+签出存储库的最小示例:
#include <git2.h>
#include <string.h>
int credentials_cb(git_credential **out, const char *url, const char *username_from_url,
unsigned int allowed_types, void *payload)
{
int err;
char *username = NULL, *password = NULL, *privkey = NULL, *pubkey = NULL;
username = strdup("git");
password = strdup("PASSWORD");
pubkey = strdup("/home/user/.ssh/id_rsa.pub");
privkey = strdup("/home/user/.ssh/id_rsa");
err = git_credential_ssh_key_new(out, username, pubkey, privkey, password);
free(username);
free(password);
free(privkey);
free(pubkey);
return err;
}
int main(void)
{
git_repository *repo;
git_libgit2_init();
git_repository_init(&repo, "libgit2-test-ssh/", 0);
git_remote *remote;
git_remote_create(
&remote, repo, "origin",
"ssh://git@github.com/libgit2/TestGitRepository");
git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
fetch_opts.callbacks.credentials = credentials_cb;
git_remote_fetch(remote, NULL, &fetch_opts, NULL);
git_object *branch;
git_revparse_single(&branch, repo, "remotes/origin/master");
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
git_checkout_tree(repo, branch, &checkout_opts);
git_object_free(branch);
git_remote_free(remote);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}
如何设置 url 以将基于 ssh
的 remote
路径添加到已初始化的存储库?我试过
git_remote_create(&remote, repo, "origin", "ssh://git@github.com/libgit2/TestGitRepository");
但它没有 fetch
正确并且 git_remote_stats
returns 0 收到对象。如果 ssh
替换为 https
,git_fetch
将按预期工作。 相同的 url 与 配合使用效果很好(见评论):git_clone
git_clone(&repo, "ssh://git@github.com/libgit2/TestGitRepository", "foo", &clone_opts);
我也试过设置ssh://git@github.com:libgit2/TestGitRepository
,但是也没用
检查是否git_remote_create_with_opts
would work better, as in this test
git_remote *remote;
git_strarray array;
git_remote_create_options opts = GIT_REMOTE_CREATE_OPTIONS_INIT;
opts.name = "test-new";
opts.repository = _repo;
opts.fetchspec = "+refs/*:refs/*";
cl_git_pass(git_remote_create_with_opts(&remote, TEST_URL, &opts));
问题是因为我没有使用凭据回调。
来自 libgit2 中的回调部分 documentation:
For a credentials example, check out the fetch example, which uses an interactive credential callback. See also the built-in credential helpers.
下面是一个使用 ssh 初始化+获取+签出存储库的最小示例:
#include <git2.h>
#include <string.h>
int credentials_cb(git_credential **out, const char *url, const char *username_from_url,
unsigned int allowed_types, void *payload)
{
int err;
char *username = NULL, *password = NULL, *privkey = NULL, *pubkey = NULL;
username = strdup("git");
password = strdup("PASSWORD");
pubkey = strdup("/home/user/.ssh/id_rsa.pub");
privkey = strdup("/home/user/.ssh/id_rsa");
err = git_credential_ssh_key_new(out, username, pubkey, privkey, password);
free(username);
free(password);
free(privkey);
free(pubkey);
return err;
}
int main(void)
{
git_repository *repo;
git_libgit2_init();
git_repository_init(&repo, "libgit2-test-ssh/", 0);
git_remote *remote;
git_remote_create(
&remote, repo, "origin",
"ssh://git@github.com/libgit2/TestGitRepository");
git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
fetch_opts.callbacks.credentials = credentials_cb;
git_remote_fetch(remote, NULL, &fetch_opts, NULL);
git_object *branch;
git_revparse_single(&branch, repo, "remotes/origin/master");
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
git_checkout_tree(repo, branch, &checkout_opts);
git_object_free(branch);
git_remote_free(remote);
git_repository_free(repo);
git_libgit2_shutdown();
return 0;
}