我想从 bitbucket 中的存储库克隆特定标签。现在我可以克隆整个存储库。我应该在代码中添加什么来克隆特定标签?
我看过这个,但它并没有真正帮助我:
https://github.com/libgit2/git2go/issues/126
git_libgit2_init();
int 数字 = 0;
git_clone_optionsclone_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错误 = git_clone(&cloned_repo, all_urls.at(num).c_str(), clone_to.at(num).c_str(), &clone_opts);
如果(错误!= 0){
const git_error *err = giterr_last();
cerr << "error in clone num " << num << " -> message :" << err->message << endl;
}
否则算了<< 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();
感谢您的宝贵时间
您可能需要创建本地存储库并手动从远程 URL 同步。
“克隆”的方式: git init -> 添加远程 url -> git fetch $refspec -> git checkout
通过使用 libgit2,示例代码如下:
// libgit version v1.7.0 (last release version)
// INPUT args
string git_address = "https://github.com/<org>/<repo>.git"
string branch, tag;
// start
git_repository *repo = nullptr;
git_remote *remote = nullptr;
// create a bare local repository
git_repository_init(&repo, "/your/local/path/", false);
// set remote url
git_remote_create(&remote, repo, "origin", git_address.c_str());
// generate refspec
// http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions
std::string refspec; // for git fetch
std::string revspec; // for git checkout
if (branch.size()) {
refspec = "+refs/heads/" + branch + ":refs/remotes/origin/" + branch;
revspec = "refs/remotes/origin/" + branch;
}
if (tag.size()) {
refspec = "+refs/tags/" + tag + ":refs/tags/" + tag;
revspec = "refs/tags/" + tag;
}
// construct fetch_opts and run fetch
// for libgit:v1.7.0 the GIT_FETCH_OPTIONS_INIT was
// missing some field, so we must assign then manually.
char *refspec_addr[] = {refspec.data()};
git_strarray refspecs{refspec_addr, 1};
git_fetch_options fetch_opt = GIT_FETCH_OPTIONS_INIT;
fetch_opt.proxy_opts.type = GIT_PROXY_AUTO;
fetch_opt.depth = 1;
fetch_opt.follow_redirects = GIT_REMOTE_REDIRECT_INITIAL;
fetch_opt.custom_headers = git_strarray{nullptr, 0};
git_remote_fetch(remote, &refspecs, &fetch_opt, NULL);
// find revision
git_object *rev;
git_revparse_single(&rev, repo, revspec.c_str());
// git checkout $branch
// git checkout tags/$tag
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
git_checkout_tree(repo, rev, &checkout_opts);
// finally close
git_remote_free(remote);
git_repository_free(repo);
附注:支持Http代理。 (在您的系统/用户配置 .gitconfig 中)