如何使用libgit2在代码中将git子模块添加到超级项目 [英] How to add a git sub-module to a super-project in code using libgit2

查看:86
本文介绍了如何使用libgit2在代码中将git子模块添加到超级项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用libgit2 C API在代码中成功创建了两个git存储库(分别名为repo和module),现在正尝试将模块"存储库作为子模块添加到"repo"存储库中.我正在尝试使用libgit2 git_submodule_add_setup C API来执行此操作.参见 https://libgit2.github.com/libgit2/#HEAD/group/submodule/git_submodule_add_setup .

I have successfully created two git repositories (named repo and module) in code using the libgit2 C API and am now trying to add the "module" repository to the "repo" repository as a submodule. I am trying to use the libgit2 git_submodule_add_setup C API to do this. See https://libgit2.github.com/libgit2/#HEAD/group/submodule/git_submodule_add_setup.

文档中的内容如下:

git_submodule_add_setup:这将"git子模块添加"到子模块内容的提取和检出.它准备一个新的子模块,在.gitmodules中创建一个条目,并在工作目录中的给定路径或从工作目录到新仓库的gitlink中的.git/modules中创建一个空的初始化存储库.

git_submodule_add_setup: This does "git submodule add" up to the fetch and checkout of the submodule contents. It preps a new submodule, creates an entry in .gitmodules and creates an empty initialized repository either at the given path in the working directory or in .git/modules with a gitlink from the working directory to the new repo.

要完全模拟"git submodule add",请调用此函数,然后打开子模块存储库并根据需要执行克隆步骤.最后,调用git_submodule_add_finalize()结束将新的子模块和.gitmodules添加到索引以准备提交的过程.

To fully emulate "git submodule add" call this function, then open the submodule repo and perform the clone step as needed. Lastly, call git_submodule_add_finalize() to wrap up adding the new submodule and .gitmodules to the index to be ready to commit.

我的问题是,鉴于对git_submodule_add_setup的调用创建了一个初始化但空的子模块(其中带有.git目录),我如何正确执行上述提到的克隆操作?我已经尝试过使用git_submodule_open和git_clone C API进行克隆,但无济于事.克隆失败,因为子模块不是空文件夹.我已经阅读了git_clone C API文档,还熟悉了 https://libgit2.github.com/docs/guides/101-samples/#repositories_clone_simple 等,但我仍然没有一个明智的选择.我们将不胜感激.那些有识之士的帮助.

My question is how exactly do I perform the clone operation alluded to above given that the call to git_submodule_add_setup creates a initialised, but empty submodule (with a .git directory inside it)? I have already made several attempts to do the clone using the git_submodule_open and git_clone C APIs, but to no avail. The clone fails because the submodule is not an empty folder. I have read the git_clone C API documentation at and have also familiarised myself with the example code at https://libgit2.github.com/docs/guides/101-samples/#repositories_clone_simple etc., but I am still none the wiser. Some help from those in the the know would be much appreciated.

我在下面添加了一个代码段,以使我了解到目前为止的内容.

I have included a code snippet below to give a flavour of what I have so far.

namespace bfs = boost::filesystem;
git_repository * repo = NULL;
bfs::path const repo_path = bfs::current_path() / "repo";
std::string const repo_url = repo_path.string();
git_repository_init(&repo, repo_url.c_str(), 0);

//Add various files to repo index, and commit them...

git_repository * module = NULL;
bfs::path const module_path = bfs::current_path() / "module";
std::string const module_url = module_path.string();
git_repository_init(&module, module_url.c_str(), 0);

//Add various files to moduleindex, and commit them...

git_submodule * submodule = NULL;
git_submodule_add_setup(&submodule, repo, module_url.c_str(), "module", 0);

//What do I need to do in here to clone the module into position??

git_submodule_add_finalize(submodule);
git_submodule_free(submodule);

git_repository_free(module);
git_repository_free(repo);

推荐答案

我在添加子模块时也遇到了问题.这是我的 Github问题.

I've had my problems with adding submodules as well. Here is my Github issue.

幸运的是,有一位贡献者给了我这个工作的例子.

Fortunately, a contributer gave me this working example.

#include <git2.h>
#include <assert.h>
#include <stdio.h>

static int just_return_origin(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
{
    return git_remote_lookup(out, repo, name);
}

static int just_return_repo(git_repository **out, const char *path, int bare, void *payload)
{
    git_submodule *sm = payload;
    return git_submodule_open(out, sm);
}

int main(int argc, char *argv[])
{
    git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
    git_submodule *sm;
    git_repository *parent, *child;
    int result = 0;

    git_libgit2_init();

    result = git_repository_open(&parent, "parent");
    assert(!result);

    assert(!git_submodule_add_setup(&sm, parent, "file:///tmp/sm/child", "sm_test", 1));

    opts.repository_cb = just_return_repo;
    opts.repository_cb_payload = sm;
    opts.remote_cb = just_return_origin;
    opts.remote_cb_payload = sm;

    assert(!git_clone(&child, "file:///tmp/sm/child", "sm_test", &opts));
    assert(!git_submodule_add_finalize(sm));

    git_submodule_free(sm);
    git_repository_free(child);
    git_repository_free(parent);
    git_libgit2_shutdown();

    return 0;
}

也许这个例子对您也有帮助(即使这个问题很老).

Maybe this example helps you as well (even though this question is rather old).

这篇关于如何使用libgit2在代码中将git子模块添加到超级项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆