forked from marklap/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-git.go
284 lines (248 loc) · 7.35 KB
/
segment-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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"fmt"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
pwl "github.com/justjanne/powerline-go/powerline"
)
type repoStats struct {
ahead int
behind int
untracked int
notStaged int
staged int
conflicted int
stashed int
}
func (r repoStats) dirty() bool {
return r.untracked+r.notStaged+r.staged+r.conflicted > 0
}
func (r repoStats) any() bool {
return r.ahead+r.behind+r.untracked+r.notStaged+r.staged+r.conflicted+r.stashed > 0
}
func addRepoStatsSegment(nChanges int, symbol string, foreground uint8, background uint8) []pwl.Segment {
if nChanges > 0 {
return []pwl.Segment{{
Name: "git-status",
Content: fmt.Sprintf("%d%s", nChanges, symbol),
Foreground: foreground,
Background: background,
}}
}
return []pwl.Segment{}
}
func (r repoStats) GitSegments(p *powerline) (segments []pwl.Segment) {
segments = append(segments, addRepoStatsSegment(r.ahead, p.symbols.RepoAhead, p.theme.GitAheadFg, p.theme.GitAheadBg)...)
segments = append(segments, addRepoStatsSegment(r.behind, p.symbols.RepoBehind, p.theme.GitBehindFg, p.theme.GitBehindBg)...)
segments = append(segments, addRepoStatsSegment(r.staged, p.symbols.RepoStaged, p.theme.GitStagedFg, p.theme.GitStagedBg)...)
segments = append(segments, addRepoStatsSegment(r.notStaged, p.symbols.RepoNotStaged, p.theme.GitNotStagedFg, p.theme.GitNotStagedBg)...)
segments = append(segments, addRepoStatsSegment(r.untracked, p.symbols.RepoUntracked, p.theme.GitUntrackedFg, p.theme.GitUntrackedBg)...)
segments = append(segments, addRepoStatsSegment(r.conflicted, p.symbols.RepoConflicted, p.theme.GitConflictedFg, p.theme.GitConflictedBg)...)
segments = append(segments, addRepoStatsSegment(r.stashed, p.symbols.RepoStashed, p.theme.GitStashedFg, p.theme.GitStashedBg)...)
return
}
func addRepoStatsSymbol(nChanges int, symbol string, GitMode string) string {
if nChanges > 0 {
if GitMode == "simple" {
return symbol
} else if GitMode == "compact" {
return fmt.Sprintf(" %d%s", nChanges, symbol )
} else {
return symbol
}
}
return ""
}
func (r repoStats) GitSymbols(p *powerline) string {
var info string
info += addRepoStatsSymbol(r.ahead, p.symbols.RepoAhead, p.cfg.GitMode)
info += addRepoStatsSymbol(r.behind, p.symbols.RepoBehind, p.cfg.GitMode)
info += addRepoStatsSymbol(r.staged, p.symbols.RepoStaged, p.cfg.GitMode)
info += addRepoStatsSymbol(r.notStaged, p.symbols.RepoNotStaged, p.cfg.GitMode)
info += addRepoStatsSymbol(r.untracked, p.symbols.RepoUntracked, p.cfg.GitMode)
info += addRepoStatsSymbol(r.conflicted, p.symbols.RepoConflicted, p.cfg.GitMode)
info += addRepoStatsSymbol(r.stashed, p.symbols.RepoStashed, p.cfg.GitMode)
return info
}
var branchRegex = regexp.MustCompile(`^## (?P<local>\S+?)(\.{3}(?P<remote>\S+?)( \[(ahead (?P<ahead>\d+)(, )?)?(behind (?P<behind>\d+))?])?)?$`)
func groupDict(pattern *regexp.Regexp, haystack string) map[string]string {
match := pattern.FindStringSubmatch(haystack)
result := make(map[string]string)
if len(match) > 0 {
for i, name := range pattern.SubexpNames() {
if i != 0 {
result[name] = match[i]
}
}
}
return result
}
var gitProcessEnv = func() []string {
homeEnv := homeEnvName()
home, _ := os.LookupEnv(homeEnv)
path, _ := os.LookupEnv("PATH")
env := map[string]string{
"LANG": "C",
homeEnv: home,
"PATH": path,
}
result := make([]string, 0)
for key, value := range env {
result = append(result, fmt.Sprintf("%s=%s", key, value))
}
return result
}()
func runGitCommand(cmd string, args ...string) (string, error) {
command := exec.Command(cmd, args...)
command.Env = gitProcessEnv
out, err := command.Output()
return string(out), err
}
func parseGitBranchInfo(status []string) map[string]string {
return groupDict(branchRegex, status[0])
}
func getGitDetachedBranch(p *powerline) string {
out, err := runGitCommand("git", "--no-optional-locks", "rev-parse", "--short", "HEAD")
if err != nil {
out, err := runGitCommand("git", "--no-optional-locks", "symbolic-ref", "--short", "HEAD")
if err != nil {
return "Error"
}
return strings.SplitN(out, "\n", 2)[0]
}
detachedRef := strings.SplitN(out, "\n", 2)
return fmt.Sprintf("%s %s", p.symbols.RepoDetached, detachedRef[0])
}
func parseGitStats(status []string) repoStats {
stats := repoStats{}
if len(status) > 1 {
for _, line := range status[1:] {
if len(line) > 2 {
code := line[:2]
switch code {
case "??":
stats.untracked++
case "DD", "AU", "UD", "UA", "DU", "AA", "UU":
stats.conflicted++
default:
if code[0] != ' ' {
stats.staged++
}
if code[1] != ' ' {
stats.notStaged++
}
}
}
}
}
return stats
}
func repoRoot(path string) (string, error) {
out, err := runGitCommand("git", "--no-optional-locks", "rev-parse", "--show-toplevel")
if err != nil {
return "", err
}
return strings.TrimSpace(out), nil
}
func indexSize(root string) (int64, error) {
fileInfo, err := os.Stat(path.Join(root, ".git", "index"))
if err != nil {
return 0, err
}
return fileInfo.Size(), nil
}
func segmentGit(p *powerline) []pwl.Segment {
repoRoot, err := repoRoot(p.cwd)
if err != nil {
return []pwl.Segment{}
}
if len(p.ignoreRepos) > 0 && p.ignoreRepos[repoRoot] {
return []pwl.Segment{}
}
args := []string{
"--no-optional-locks", "status", "--porcelain", "-b", "--ignore-submodules",
}
if p.cfg.GitAssumeUnchangedSize > 0 {
indexSize, _ := indexSize(p.cwd)
if indexSize > (p.cfg.GitAssumeUnchangedSize * 1024) {
args = append(args, "-uno")
}
}
out, err := runGitCommand("git", args...)
if err != nil {
return []pwl.Segment{}
}
status := strings.Split(out, "\n")
stats := parseGitStats(status)
branchInfo := parseGitBranchInfo(status)
var branch string
if branchInfo["local"] != "" {
ahead, _ := strconv.ParseInt(branchInfo["ahead"], 10, 32)
stats.ahead = int(ahead)
behind, _ := strconv.ParseInt(branchInfo["behind"], 10, 32)
stats.behind = int(behind)
branch = branchInfo["local"]
} else {
branch = getGitDetachedBranch(p)
}
if len(p.symbols.RepoBranch) > 0 {
branch = fmt.Sprintf("%s %s", p.symbols.RepoBranch, branch)
}
var foreground, background uint8
if stats.dirty() {
foreground = p.theme.RepoDirtyFg
background = p.theme.RepoDirtyBg
} else {
foreground = p.theme.RepoCleanFg
background = p.theme.RepoCleanBg
}
segments := []pwl.Segment{{
Name: "git-branch",
Content: branch,
Foreground: foreground,
Background: background,
}}
stashEnabled := true
for _, stat := range p.cfg.GitDisableStats {
// "ahead, behind, staged, notStaged, untracked, conflicted, stashed"
switch stat {
case "ahead":
stats.ahead = 0
case "behind":
stats.behind = 0
case "staged":
stats.staged = 0
case "notStaged":
stats.notStaged = 0
case "untracked":
stats.untracked = 0
case "conflicted":
stats.conflicted = 0
case "stashed":
stats.stashed = 0
stashEnabled = false
}
}
if stashEnabled {
out, err = runGitCommand("git", "--no-optional-locks", "rev-list", "-g", "refs/stash")
if err == nil {
stats.stashed = strings.Count(out, "\n")
}
}
if p.cfg.GitMode == "simple" {
if stats.any() {
segments[0].Content += " " + stats.GitSymbols(p)
}
} else if p.cfg.GitMode == "compact" {
if stats.any() {
segments[0].Content += stats.GitSymbols(p)
}
} else { // fancy
segments = append(segments, stats.GitSegments(p)...)
}
return segments
}