From 64f77af5b6177ccfb73d01f1980e8d24784ada1f Mon Sep 17 00:00:00 2001 From: mpl Date: Fri, 25 May 2018 02:54:15 +0200 Subject: [PATCH] devcam: review: fix origin in git config Fixes #1176 Change-Id: I18222fd7b2d9ac47d4ab01740ef55b14d44b8b11 --- dev/devcam/review.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/dev/devcam/review.go b/dev/devcam/review.go index a2f766fd8..239cd58a0 100644 --- a/dev/devcam/review.go +++ b/dev/devcam/review.go @@ -26,6 +26,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "perkeep.org/pkg/cmdmain" ) @@ -33,6 +34,7 @@ import ( var ( defaultHook = filepath.FromSlash("misc/commit-msg.githook") hookFile = filepath.FromSlash(".git/hooks/commit-msg") + configFile = filepath.FromSlash(".git/config") ) type reviewCmd struct { @@ -61,6 +63,7 @@ func (c *reviewCmd) RunCommand(args []string) error { } goToCamliRoot() c.checkHook() + checkOrigin() c.gitPush() return nil } @@ -117,6 +120,42 @@ func (c *reviewCmd) checkHook() { } } +const newOrigin = "https://perkeep.googlesource.com/perkeep" + +var ( + newFetch = regexp.MustCompile(`.*Fetch\s+URL:\s+` + newOrigin + `.*`) + newPush = regexp.MustCompile(`.*Push\s+URL:\s+` + newOrigin + `.*`) +) + +func checkOrigin() { + out, err := exec.Command("git", "remote", "show", "origin").CombinedOutput() + if err != nil { + log.Fatalf("%v, %s", err, out) + } + + if !newPush.Match(out) { + setPushOrigin() + } + + if !newFetch.Match(out) { + setFetchOrigin() + } +} + +func setPushOrigin() { + out, err := exec.Command("git", "remote", "set-url", "--push", "origin", newOrigin).CombinedOutput() + if err != nil { + log.Fatal("%v, %s", err, out) + } +} + +func setFetchOrigin() { + out, err := exec.Command("git", "remote", "set-url", "origin", newOrigin).CombinedOutput() + if err != nil { + log.Fatal("%v, %s", err, out) + } +} + func (c *reviewCmd) gitPush() { args := []string{"push", "origin"} if c.releaseBranch != "" {