如何正确设置git_clone?

How to set up correctly a git_clone?

我正在做一个 C++ 程序,它应该使用 libgit2 克隆几个存储库。 问题是,当我克隆存储库时,它会将整个内容克隆到路径中,但它不会创建子文件,这与我使用 git bash 执行相同命令时不同,它会克隆到一个子文件。

git_libgit2_init();
git_repository *repo = NULL;


    error = git_clone(&repo, url, path, NULL);

    git_repository_free(repo);
    git_libgit2_shutdown();

让我们调用存储库 Foo.

当您在 bash 中写入时:

$ git clone url

使用您的 libgit2 示例它完全等同于:

error = git_clone(&repo, url, "./Foo", NULL);

其实就是说git_clone()的第三个参数不是克隆仓库目录的路径,而是本地仓库本身的路径(所以你必须提供仓库名字也是,当然你可以改。

知道了这一点,如果我再用你的例子写下:

error = git_clone(&repo, url ,"/path_to_somewhere/Bar", NULL);

这相当于我在 bash:

中写的
$ git clone url /path_to_somewhere/Bar

远程 Foo 存储库将重命名为 Bar(当然只在我的本地存储库中)。


因此,为了给您一个具体的答案:)您只需将存储库名称附加到目标路径,它就会按照您的预期创建。

错误信息是什么。 git_clone 做这样的事情:

git_libgit2_init();
git_repository *repo = NULL;
git_clone_options clone_options;//the struct is clone credential.and other info.
error = git_clone(&repo, url, path, &clone_options);

git_repository_free(repo);
git_libgit2_shutdown();