Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating a submodule with go-git
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 = git@github.com: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: "francesc@sourced.tech",
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