Search code examples
c++gitgit-clonelibgit2

How to set up correctly a git_clone?


I'm doing a C++ program that should clone several repositories using libgit2. The problem is that when I clone a repository, it clone the whole content into the path but it doesn't create a sub-file unlike when I do the same command with git bash where it clone into a subfile.

git_libgit2_init();
git_repository *repo = NULL;


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

    git_repository_free(repo);
    git_libgit2_shutdown();

Solution

  • Let's call the repository Foo.

    When you writes in bash:

    $ git clone url
    

    Using your libgit2 example it is quite equivalent to:

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

    Actually it means that the git_clone()'s third parameter is not the path to the directory where to clone the repository, but it is the path to the local repository itself (so you have to provide the repository name too, of course you can change it).

    Knowing this, if I use your example again and write the following:

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

    It will be equivalent as if I had written in bash:

    $ git clone url /path_to_somewhere/Bar
    

    The remote Foo repository will be renamed as Bar (only in my local repository of course).


    Therefore, to give you a concrete answer :) You just have to append the repository name to the destination path and it will be created as you expected.