创建基于ssh的远程路径

高尔卡

应该如何设置url以将ssh基础remote路径添加到已初始化的存储库?我尝试过

git_remote_create(&remote, repo, "origin", "ssh://[email protected]/libgit2/TestGitRepository");

但它不fetch正确,并git_remote_stats返回0个接收到的对象。如果ssh替换为httpsgit_fetch则按预期方式工作。相同的网址与 git_clone(请参阅评论):

git_clone(&repo, "ssh://[email protected]/libgit2/TestGitRepository", "foo", &clone_opts);

我也尝试设置ssh://[email protected]:libgit2/TestGitRepository,但是它也不起作用。

高尔卡

造成此问题的原因是我没有使用凭据回调。

从libgit2文档的回调部分中

对于凭证示例,请查看fetch示例,该示例使用交互式凭证回调另请参阅内置的凭据帮助器

下面是一个使用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://[email protected]/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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章