2016-06-30 9 views
2

Ich habe erfolgreich zwei Git-Repositories (namens Repo und Modul) im Code mit der libgit2 C API erstellt und versuche nun das "Modul" hinzuzufügen Repository zum Repository "repo" als Submodul. Ich versuche, die libgit2 git_submodule_add_setup C API zu verwenden, um dies zu tun. Siehe https://libgit2.github.com/libgit2/#HEAD/group/submodule/git_submodule_add_setup.So fügen Sie ein git Submodul zu einem Superprojekt in code mit libgit2 hinzu

Die Dokumentation sagt der folgende:

git_submodule_add_setup: Dies bedeutet „git Submodul hinzufügen“ bis zum Abruf und Check-out der Submodul Inhalte. Es erstellt ein neues Submodul, erstellt einen Eintrag in .gitmodules und erstellt ein leeres initialisiertes Repository entweder am angegebenen Pfad im Arbeitsverzeichnis oder in .git/modules mit einem Gitlink vom Arbeitsverzeichnis zum neuen Repo.

Um "git submodule add" vollständig zu emulieren, rufen Sie diese Funktion auf, öffnen Sie dann das Submodul Repo und führen Sie den Klonschritt nach Bedarf durch. Zuletzt rufe git_submodule_add_finalize() auf, um das neue Submodul und die .gitmodules dem Index hinzuzufügen, damit sie bereit sind, zu committen.

Meine Frage ist, wie genau führe ich den oben genannten Klon-Vorgang vor, da der Aufruf von git_submodule_add_setup ein initialisiertes, aber leeres Submodul (mit einem .git-Verzeichnis darin) erzeugt? Ich habe bereits mehrere Versuche unternommen, den Klon mit den APIs git_submodule_open und git_clone C zu machen, aber ohne Erfolg. Der Klon schlägt fehl, weil das Submodul kein leerer Ordner ist. Ich habe die git_clone C API Dokumentation gelesen und habe mich auch mit dem Beispielcode unter https://libgit2.github.com/docs/guides/101-samples/#repositories_clone_simple etc. vertraut gemacht, aber mir ist noch keiner klüger. Einige Hilfe von denen, die in der wissen, würde sehr geschätzt werden.

Ich habe unten ein Code-Snippet eingefügt, um einen Eindruck von dem zu geben, was ich bisher habe.

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

Ich brauche die Antwort schlecht; ( –

Antwort

0

Ich hatte meine Probleme mit dem Hinzufügen von Submodulen. Hier ist meine Github issue.

Glücklicherweise gab mir ein Mitarbeiter dieses Arbeitsbeispiel.

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

Vielleicht hilft Ihnen auch dieses Beispiel (obwohl diese Frage eher alt ist).

Verwandte Themen