Skip to content

Commit d99d1db

Browse files
committed
Use more robust git combination that is tolerant to force pushed branches.
1 parent ed547ac commit d99d1db

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

vcs/git.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package vcs
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"log"
78
"os/exec"
89
"path/filepath"
910
"strings"
1011
)
1112

13+
const defaultRef = "master"
14+
1215
func init() {
1316
Register(newGit, "git")
1417
}
@@ -44,12 +47,37 @@ func (g *GitDriver) HeadRev(dir string) (string, error) {
4447
return strings.TrimSpace(buf.String()), cmd.Wait()
4548
}
4649

50+
func run(desc, dir, cmd string, args ...string) error {
51+
c := exec.Command(cmd, args...)
52+
c.Dir = dir
53+
if out, err := c.CombinedOutput(); err != nil {
54+
log.Printf(
55+
"Failed to %s %s, see output below\n%sContinuing...",
56+
desc,
57+
dir,
58+
out)
59+
return err
60+
}
61+
return nil
62+
}
63+
4764
func (g *GitDriver) Pull(dir string) (string, error) {
48-
cmd := exec.Command("git", "pull")
49-
cmd.Dir = dir
50-
out, err := cmd.CombinedOutput()
51-
if err != nil {
52-
log.Printf("Failed to git pull %s, see output below\n%sContinuing...", dir, out)
65+
if err := run("git fetch", dir,
66+
"git",
67+
"fetch",
68+
"--prune",
69+
"--no-tags",
70+
"--depth", "1",
71+
"origin",
72+
fmt.Sprintf("+%s:remotes/origin/%s", defaultRef, defaultRef)); err != nil {
73+
return "", err
74+
}
75+
76+
if err := run("git reset", dir,
77+
"git",
78+
"reset",
79+
"--hard",
80+
fmt.Sprintf("origin/%s", defaultRef)); err != nil {
5381
return "", err
5482
}
5583

0 commit comments

Comments
 (0)