aboutsummaryrefslogtreecommitdiffstats
path: root/submodule.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-02-14 00:25:09 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2017-02-14 00:25:09 +0100
commit7e990a811d9e23b5a3573c405b70f06a1be9e7b6 (patch)
tree5cf329829edb41a562319f6242c13e90e350d365 /submodule.go
parent65351f835dcaa4b50dd44bce7bf3f2e31582dadc (diff)
downloadgo-git-7e990a811d9e23b5a3573c405b70f06a1be9e7b6.tar.gz
submodule init and update implementation
Diffstat (limited to 'submodule.go')
-rw-r--r--submodule.go68
1 files changed, 57 insertions, 11 deletions
diff --git a/submodule.go b/submodule.go
index ea099f0..83c28b7 100644
--- a/submodule.go
+++ b/submodule.go
@@ -1,33 +1,79 @@
package git
import (
- "fmt"
-
+ "srcd.works/go-git.v4/config"
"srcd.works/go-git.v4/plumbing"
)
+// Submodule a submodule allows you to keep another Git repository in a
+// subdirectory of your repository.
type Submodule struct {
- Name string
- Branch string
- URL string
-
+ m *config.Submodule
+ w *Worktree
+ // r is the submodule repository
r *Repository
}
+// Config returns the submodule config
+func (s *Submodule) Config() *config.Submodule {
+ return s.m
+}
+
+// Init initialize the submodule reading the recoreded Entry in the index for
+// the given submodule
func (s *Submodule) Init() error {
- return s.r.clone(&CloneOptions{
- URL: s.URL,
- ReferenceName: plumbing.ReferenceName(s.Branch),
+ e, err := s.w.readIndexEntry(s.m.Path)
+ if err != nil {
+ return err
+ }
+
+ _, err = s.r.CreateRemote(&config.RemoteConfig{
+ Name: DefaultRemoteName,
+ URL: s.m.URL,
})
+
+ if err != nil {
+ return err
+ }
+
+ return s.fetchAndCheckout(e.Hash)
+}
+
+// Update the registered submodule to match what the superproject expects
+func (s *Submodule) Update() error {
+ e, err := s.w.readIndexEntry(s.m.Path)
+ if err != nil {
+ return err
+ }
+
+ return s.fetchAndCheckout(e.Hash)
+}
+
+func (s *Submodule) fetchAndCheckout(hash plumbing.Hash) error {
+ if err := s.r.Fetch(&FetchOptions{}); err != nil && err != NoErrAlreadyUpToDate {
+ return err
+ }
+
+ w, err := s.r.Worktree()
+ if err != nil {
+ return err
+ }
+
+ if err := w.Checkout(hash); err != nil {
+ return err
+ }
+
+ head := plumbing.NewHashReference(plumbing.HEAD, hash)
+ return s.r.Storer.SetReference(head)
}
+// Submodules list of several submodules from the same repository
type Submodules []*Submodule
+// Init initialize the submodule recorded in the index
func (s Submodules) Init() error {
for _, sub := range s {
- fmt.Println("clone", sub.URL)
if err := sub.Init(); err != nil {
- fmt.Println(err)
return err
}
}