-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
203 lines (179 loc) · 4.36 KB
/
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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
type GitStatus struct {
branch string
ahead int
behind int
staged int
conflicts int
changed int
untracked int
// number of stashes created today
stashes int
// how many submodules deep we are
depth int
}
func queryGitStashCountToday() (result int) {
stashesRaw, _ := runCheck("git", "stash", "list", "--date=iso")
if stashesRaw == "" {
return 0
}
stashes := strings.Split(strings.TrimSpace(stashesRaw), "\n")
today := time.Now().Format("2006-01-02")
for _, s := range stashes { // filter for today's stashes
// extracts date out of the git stash output
// e.g.:
// stash@{2021-05-28 12:02:24 +0200}: WIP on master: 153225e add README
stashDate := s[7 : 7+10]
if today == stashDate {
result++
}
}
return
}
func fetchGitStatus(prehash string) *GitStatus {
var branch string
{ // get branch
retCode, branchRaw, _ := run("git", "symbolic-ref", "HEAD")
if retCode == 0 {
branch = branchRaw[11:]
branch = strings.TrimSpace(branch)
}
}
var changedFiles []byte
var stagedFiles []byte
{
_, res, err := run("git", "diff", "--name-status")
if strings.Contains(err, "fatal") {
os.Exit(0)
}
changedFiles = make([]byte, 0)
for _, line := range strings.Split(res, "\n") {
if len(line) > 0 {
changedFiles = append(changedFiles, line[0])
}
}
res, _ = runCheck("git", "diff", "--staged", "--name-status")
stagedFiles = make([]byte, 0)
for _, line := range strings.Split(res, "\n") {
if len(line) > 0 {
stagedFiles = append(stagedFiles, line[0])
}
}
}
nbChanged := len(changedFiles) - strings.Count(string(changedFiles), "U")
nbU := strings.Count(string(stagedFiles), "U")
nbStaged := len(stagedFiles) - nbU
conflicts := nbU
changed := nbChanged
var nbUntracked int
{
res, _ := runCheck("git", "status", "--porcelain")
for _, status := range strings.Split(res, "\n") {
if strings.HasPrefix(status, "??") {
nbUntracked++
}
}
}
ahead := 0
behind := 0
if branch == "" {
res, _ := runCheck("git", "rev-parse", "--short", "HEAD")
branch = prehash + res[:len(res)-1]
} else {
_, remoteNameRaw, _ := run("git", "config", fmt.Sprintf("branch.%s.remote", branch))
remoteName := strings.TrimSpace(remoteNameRaw)
if remoteName != "" {
mergeNameRaw, _ := runCheck("git", "config", fmt.Sprintf("branch.%s.merge", branch))
mergeName := strings.TrimSpace(mergeNameRaw)
var remoteRef string
if remoteName == "." {
remoteRef = mergeName
} else {
remoteRef = fmt.Sprintf("refs/remotes/%s/%s", remoteName, mergeName[11:])
}
retCode, revList, _ := run("git", "rev-list", "--left-right", fmt.Sprintf("%s...HEAD", remoteRef))
if retCode != 0 {
revList, _ = runCheck("git", "rev-list", "--left-right", fmt.Sprintf("%s...HEAD", mergeName))
}
if len(revList) > 0 {
behead := strings.Split(strings.TrimSpace(revList), "\n")
ahead = 0
for _, v := range behead {
if len(v) < 1 {
continue
}
if v[0] == '>' {
ahead++
}
}
behind = len(behead) - ahead
}
}
}
depth := 0
determineDepth := func() {
currentPath, err := os.Getwd()
if err != nil {
return
}
currentPath = filepath.Join(currentPath, "doesnt_matter")
gitdirSet := map[string]bool{}
gitdir := ""
for true {
// we reached root ?
currentPath = filepath.Dir(currentPath)
if currentPath == "/" {
break
}
// "realpath" $currentPath/.git
gitdir, err = filepath.EvalSymlinks(filepath.Join(currentPath, ".git"))
if err != nil {
continue
}
gitdir, err = filepath.Abs(gitdir)
if err != nil {
continue
}
// isdir? isfile?
if fi, err := os.Stat(gitdir); err != nil {
continue
} else if fi.IsDir() {
gitdirSet[gitdir] = true
} else {
bytes, err := ioutil.ReadFile(gitdir)
if err != nil {
continue
}
contents := string(bytes)
if strings.HasPrefix((contents), "gitdir: ") {
gitdirSet[filepath.Clean(filepath.Join(currentPath, contents[8:]))] = true
} else {
continue
}
}
}
for _, _ = range gitdirSet {
depth++
}
}
determineDepth()
return &GitStatus{
ahead: ahead,
behind: behind,
branch: branch,
changed: changed,
conflicts: conflicts,
depth: depth,
staged: nbStaged,
stashes: queryGitStashCountToday(),
untracked: nbUntracked,
}
}