-
Notifications
You must be signed in to change notification settings - Fork 0
/
info_status.go
93 lines (80 loc) · 1.94 KB
/
info_status.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
package gitw
import (
"regexp"
"strings"
"github.com/gookit/goutil/strutil"
)
// StatusPattern string. eg: master...origin/master
const StatusPattern = `^([\w-]+)...([\w-]+)/(\w[\w/-]+)$`
var statusRegex = regexp.MustCompile(StatusPattern)
// StatusInfo struct
//
// by run: git status -bs -u
type StatusInfo struct {
// Branch current branch name.
Branch string
// UpRemote current upstream remote name.
UpRemote string
// UpBranch current upstream remote branch name.
UpBranch string
fileNum int
// Deleted files
Deleted []string
// Renamed files, contains RM(rename and modify) files
Renamed []string
// Modified files
Modified []string
// Unstacked new created files.
Unstacked []string
}
// NewStatusInfo from string.
func NewStatusInfo(str string) *StatusInfo {
si := &StatusInfo{}
return si.FromString(str)
}
// FromString parse and load info
func (si *StatusInfo) FromString(str string) *StatusInfo {
return si.FromLines(strings.Split(str, "\n"))
}
// FromLines parse and load info
func (si *StatusInfo) FromLines(lines []string) *StatusInfo {
for _, line := range lines {
line = strings.Trim(line, " \t")
if len(line) == 0 {
continue
}
// files
mark, value := strutil.MustCut(line, " ")
switch mark {
case "##":
ss := statusRegex.FindStringSubmatch(value)
if len(ss) > 1 {
si.Branch, si.UpRemote, si.UpBranch = ss[1], ss[2], ss[3]
}
case "D":
si.fileNum++
si.Deleted = append(si.Deleted, value)
case "R":
si.fileNum++
si.Renamed = append(si.Renamed, value)
case "M":
si.fileNum++
si.Modified = append(si.Modified, value)
case "RM": // rename and modify
si.fileNum++
si.Renamed = append(si.Renamed, value)
case "??":
si.fileNum++
si.Unstacked = append(si.Unstacked, value)
}
}
return si
}
// FileNum in git status
func (si *StatusInfo) FileNum() int {
return si.fileNum
}
// IsCleaned status in workspace
func (si *StatusInfo) IsCleaned() bool {
return si.fileNum == 0
}