【发布时间】:2018-05-22 00:41:01
【问题描述】:
在使用 libgit2 的 C++ 中,我想创建一个新的本地存储库,其中其 master 分支基于来自另一个本地存储库的 specific-branch,维护其历史记录,以便稍后在两者之间进行同步。
基本上,我正在尝试以下操作,除了使用 libgit2:
https://stackoverflow.com/a/9529847/1019385
所以如果我有如下排列的文件:
./old.git [分支:master,特定分支]
./old/* [./old.git 在特定分支的文件和克隆]
命令应该是这样的:
git init --bare ./new.git
cd ./old
git push ./new.git +specific-branch:master
并想出类似的东西(删除错误检查以减少代码):
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);
我不太确定从这里去哪里以及如何正确调用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();
}
这仍然没有效果。
【问题讨论】:
-
请显示您正在查看的实际调试结果。 mcve 加上您期望结果(返回/效果)的特定点以及原因,以及您得到了什么以及为什么..
-
@jthill 显示预期结果。这是一个相当普遍的问题,可以使用任何存储库来完成。我还链接到一个问题,其中包含与我的问题有关的几个答案,而没有使用 libgit2。唯一的区别是我希望使用 libgit2 来避免 cli。
-
您正在做出没有任何证据支持的断言(“这仍然没有效果”),没有费心提供可编译的测试用例或显示您的诊断步骤,通常除了把它扔到墙上,看看它是否粘住了编码。难怪您的代码不起作用。我花了两个小时从全新的 libgit2 到工作代码,来帮助你,但我没有办法奖励这样一个不合作、不费力的需求。如果您费心去做这项工作,那么您已经解决了这个问题,或者至少说明了您在这里所做的任何错误假设。
-
这些函数的组合方式并不多。我只是不认为这些电话等于答案。我已经包含了一个示例源代码,它可以很容易地放入测试项目中,在您有权访问的任何存储库中。