2016-06-30 2 views
2

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을 참조하십시오.libgit2를 사용하여 코드에서 수퍼 프로젝트에 git 하위 모듈을 추가하는 방법

git_submodule_add_setup : 이가 인출 및 서브 모듈 내용의 체크 아웃에 "자식 서브 모듈 추가"않습니다

문서는이 다음 말했다. 새로운 서브 모듈을 준비하고 .gitmodules에 엔트리를 만들고 작업 디렉토리의 주어진 경로 또는 작업 디렉토리에서 새로운 저장소로의 git 링크를 가진 .git/modules에 빈 초기화 된 저장소를 만듭니다.

"git submodule add"를 완전히 에뮬레이션하려면이 함수를 호출 한 다음 submodule repo를 열고 필요에 따라 복제 단계를 수행하십시오.. 마지막으로 git_submodule_add_finalize()를 호출하여 새 하위 모듈을 추가하고 .git 모듈을 인덱스에 추가하여 커밋 준비를 완료합니다.

제 질문은 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 등의 예제 코드에 익숙해졌지만 여전히 현명한 사람은 아닙니다. 알고있는 사람들의 도움을 많이 주시면 감사하겠습니다.

나는 지금까지 가지고있는 것의 맛을 내기 위해 아래 코드 스 니펫을 포함 시켰습니다.

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

답변이 잘못되었습니다. ( –

답변

0

하위 모듈 추가시에도 문제가있었습니다. 여기 내 Github issue입니다.

다행히도 기여자가이 작업 예제를 나에게주었습니다.

#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; 
} 

이 예는 사용자에게 도움이 될지도 모릅니다.

관련 문제