/
utils.go
208 lines (167 loc) · 4.14 KB
/
utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"github.com/reconquest/hierr-go"
)
func printUntracked(path path, postfix ...string) {
if len(postfix) > 0 {
fmt.Printf("%s %s %s\n", untrackedMarker, path, yellow(postfix[0]))
} else {
fmt.Printf("%s %s\n", untrackedMarker, path)
}
}
func printEmpty(path path, postfix ...string) {
if len(postfix) > 0 {
fmt.Printf("%s %s %s\n", emptyMarker, path, blue(postfix[0]))
} else {
fmt.Printf("%s %s\n", emptyMarker, path)
}
}
func printDirty(path path, postfix ...string) {
if len(postfix) > 0 {
fmt.Printf("%s %s %s\n", dirtyMarker, path, red(postfix[0]))
} else {
fmt.Printf("%s %s\n", dirtyMarker, path)
}
}
func printClean(path path, onlyDirty bool, postfix ...string) {
if onlyDirty {
return
}
if len(postfix) > 0 {
fmt.Printf("%s %s %s\n", cleanMarker, path, green(postfix[0]))
} else {
fmt.Printf("%s %s\n", cleanMarker, path)
}
}
func printUnpushed(path path, postfix ...string) {
if len(postfix) > 0 {
fmt.Printf("%s %s %s\n", unpushedMarker, path, yellow(postfix[0]))
} else {
fmt.Printf("%s %s\n", unpushedMarker, path)
}
}
func getCurrentBranch(dir string, quiet bool) (string, error) {
out, err := execute(dir, quiet, "branch", "--points-at", "HEAD")
if err != nil {
return "", err
}
branches := strings.Split(out, "\n")
for _, branch := range branches {
if branch[0] == '*' {
branch = strings.TrimLeft(branch, "* ")
branch = strings.TrimRight(branch, "\n")
return strings.TrimSpace(branch), nil
}
}
return "", errors.New("can't find current branch")
}
func isEmpty(dir string, quiet bool) (bool, error) {
out, err := execute(dir, quiet, "branch", "-lr")
if err != nil {
return false, err
}
if len(out) == 0 {
return true, nil
}
return false, nil
}
func isUntracked(dir string, quiet bool) (bool, error) {
out, err := execute(dir, quiet, "status", "--porcelain", "-b")
if err != nil {
return false, err
}
untrackedRegexp := regexp.MustCompile(`^\?\?\s`)
for _, row := range strings.Split(out, "\n") {
if untrackedRegexp.MatchString(row) {
return true, nil
}
}
return false, nil
}
func isTracked(dir string, quiet bool) (bool, error) {
out, err := execute(dir, quiet, "status", "--porcelain", "-b")
if err != nil {
return false, err
}
branchInfo := strings.Split(out, "\n")[0]
trackedReg := regexp.MustCompile(`## [^.]+\.\.\.[^/]+/.+`)
return trackedReg.MatchString(branchInfo), nil
}
func isDetachedHead(dir string, quiet bool) (bool, error) {
branch, err := getCurrentBranch(dir, quiet)
if err != nil {
return false, err
}
return strings.Contains(branch, "detached"), nil
}
func lastCommitDate(dir string, quiet bool) (time.Time, error) {
out, err := execute(dir, quiet, "log", "-1", "--format=%ct")
if err != nil {
return time.Time{}, err
}
out = strings.TrimSpace(out)
//out = strings.Replace(out, "+", "Z", -1)
//out = out[:len(out)-7]
timestamp, err := strconv.ParseInt(out, 10, 64)
if err != nil {
return time.Time{}, err
}
date := time.Unix(timestamp, 0)
return date, nil
}
func isDirty(dir string, quiet bool) (bool, error) {
out, err := execute(dir, quiet, "status", "--porcelain")
if err != nil {
return false, err
}
return out != "", nil
}
func isPushed(dir string, quiet bool) (bool, error) {
isStray, err := isStrayBranch(dir)
if err != nil {
return false, err
}
if isStray {
return false, nil
}
out, err := execute(dir, quiet, "log", "@{push}..")
if err != nil {
return false, err
}
return out == "", nil
}
func isStrayBranch(dir string) (bool, error) {
cmd := exec.Command("git", "rev-parse", "-q", "--verify", "@{push}")
cmd.Dir = dir
err := cmd.Run()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
return true, nil
}
return false, err
}
return false, nil
}
func execute(dir string, quiet bool, commandLine ...string) (string, error) {
cmd := exec.Command("git", commandLine...)
cmd.Dir = dir
if !quiet {
cmd.Stderr = os.Stderr
}
out, err := cmd.Output()
if err != nil {
return "", hierr.Errorf(
err, "can't run 'git %s' in '%s'",
strings.Join(commandLine, " "), dir,
)
}
return string(out), nil
}