git2go icon indicating copy to clipboard operation
git2go copied to clipboard

how to clone specific tag using git_clone()

Open jablkojem opened this issue 7 years ago • 3 comments

Hi all, I want to clone specific tag from my repository in bitbucket. Now I am able to clone just the whole repository. What should I add to my code to clone specific tag? I have seen this but it doesnt really helped me :

https://github.com/libgit2/git2go/issues/126


git_libgit2_init();

int num = 0;

git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
clone_opts.checkout_opts = checkout_opts;
clone_opts.fetch_opts.callbacks.credentials = cred_acquire_cb;

git_repository *cloned_repo = NULL;

int error = git_clone(&cloned_repo, all_urls.at(num).c_str(), clone_to.at(num).c_str(), &clone_opts);

if (error != 0) {
	const git_error *err = giterr_last();
	cerr << "error in clone num " << num << " -> message :" << err->message << endl;
}
else cout << endl << "Clone " << num << " succesful" << "(from url : " << all_urls.at(num) << "    " << "to path : " << clone_to.at(num) << ")" << endl;

git_repository_free(cloned_repo);
git_libgit2_shutdown();

Thanks for your help

jablkojem avatar Apr 03 '17 12:04 jablkojem

I added

const char* checkout_branch ="v0.0.1"; clone_opts.checkout_branch = checkout_branch;

but now there is an error message Reference 'refs/remotes/origin/v0.0.1' not found

jablkojem avatar Apr 03 '17 13:04 jablkojem

I don't know all of this but did you try to use the complete ref ? that is refs/tags/v0.0.1 I read roughly the code, I doubt it would work that easily. You might have to fetch tags and then checkout

apflieger avatar Apr 03 '17 14:04 apflieger

#126 (which you linked to) explains that libgit doesn't implement passing a tag as the branch to clone like the git command does. you will need to checkout the tag after cloning

jbg avatar Jul 02 '20 13:07 jbg