-
Notifications
You must be signed in to change notification settings - Fork 0
/
guard.go
62 lines (54 loc) · 1.37 KB
/
guard.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
package main
import (
"fmt"
"strings"
)
type guardResult struct {
uncommittedChanges string
untrackedFiles string
skipped bool
}
func (g *guardResult) Message() string {
var r string
if len(g.uncommittedChanges) > 0 && len(g.untrackedFiles) > 0 {
r = "There are uncommitted changes and untracked files"
} else if len(g.untrackedFiles) > 0 {
r = "There are untracked files"
} else {
r = "There are uncommitted changes"
}
if g.skipped {
r += " but guard was skipped by options"
}
return r
}
func (g *guardResult) Format() string {
parts := []string{g.Message()}
if len(g.uncommittedChanges) > 0 {
parts = append(parts, fmt.Sprintf("Uncommitted changes:\n%s", g.uncommittedChanges))
}
if len(g.untrackedFiles) > 0 {
parts = append(parts, fmt.Sprintf("Untracked files:\n%s", g.untrackedFiles))
}
return strings.Join(parts, "\n\n")
}
func guard(opts *Options) (*guardResult, error) {
diff, err := uncommittedChanges()
if err != nil {
return nil, err
}
untrackedFiles, err := untrackedFiles()
if err != nil {
return nil, err
}
if len(diff) == 0 && len(untrackedFiles) == 0 {
return nil, nil
}
return &guardResult{
uncommittedChanges: diff,
untrackedFiles: untrackedFiles,
skipped: opts.SkipGuard ||
(opts.SkipGuardUncommittedChanges && len(diff) > 0) ||
(opts.SkipGuardUntrackedFiles && len(untrackedFiles) > 0),
}, nil
}