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

时间:2023-02-04 01:19:39

In C++ using libgit2, I'd like to create a new local repository where its master branch is based on specific-branch from another local repository, maintaining its history so I can later synch between the two.

在使用libgit2的c++中,我希望创建一个新的本地存储库,在这个存储库中,它的主分支基于来自另一个本地存储库的特定分支,维护它的历史,以便稍后在这两个存储库之间进行同步。

Essentially, I'm attempting the following, except using libgit2:

基本上,除了使用libgit2之外,我正在尝试以下步骤:

https://*.com/a/9529847/1019385

https://*.com/a/9529847/1019385

So if I had files arranged as follows:

所以如果我有如下的文件:

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

/老。git(分支:主人,一枝分支)

./old/* [files and clone of ./old.git at specific-branch]

./old/*[文件和./old的克隆。git在一枝分支)

Where the commands would be something like:

命令是这样的:

git init --bare ./new.git
cd ./old
git push ./new.git +specific-branch:master

And come up with something like (removed error checking to reduce code):

并提出类似(删除错误检查以减少代码):

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);

What I'm not really sure is where to go from here and how to invoke git_remote_push() properly where it actually does something. This currently has no side effects, as ./old.git is not referenced. That is, ./new.git is created properly, but it doesn't contain contents of ./old.git/./old/*.

我不确定从这里到哪里,以及如何正确地调用git_remote_push(),在那里它实际做了一些事情。这目前没有副作用,如。/旧的。git不是引用。也就是说,。/新。正确地创建了git,但是它不包含./old.git/. old/*的内容。

Help much appreciated.

感谢帮助。


Based on an answer suggesting a "fetch" approach, I've also attempted the following:

基于一个建议“获取”方法的答案,我还尝试了以下方法:

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();
}

This still has no effect.

这仍然没有效果。

2 个解决方案

#1


2  

In straight git the most flexible and direct method (as it doesn't require you to already have the entire repository you only want pieces of) is e.g.

在直接git中,最灵活和直接的方法(因为它不需要您已经拥有您只想要的部分的整个存储库)是例如。

git init --bare new.git; cd $_

git fetch --no-tags ~/src/git next:master    # to fetch and rename a branch
# or
git fetch ~/src/git v2.17.0; git branch master FETCH_HEAD   # full handroll

To do this in libgit2, you can create a repository as usual with git_repository_init and an in-memory "anonymous" remote from a url (i'd hope a path would do as well, check that) with git_remote_create_anonymous, then git_remote_download git_remote_fetch the refspec you want from that.

要在libgit2中实现这一点,您可以使用git_repository_init和一个来自url的内存中的“匿名”远程(我希望路径也能做到这一点,请检查)使用git_remote_create_anonymous创建一个存储库,然后使用git_remote_download git_remote_remote_remote_fetch从url获取所需的refspec。

#2


0  

It looks like you're creating a new repository, and then adding a remote on it and trying to use it to push to itself... If you want to truly emulate your commands, you'll need two repositories:

看起来您正在创建一个新的存储库,然后在它上面添加一个远程,并尝试使用它来推动自己……如果您想真正地模拟您的命令,您将需要两个存储库:

  1. git_repository_init the new.git, then
  2. git_repository_init新。git,然后
  3. git_repository_open the old, and then set up the remote on it, and push it to the new repository.
  4. git_repository_open旧的,然后在它上面设置远程,并将它推送到新的存储库。

Something along the lines of:

类似于:

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);

#1


2  

In straight git the most flexible and direct method (as it doesn't require you to already have the entire repository you only want pieces of) is e.g.

在直接git中,最灵活和直接的方法(因为它不需要您已经拥有您只想要的部分的整个存储库)是例如。

git init --bare new.git; cd $_

git fetch --no-tags ~/src/git next:master    # to fetch and rename a branch
# or
git fetch ~/src/git v2.17.0; git branch master FETCH_HEAD   # full handroll

To do this in libgit2, you can create a repository as usual with git_repository_init and an in-memory "anonymous" remote from a url (i'd hope a path would do as well, check that) with git_remote_create_anonymous, then git_remote_download git_remote_fetch the refspec you want from that.

要在libgit2中实现这一点,您可以使用git_repository_init和一个来自url的内存中的“匿名”远程(我希望路径也能做到这一点,请检查)使用git_remote_create_anonymous创建一个存储库,然后使用git_remote_download git_remote_remote_remote_fetch从url获取所需的refspec。

#2


0  

It looks like you're creating a new repository, and then adding a remote on it and trying to use it to push to itself... If you want to truly emulate your commands, you'll need two repositories:

看起来您正在创建一个新的存储库,然后在它上面添加一个远程,并尝试使用它来推动自己……如果您想真正地模拟您的命令,您将需要两个存储库:

  1. git_repository_init the new.git, then
  2. git_repository_init新。git,然后
  3. git_repository_open the old, and then set up the remote on it, and push it to the new repository.
  4. git_repository_open旧的,然后在它上面设置远程,并将它推送到新的存储库。

Something along the lines of:

类似于:

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);