]> xenbits.xensource.com Git - people/gdunlap/xsatool/commitdiff
Implement 'repo update'
authorGeorge Dunlap <george.dunlap@citrix.com>
Wed, 7 Jun 2017 14:07:49 +0000 (15:07 +0100)
committerGeorge Dunlap <george.dunlap@citrix.com>
Wed, 7 Jun 2017 14:07:49 +0000 (15:07 +0100)
Implement Repo.Branches() with a pattern.  Add code to handle empty
output and 'stray' values.

Implement MainRepoUpdate, which will call 'git fetch' and 'git merge'
on master and stable-* branches.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
git.go
main.go
repo.go

diff --git a/git.go b/git.go
index e7b5bc7c0de5d2a49b55298ee4619add7db94831..fa6827a5f9688d1b1c290d8dae6f20b70beb0138 100644 (file)
--- a/git.go
+++ b/git.go
@@ -82,6 +82,18 @@ func (r Repo) Fetch() (out []byte, err error) {
        return
 }
 
+func (r Repo) MergeDefault(branch string) (out []byte, err error) {
+       out, err = r.gitCmd("checkout", branch)
+
+       if err != nil {
+               return
+       }
+
+       out, err = r.gitCmd("merge", "--ff-only")
+
+       return
+}
+
 func (r Repo) GetPath() string {
        return r.Path
 }
@@ -166,6 +178,19 @@ func (r Repo) Am(amglob string) (out []byte, err error) {
        return
 }
 
+// Parse output into one string per line
+func outputToList(out []byte) (list []string) {
+       if len(out) > 0 {
+               list = strings.Split(string(out), "\n")
+               
+               l := len(list)
+               if l > 0 && list[l-1] == "" {
+                       list = list[:l-1]
+               }
+       }
+       return
+}
+
 func (r Repo) Tags() (tags []string, err error) {
        out, err := r.gitCmd("tag")
 
@@ -173,7 +198,8 @@ func (r Repo) Tags() (tags []string, err error) {
                return nil, err
        }
 
-       tags = strings.Split(string(out), "\n")
+       tags = outputToList(out)
+       
        return
 }
 
@@ -183,6 +209,30 @@ func (r Repo) DeleteTag(tag string) (err error) {
        return
 }
 
+func (r Repo) Branches(pattern string) (branches []string, err error) {
+       var out []byte
+       if pattern == "" {
+               out, err = r.gitCmd("branch")
+       } else {
+               out, err = r.gitCmd("branch", "--list", pattern)
+       }
+
+       if err != nil {
+               return nil, err
+       }
+
+       branches = outputToList(out)
+
+       // Strip the first two bytes from the string
+       for i := range branches {
+               fmt.Printf("Parsing branch %d '%s'\n", i, branches[i])
+               branches[i] = (branches[i])[2:]
+       }
+
+       return
+}
+
+
 // Do a 'git am', and if it fails, clean up the tree
 func (r Repo) AmClean(amglob string) (out []byte, err error) {
        files, err := globToFullPath(amglob)
diff --git a/main.go b/main.go
index 3bc516776eac04fb3d27e2f2665a979aa3ffb60f..99a540bc5e387d003197ea66bfdecc68ae510aa3 100644 (file)
--- a/main.go
+++ b/main.go
@@ -126,6 +126,9 @@ func XsaMain(args []string) int {
                switch cmd {
                case "init":
                        main = MainRepoInit
+               case "update":
+                       main = MainRepoUpdate
+                       loadConfig = true
                case "x":
                        main = MainRepoX
                        loadConfig = true
diff --git a/repo.go b/repo.go
index 95dcc0cd8f6a9c3ce29e2d57683ca89dffeb9760..d6e7392c4ce954004eb75f5e0f2b74f5b1c505d8 100644 (file)
--- a/repo.go
+++ b/repo.go
@@ -143,6 +143,38 @@ func MainRepoInit(unused *XSAMeta, args []string) (ret int) {
        return 0
 }
 
+func MainRepoUpdate(unused *XSAMeta, args []string) (ret int) {
+       pull := func(xr *XenRepo, tree Tree) (err error) {
+               // git fetch
+               if _, err = xr.Fetch(); err != nil {
+                       err = fmt.Errorf("Fetching for tree %v: %v\n",
+                               tree, err)
+                       return
+               }
+
+               // for master, stable-* branches: 'git merge'
+               branches, err := xr.Branches("stable-*")
+
+               branches = append([]string{"master"}, branches...)
+               
+               for _, branch := range branches {
+                       if _, err = xr.MergeDefault(branch) ; err != nil {
+                               err = fmt.Errorf("Merging branch %s on tree %v: %v\n",
+                                       branch, tree, err)
+                       }
+               }
+
+               return
+       }
+       
+       if err := ForEachXenRepo(pull); err != nil {
+               fmt.Printf("Trying to update trees: %v\n", err)
+               return 1
+       }
+       
+       return 0
+}
+
 func MainRepoInfo(unused *XSAMeta, args []string) (ret int) {
        fmt.Printf("[Tool]\n")
        fmt.Printf("rootDir: %s\n", G.config.Tool.rootDir)