Skip to content

Instantly share code, notes, and snippets.

@campoy
Created February 21, 2018 19:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save campoy/5ffad70414afedc3b551b044f1bdc8d0 to your computer and use it in GitHub Desktop.
Save campoy/5ffad70414afedc3b551b044f1bdc8d0 to your computer and use it in GitHub Desktop.
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