-
-
Save roscopecoltran/055b7474678b711a0e07efc7cc27c6d3 to your computer and use it in GitHub Desktop.
Creating a submodule with go-git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"strings" | |
"time" | |
"gopkg.in/src-d/go-billy.v4/memfs" | |
git "gopkg.in/src-d/go-git.v4" | |
"gopkg.in/src-d/go-git.v4/plumbing/object" | |
"gopkg.in/src-d/go-git.v4/storage/memory" | |
) | |
func checkerr(err error) { | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func main() { | |
mem := make(memory.ModuleStorage) | |
s, err := mem.Module("foo") | |
checkerr(err) | |
fs := memfs.New() | |
f, err := fs.Create(".gitmodules") | |
checkerr(err) | |
fmt.Fprintln(f, strings.Join([]string{ | |
"[submodule \"go-git\"]", | |
"\tpath = include/go-git", | |
"\turl = [email protected]:src-d/go-git.git"}, "\n")) | |
checkerr(f.Close()) | |
repo, err := git.Init(s, fs) | |
checkerr(err) | |
wt, err := repo.Worktree() | |
_, err = wt.Add(".gitmodules") | |
checkerr(err) | |
_, err = wt.Commit("creating submodule", &git.CommitOptions{ | |
Author: &object.Signature{ | |
Name: "Francesc", | |
Email: "[email protected]", | |
When: time.Now(), | |
}, | |
}) | |
checkerr(err) | |
sms, err := wt.Submodules() | |
checkerr(err) | |
for _, sm := range sms { | |
cfg := sm.Config() | |
fmt.Println(cfg.Name, cfg.URL, cfg.Branch) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment