Skip to content

Commit

Permalink
Merge pull request hound-search#221 from etsy/force-pushes
Browse files Browse the repository at this point in the history
  • Loading branch information
stanistan committed Sep 16, 2016
2 parents ed547ac + d99d1db commit 5302e38
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package vcs

import (
"bytes"
"fmt"
"io"
"log"
"os/exec"
"path/filepath"
"strings"
)

const defaultRef = "master"

func init() {
Register(newGit, "git")
}
Expand Down Expand Up @@ -44,12 +47,37 @@ func (g *GitDriver) HeadRev(dir string) (string, error) {
return strings.TrimSpace(buf.String()), cmd.Wait()
}

func run(desc, dir, cmd string, args ...string) error {
c := exec.Command(cmd, args...)
c.Dir = dir
if out, err := c.CombinedOutput(); err != nil {
log.Printf(
"Failed to %s %s, see output below\n%sContinuing...",
desc,
dir,
out)
return err
}
return nil
}

func (g *GitDriver) Pull(dir string) (string, error) {
cmd := exec.Command("git", "pull")
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Failed to git pull %s, see output below\n%sContinuing...", dir, out)
if err := run("git fetch", dir,
"git",
"fetch",
"--prune",
"--no-tags",
"--depth", "1",
"origin",
fmt.Sprintf("+%s:remotes/origin/%s", defaultRef, defaultRef)); err != nil {
return "", err
}

if err := run("git reset", dir,
"git",
"reset",
"--hard",
fmt.Sprintf("origin/%s", defaultRef)); err != nil {
return "", err
}

Expand Down

0 comments on commit 5302e38

Please sign in to comment.