Skip to content

Commit 484bd87

Browse files
committed
Support git tag
1 parent d7074c4 commit 484bd87

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

code/git.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8+
"regexp"
89
"strings"
910

1011
log "github.com/Sirupsen/logrus"
@@ -16,6 +17,8 @@ type Git struct {
1617
ref string
1718
}
1819

20+
var isTag = regexp.MustCompile("^refs/tags/")
21+
1922
func (g *Git) checkUpdate() (bool, error) {
2023
out, err := execPipeline(
2124
g.path,
@@ -40,7 +43,11 @@ func (g *Git) checkUpdate() (bool, error) {
4043
return false, err
4144
}
4245

43-
cmd = exec.Command("git", "diff", fmt.Sprintf("origin/%s", g.ref))
46+
if isTag.MatchString(g.ref) {
47+
cmd = exec.Command("git", "diff", g.ref)
48+
} else {
49+
cmd = exec.Command("git", "diff", fmt.Sprintf("origin/%s", g.ref))
50+
}
4451
cmd.Dir = g.path
4552

4653
out, err = cmd.Output()
@@ -57,12 +64,29 @@ func (g *Git) checkUpdate() (bool, error) {
5764
}
5865

5966
func (g *Git) get() error {
60-
log.Infof("Executing git clone -b %s %s %s", g.ref, g.url, g.path)
61-
out, err := exec.Command("git", "clone", "-b", g.ref, g.url, g.path).CombinedOutput()
62-
if err != nil {
63-
log.Error(string(out))
67+
if isTag.MatchString(g.ref) {
68+
log.Infof("Executing git clone %s %s", g.url, g.path)
69+
out, err := exec.Command("git", "clone", g.url, g.path).CombinedOutput()
70+
if err != nil {
71+
log.Error(string(out))
72+
}
73+
74+
log.Infof("Executing git checkout %s ", g.ref)
75+
cmd := exec.Command("git", "checkout", g.ref)
76+
cmd.Dir = g.path
77+
err = cmd.Run()
78+
if err != nil {
79+
return err
80+
}
81+
return err
82+
} else {
83+
log.Infof("Executing git clone -b %s %s %s", g.ref, g.url, g.path)
84+
out, err := exec.Command("git", "clone", "-b", g.ref, g.url, g.path).CombinedOutput()
85+
if err != nil {
86+
log.Error(string(out))
87+
}
88+
return err
6489
}
65-
return err
6690
}
6791

6892
func execPipeline(dir string, commands ...[]string) ([]byte, error) {

0 commit comments

Comments
 (0)