]> xenbits.xensource.com Git - people/gdunlap/xsatool.git/commitdiff
git: Use full path for AM operations
authorGeorge Dunlap <george.dunlap@citrix.com>
Wed, 17 May 2017 15:17:17 +0000 (16:17 +0100)
committerGeorge Dunlap <george.dunlap@citrix.com>
Wed, 17 May 2017 15:20:07 +0000 (16:20 +0100)
...so that Repo.Am() works regardless of your cwd.

Factor "normalization" into normalizePath.

Implement globToFullPath, which will take a glob and return a slice of
normalized filenames.

Use globToFullPath() for Repo.Am() and Repo.AmClean().

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

diff --git a/git.go b/git.go
index 845d153c65bab238c00ccfa051f2dbaf86d581ac..b015c75dd85f4a26010b4265ea711daf13d6f740 100644 (file)
--- a/git.go
+++ b/git.go
@@ -13,16 +13,20 @@ type Repo struct {
        Path string
 }
 
-func GitOpen(gpath string) (r Repo, err error) {
+func normalizePath(gpath string) string {
        if !path.IsAbs(gpath) {
                cwd, err := os.Getwd()
                if err != nil {
-                       fmt.Printf("Getwd failed?\n")
-                       return r, err
+                       panic("Getwd failed?\n")
                }
                gpath = cwd + "/" + gpath
                gpath = path.Clean(gpath)
        }
+       return gpath
+}
+
+func GitOpen(gpath string) (r Repo, err error) {
+       gpath = normalizePath(gpath)
 
        fmt.Printf("Path: %s\n", gpath)
        r.Path = gpath
@@ -124,8 +128,19 @@ func (r Repo) FormatPatch(path string, baseline string, branch string) (out []by
        return
 }
 
+func globToFullPath(glob string) (files []string, err error) {
+       files, err = filepath.Glob(glob)
+       if err != nil {
+               return
+       }
+       for i := range files {
+               files[i] = normalizePath(files[i])
+       }
+       return
+}
+
 func (r Repo) Am(amglob string) (out []byte, err error) {
-       files, err := filepath.Glob(amglob)
+       files, err := globToFullPath(amglob)
        if err != nil {
                return
        }
@@ -133,6 +148,7 @@ func (r Repo) Am(amglob string) (out []byte, err error) {
                err = fmt.Errorf("%s didn't match any files", amglob)
                return
        }
+
        out, err = r.gitCmd(append([]string{"am", "--reject"}, files...)...)
 
        return
@@ -157,7 +173,7 @@ func (r Repo) DeleteTag(tag string) (err error) {
 
 // Do a 'git am', and if it fails, clean up the tree
 func (r Repo) AmClean(amglob string) (out []byte, err error) {
-       files, err := filepath.Glob(amglob)
+       files, err := globToFullPath(amglob)
        if err != nil {
                return
        }