From 99d2454c57e2b4761d667f265ec24122e40cd514 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Mon, 19 Jun 2017 19:43:17 +0200 Subject: pkg/git: improve Poll Support changing repo/branch and force pushes. --- pkg/git/git.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'pkg/git/git.go') 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) -- cgit mrf-deployment