aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/git/git.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-06-19 19:43:17 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-06-20 19:52:50 +0200
commit99d2454c57e2b4761d667f265ec24122e40cd514 (patch)
tree912069507baa82d2651393fb371341cab4c68d01 /pkg/git/git.go
parent653ec8a64a80efcad9117609160cfababdf1e384 (diff)
pkg/git: improve Poll
Support changing repo/branch and force pushes.
Diffstat (limited to 'pkg/git/git.go')
-rw-r--r--pkg/git/git.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/pkg/git/git.go b/pkg/git/git.go
index fdea886cf..5c91c6040 100644
--- a/pkg/git/git.go
+++ b/pkg/git/git.go
@@ -22,16 +22,27 @@ func Poll(dir, repo, branch string) (string, error) {
osutil.RunCmd(timeout, dir, "git", "reset", "--hard")
origin, err := osutil.RunCmd(timeout, dir, "git", "remote", "get-url", "origin")
if err != nil || strings.TrimSpace(string(origin)) != repo {
+ // The repo is here, but it has wrong origin (e.g. repo in config has changed), re-clone.
+ if err := clone(dir, repo, branch); err != nil {
+ return "", err
+ }
+ }
+ // Use origin/branch for the case the branch was force-pushed,
+ // in such case branch is not the same is origin/branch and we will
+ // stuck with the local version forever (git checkout won't fail).
+ if _, err := osutil.RunCmd(timeout, dir, "git", "checkout", "origin/"+branch); err != nil {
+ // No such branch (e.g. branch in config has changed), re-clone.
if err := clone(dir, repo, branch); err != nil {
return "", err
}
}
if _, err := osutil.RunCmd(timeout, dir, "git", "fetch", "--no-tags", "--depth", "1"); err != nil {
+ // Something else is wrong, re-clone.
if err := clone(dir, repo, branch); err != nil {
return "", err
}
}
- if _, err := osutil.RunCmd(timeout, dir, "git", "checkout", branch); err != nil {
+ if _, err := osutil.RunCmd(timeout, dir, "git", "checkout", "origin/"+branch); err != nil {
return "", err
}
return HeadCommit(dir)