diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-02-12 23:03:47 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-02-12 23:04:00 +0100 |
commit | b3b6e51565dcdbf81546f1d8f0121874a4e4fce9 (patch) | |
tree | 5ad244f4b9c350367ed65d57d8881078aefe0753 /worktree.go | |
parent | 498dbf7dc92e288641f1af1acc52704150e8a6c0 (diff) | |
download | go-git-b3b6e51565dcdbf81546f1d8f0121874a4e4fce9.tar.gz |
submodule init implementation
Diffstat (limited to 'worktree.go')
-rw-r--r-- | worktree.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/worktree.go b/worktree.go index 58e008e..8aee0dc 100644 --- a/worktree.go +++ b/worktree.go @@ -4,8 +4,10 @@ import ( "errors" "fmt" "io" + "io/ioutil" "os" + "srcd.works/go-git.v4/config" "srcd.works/go-git.v4/plumbing" "srcd.works/go-git.v4/plumbing/format/index" "srcd.works/go-git.v4/plumbing/object" @@ -104,6 +106,7 @@ func (w *Worktree) Status() (Status, error) { files, err := readDirAll(w.fs) if err != nil { + fmt.Println("ch", err) return nil, err } @@ -167,6 +170,61 @@ func (w *Worktree) getMode(fi billy.FileInfo) os.FileMode { return object.FileMode } +const gitmodulesFile = ".gitmodules" + +func (w *Worktree) Submodules() (Submodules, error) { + l := make(Submodules, 0) + m, err := w.readGitmodulesFile() + if err != nil || m == nil { + return l, err + } + + for _, c := range m.Submodules { + s, err := w.newSubmodule(c) + if err != nil { + return nil, err + } + + l = append(l, s) + } + + return l, nil +} + +func (w *Worktree) newSubmodule(m *config.Submodule) (*Submodule, error) { + s, err := w.r.Storer.Module(m.Name) + if err != nil { + return nil, err + } + + return &Submodule{ + Name: m.Name, + URL: m.URL, + + r: newRepository(s, w.fs.Dir(m.Path)), + }, nil +} + +func (w *Worktree) readGitmodulesFile() (*config.Modules, error) { + f, err := w.fs.Open(gitmodulesFile) + if err != nil { + if os.IsNotExist(err) { + return nil, nil + } + + return nil, err + } + + input, err := ioutil.ReadAll(f) + if err != nil { + return nil, err + } + + m := config.NewModules() + return m, m.Unmarshal(input) + +} + // Status current status of a Worktree type Status map[string]*FileStatus @@ -287,6 +345,10 @@ func doReadDirAll(fs billy.Filesystem, path string, files map[string]billy.FileI l, err := fs.ReadDir(path) if err != nil { + if os.IsNotExist(err) { + return nil + } + return err } |