Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git 创建基于ssh的远程路径_Git_Ssh_Libgit2 - Fatal编程技术网

Git 创建基于ssh的远程路径

Git 创建基于ssh的远程路径,git,ssh,libgit2,Git,Ssh,Libgit2,应该如何设置url以向初始化的存储库添加基于ssh的远程路径?我试过了 git_remote_create(&remote, repo, "origin", "ssh://git@github.com/libgit2/TestGitRepository"); 但是它不能正确地fetch,并且git\u remote\u stats返回0个接收到的对象。如果将ssh替换为https,git\u fetch按预期工作。同样的url也适用于git_cl

应该如何设置url以向初始化的存储库添加基于
ssh
远程
路径?我试过了

git_remote_create(&remote, repo, "origin", "ssh://git@github.com/libgit2/TestGitRepository");
但是它不能正确地
fetch
,并且
git\u remote\u stats
返回0个接收到的对象。如果将
ssh
替换为
https
git\u fetch
按预期工作。同样的url也适用于git_clone(参见注释):

我还尝试设置
ssh://git@github.com:libgit2/TestGitRepository
,但它也不起作用。

检查是否会更好,如中所示

检查是否工作得更好,如中所示


问题的原因是我没有使用凭据回调

从libgit2中的回调部分:

有关凭据示例,请签出,它使用。另请参见内置的

下面是一个使用ssh初始化+获取+签出repo的简单示例:

#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;
}
#包括
#包括
int-credentials\u-cb(git\u-credential**out,const-char*url,const-char*username\u-from\u-url,
允许的无符号整数(类型,无效*有效负载)
{
INTERR;
char*username=NULL,*password=NULL,*privkey=NULL,*pubkey=NULL;
用户名=strdup(“git”);
密码=strdup(“密码”);
pubkey=strdup(“/home/user/.ssh/id_rsa.pub”);
privkey=strdup(“/home/user/.ssh/id_rsa”);
err=git\u凭证\u ssh\u密钥\u新(out、用户名、公钥、私钥、密码);
免费(用户名);
免费(密码);
免费(私钥);
免费(公开密钥);
返回错误;
}
内部主(空)
{
git_仓库*回购;
git_libgit2_init();
git_repository_init(&repo,“libgit2testssh/”,0);
git_remote*remote;
git_远程_创建(
&远程,回购,“来源”,
"ssh://git@github.com/libgit2/TestGitRepository”);
git\u fetch\u options fetch\u opts=git\u fetch\u options\u INIT;
fetch_opts.callbacks.credentials=credentials_cb;
git_remote_fetch(remote,NULL,&fetch_opts,NULL);
git_对象*分支;
git_revparse_single(&branch,repo,“远程/源/主”);
git\u checkout\u options checkout\u opts=git\u checkout\u options\u INIT;
git校验树(回购、分支和校验选项);
git_对象_自由(分支);
git_远程免费(远程);
无git(回购);
git_libgit2_shutdown();
返回0;
}

问题的原因是我没有使用凭据回调

从libgit2中的回调部分:

有关凭据示例,请签出,它使用。另请参见内置的

下面是一个使用ssh初始化+获取+签出repo的简单示例:

#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;
}
#包括
#包括
int-credentials\u-cb(git\u-credential**out,const-char*url,const-char*username\u-from\u-url,
允许的无符号整数(类型,无效*有效负载)
{
INTERR;
char*username=NULL,*password=NULL,*privkey=NULL,*pubkey=NULL;
用户名=strdup(“git”);
密码=strdup(“密码”);
pubkey=strdup(“/home/user/.ssh/id_rsa.pub”);
privkey=strdup(“/home/user/.ssh/id_rsa”);
err=git\u凭证\u ssh\u密钥\u新(out、用户名、公钥、私钥、密码);
免费(用户名);
免费(密码);
免费(私钥);
免费(公开密钥);
返回错误;
}
内部主(空)
{
git_仓库*回购;
git_libgit2_init();
git_repository_init(&repo,“libgit2testssh/”,0);
git_remote*remote;
git_远程_创建(
&远程,回购,“来源”,
"ssh://git@github.com/libgit2/TestGitRepository”);
git\u fetch\u options fetch\u opts=git\u fetch\u options\u INIT;
fetch_opts.callbacks.credentials=credentials_cb;
git_remote_fetch(remote,NULL,&fetch_opts,NULL);
git_对象*分支;
git_revparse_single(&branch,repo,“远程/源/主”);
git\u checkout\u options checkout\u opts=git\u checkout\u options\u INIT;
git校验树(回购、分支和校验选项);
git_对象_自由(分支);
git_远程免费(远程);
无git(回购);
git_libgit2_shutdown();
返回0;
}

您是否正在检查git_clone中的错误?您是使用ssh支持构建的吗?
git\u clone
正在返回err-4(git\u EEXISTS),这是在dest dir已经退出时给出的。我错误地认为,
git\u clone
正在覆盖dest dir(因为在这种情况下,
git\u fetch
崩溃)。显然,我从未使用
git\u clone
通过ssh获取数据。您是否在
git\u clone
中检查错误?您是使用ssh支持构建的吗?
git\u clone
正在返回err-4(git\u EEXISTS),这是在dest dir已经退出时给出的。我错误地认为,
git\u clone
正在覆盖dest dir(因为在这种情况下,
git\u fetch
崩溃)。显然,我从未使用
git\u clone
通过ssh获取。感谢您建议使用
git\u remote\u create\u with\u opts
的解决方案。很高兴知道如何使用更多选项获取。感谢您建议使用
git\u remote\u create\u with\u opts
的解决方案。很高兴知道如何获取更多选项。
#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;
}