diff options
Diffstat (limited to 'pkg/git/git.go')
| -rw-r--r-- | pkg/git/git.go | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/pkg/git/git.go b/pkg/git/git.go index 8e492177a..fdea886cf 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -7,6 +7,7 @@ package git import ( "fmt" "os" + "strings" "time" "github.com/google/syzkaller/pkg/osutil" @@ -19,22 +20,14 @@ const timeout = time.Hour // timeout for all git invocations // Returns hash of the HEAD commit in the specified branch. func Poll(dir, repo, branch string) (string, error) { osutil.RunCmd(timeout, dir, "git", "reset", "--hard") - if _, err := osutil.RunCmd(timeout, dir, "git", "fetch", "--no-tags", "--depth", "1"); err != nil { - if err := os.RemoveAll(dir); err != nil { - return "", fmt.Errorf("failed to remove repo dir: %v", err) - } - if err := os.MkdirAll(dir, 0700); err != nil { - return "", fmt.Errorf("failed to create repo dir: %v", err) - } - args := []string{ - "clone", - repo, - "--depth", "1", - "--single-branch", - "--branch", branch, - dir, + origin, err := osutil.RunCmd(timeout, dir, "git", "remote", "get-url", "origin") + if err != nil || strings.TrimSpace(string(origin)) != repo { + if err := clone(dir, repo, branch); err != nil { + return "", err } - if _, err := osutil.RunCmd(timeout, "", "git", args...); err != nil { + } + if _, err := osutil.RunCmd(timeout, dir, "git", "fetch", "--no-tags", "--depth", "1"); err != nil { + if err := clone(dir, repo, branch); err != nil { return "", err } } @@ -44,6 +37,25 @@ func Poll(dir, repo, branch string) (string, error) { return HeadCommit(dir) } +func clone(dir, repo, branch string) error { + if err := os.RemoveAll(dir); err != nil { + return fmt.Errorf("failed to remove repo dir: %v", err) + } + if err := os.MkdirAll(dir, 0700); err != nil { + return fmt.Errorf("failed to create repo dir: %v", err) + } + args := []string{ + "clone", + repo, + "--depth", "1", + "--single-branch", + "--branch", branch, + dir, + } + _, err := osutil.RunCmd(timeout, "", "git", args...) + return err +} + // HeadCommit returns hash of the HEAD commit of the current branch of git repository in dir. func HeadCommit(dir string) (string, error) { output, err := osutil.RunCmd(timeout, dir, "git", "log", "--pretty=format:'%H'", "-n", "1") |
