使用libgit2从另一个分支创建新的git存储库?

Mik*_*eir 7 c++ git libgit2

在使用libgit2的C++中,我想创建一个新的本地存储库,其master分支基于specific-branch另一个本地存储库,保持其历史记录,以便稍后我可以在两者之间同步.

基本上,我尝试以下,除了使用libgit2:

/sf/answers/667089321/

所以,如果我的文件安排如下:

./old.git [branches:master,specific-branch]

./old/*[特定分支的./old.git文件和克隆]

命令的位置如下:

git init --bare ./new.git
cd ./old
git push ./new.git +specific-branch:master
Run Code Online (Sandbox Code Playgroud)

并想出类似的东西(删除错误检查以减少代码):

git_libgit2_init();
git_repository* repo = nullptr;
git_repository_init(&repo, "./new.git", true);
git_remote_create(&remote, repo, "origin", "./new.git");
git_remote_add_push(repo, "origin", "+specific-branch:master");
git_push_options optionsPush = GIT_PUSH_OPTIONS_INIT;
git_remote_push(remote, nullptr, &optionsPush);
Run Code Online (Sandbox Code Playgroud)

我不确定的是从这里开始的地方以及如何git_remote_push()正确地调用实际执行某些操作的地方.目前没有副作用,因为./old.git没有参考.也就是说,./new.git是正确创建的,但它不包含./old.git/的内容./old/*.

非常感谢.


基于建议采用"获取"方法的答案,我还尝试了以下方法:

git_repository* repo = nullptr;
if (git_repository_init(&repo, "./new.git", true)) {
    FATAL();
}
git_remote* remote;
git_remote_create_anonymous(&remote, repo, "./old");
char* specs[] = { _strdup("specific-branch:master"), nullptr };
git_strarray refspecs;
refspecs.count = 1;
refspecs.strings = specs;
if (git_remote_download(remote, &refspecs, NULL)) {
    FATAL();
}
Run Code Online (Sandbox Code Playgroud)

这仍然没有效果.

Edw*_*son -1

看起来您正在创建一个新的存储库,然后在其上添加一个遥控器并尝试使用它来推送到自身...如果您想真正模拟您的命令,您将需要两个存储库:

  1. git_repository_initnew.git,那么
  2. git_repository_open,然后在old上设置远程,并将推送到新存储库。

大致如下:

git_repository *old = NULL, *new = NULL;

git_libgit2_init();
git_repository_init(&new, "./new.git", true);
git_repository_free(new);

git_repository_open(&old, "./old");
git_remote_create(&remote, old, "origin", "./new.git");
git_remote_add_push(old, "origin", "+specific-branch:master");
git_remote_push(remote, NULL, NULL);
git_repository_free(old);
Run Code Online (Sandbox Code Playgroud)