-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Git VCS driver: Support non-default target refs #345
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ | |
/node_modules | ||
.DS_Store | ||
*.exe | ||
/db | ||
/data |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
@@ -34,4 +35,21 @@ func TestExampleConfigsAreValid(t *testing.T) { | |
t.Fatal(err) | ||
} | ||
} | ||
|
||
// Ensure that global VCS config vals are merged | ||
repo := cfg.Repos["SomeGitRepo"] | ||
vcsConfigBytes := repo.VcsConfig() | ||
var vcsConfigVals map[string]interface{} | ||
json.Unmarshal(vcsConfigBytes, &vcsConfigVals) | ||
if detectRef, ok := vcsConfigVals["detect-ref"]; !ok || !detectRef.(bool) { | ||
t.Error("global detectRef vcs config setting not set for repo") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm the |
||
} | ||
|
||
repo = cfg.Repos["GitRepoWithDetectRefDisabled"] | ||
vcsConfigBytes = repo.VcsConfig() | ||
json.Unmarshal(vcsConfigBytes, &vcsConfigVals) | ||
if detectRef, ok := vcsConfigVals["detect-ref"]; !ok || detectRef.(bool) { | ||
t.Error("global detectRef vcs config setting not overriden by repo-level setting") | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,24 +2,49 @@ package vcs | |
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const defaultRef = "master" | ||
|
||
var headBranchRegexp = regexp.MustCompile(`HEAD branch: (?P<branch>.+)`) | ||
|
||
func init() { | ||
Register(newGit, "git") | ||
} | ||
|
||
type GitDriver struct{} | ||
type GitDriver struct { | ||
DetectRef bool `json:"detect-ref"` | ||
Ref string `json:"ref"` | ||
refDetetector refDetetector | ||
} | ||
|
||
type refDetetector interface { | ||
detectRef(dir string) string | ||
} | ||
|
||
type headBranchDetector struct { | ||
} | ||
|
||
func newGit(b []byte) (Driver, error) { | ||
return &GitDriver{}, nil | ||
var d GitDriver | ||
|
||
if b != nil { | ||
if err := json.Unmarshal(b, &d); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
d.refDetetector = &headBranchDetector{} | ||
|
||
return &d, nil | ||
} | ||
|
||
func (g *GitDriver) HeadRev(dir string) (string, error) { | ||
|
@@ -47,43 +72,61 @@ func (g *GitDriver) HeadRev(dir string) (string, error) { | |
return strings.TrimSpace(buf.String()), cmd.Wait() | ||
} | ||
|
||
func run(desc, dir, cmd string, args ...string) error { | ||
func run(desc, dir, cmd string, args ...string) (string, error) { | ||
c := exec.Command(cmd, args...) | ||
c.Dir = dir | ||
if out, err := c.CombinedOutput(); err != nil { | ||
out, err := c.CombinedOutput() | ||
if err != nil { | ||
log.Printf( | ||
"Failed to %s %s, see output below\n%sContinuing...", | ||
desc, | ||
dir, | ||
out) | ||
return err | ||
} | ||
return nil | ||
|
||
return string(out), nil | ||
} | ||
|
||
func (g *GitDriver) Pull(dir string) (string, error) { | ||
if err := run("git fetch", dir, | ||
targetRef := g.targetRef(dir) | ||
|
||
if _, err := run("git fetch", dir, | ||
"git", | ||
"fetch", | ||
"--prune", | ||
"--no-tags", | ||
"--depth", "1", | ||
"origin", | ||
fmt.Sprintf("+%s:remotes/origin/%s", defaultRef, defaultRef)); err != nil { | ||
fmt.Sprintf("+%s:remotes/origin/%s", targetRef, targetRef)); err != nil { | ||
return "", err | ||
} | ||
|
||
if err := run("git reset", dir, | ||
if _, err := run("git reset", dir, | ||
"git", | ||
"reset", | ||
"--hard", | ||
fmt.Sprintf("origin/%s", defaultRef)); err != nil { | ||
fmt.Sprintf("origin/%s", targetRef)); err != nil { | ||
return "", err | ||
} | ||
|
||
return g.HeadRev(dir) | ||
} | ||
|
||
func (g *GitDriver) targetRef(dir string) string { | ||
var targetRef string | ||
if g.Ref != "" { | ||
targetRef = g.Ref | ||
} else if g.DetectRef { | ||
targetRef = g.refDetetector.detectRef(dir) | ||
} | ||
|
||
if targetRef == "" { | ||
targetRef = defaultRef | ||
} | ||
|
||
return targetRef | ||
} | ||
|
||
func (g *GitDriver) Clone(dir, url string) (string, error) { | ||
par, rep := filepath.Split(dir) | ||
cmd := exec.Command( | ||
|
@@ -99,11 +142,42 @@ func (g *GitDriver) Clone(dir, url string) (string, error) { | |
return "", err | ||
} | ||
|
||
return g.HeadRev(dir) | ||
return g.Pull(dir) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you give me more details on what this additional There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it, much appreciated. |
||
} | ||
|
||
func (g *GitDriver) SpecialFiles() []string { | ||
return []string{ | ||
".git", | ||
} | ||
} | ||
|
||
func (d *headBranchDetector) detectRef(dir string) string { | ||
output, err := run("git show remote info", dir, | ||
"git", | ||
"remote", | ||
"show", | ||
"origin", | ||
) | ||
|
||
if err != nil { | ||
log.Printf( | ||
"error occured when fetching info to determine target ref in %s: %s. Will fall back to default ref %s", | ||
dir, | ||
err, | ||
defaultRef, | ||
) | ||
return "" | ||
} | ||
|
||
matches := headBranchRegexp.FindStringSubmatch(output) | ||
if len(matches) != 2 { | ||
log.Printf( | ||
"could not determine target ref in %s. Will fall back to default ref %s", | ||
dir, | ||
defaultRef, | ||
) | ||
return "" | ||
} | ||
|
||
return matches[1] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config.json
could probably use some more complete documentation generally, but just for the sake of completeness, could you add an example of using a customref
somewhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do - thanks!