-
Notifications
You must be signed in to change notification settings - Fork 48
/
git.go
75 lines (59 loc) · 1.89 KB
/
git.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package captain // import "github.com/harbur/captain"
import (
"regexp"
"strings"
)
func getRevision(long_sha bool) string {
params := []string{"rev-parse"}
if !long_sha {
params = append(params, "--short")
}
params = append(params, "HEAD")
res, _ := oneliner("git", params...)
return res
}
func getBranches(all_branches bool) []string {
// Labels (branches + tags)
var labels = []string{}
branches_str, _ := oneliner("git", "name-rev", "--name-only", "--exclude=tags/*", "HEAD")
if all_branches {
branches_str, _ = oneliner("git", "branch", "--no-column", "--contains", "HEAD")
}
var branches = make([]string, 5)
if branches_str != "" {
// Remove asterisk from branches list
r := regexp.MustCompile("[\\* ] ")
branches_str = r.ReplaceAllString(branches_str, "")
branches = strings.Split(branches_str, "\n")
// Branches list is separated by spaces. Let's put it in an array
labels = append(labels, branches...)
}
tags_str, _ := oneliner("git", "tag", "--points-at", "HEAD")
if tags_str != "" {
tags := strings.Split(tags_str, "\n")
debug("Active branches %s and tags %s", branches, tags)
// Git tag list is separated by multi-lines. Let's put it in an array
labels = append(labels, tags...)
}
for key := range labels {
// Remove start of "heads/origin" if exist
r := regexp.MustCompile("^heads\\/origin\\/")
labels[key] = r.ReplaceAllString(labels[key], "")
// Remove start of "remotes/origin" if exist
r = regexp.MustCompile("^remotes\\/origin\\/")
labels[key] = r.ReplaceAllString(labels[key], "")
// Replace all "/" with "."
labels[key] = strings.Replace(labels[key], "/", ".", -1)
// Replace all "~" with "."
labels[key] = strings.Replace(labels[key], "~", ".", -1)
}
return labels
}
func isDirty() bool {
res, _ := oneliner("git", "status", "--porcelain")
return len(res) > 0
}
func isGit() bool {
res := getRevision(false)
return len(res) > 0
}