forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-git.go
215 lines (186 loc) · 5.58 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
package main
import (
"fmt"
pwl "github.com/justjanne/powerline-go/powerline"
"os"
"os/exec"
"path"
"regexp"
"strconv"
"strings"
)
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 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.symbolTemplates.RepoAhead, p.theme.GitAheadFg, p.theme.GitAheadBg)...)
segments = append(segments, addRepoStatsSegment(r.behind, p.symbolTemplates.RepoBehind, p.theme.GitBehindFg, p.theme.GitBehindBg)...)
segments = append(segments, addRepoStatsSegment(r.staged, p.symbolTemplates.RepoStaged, p.theme.GitStagedFg, p.theme.GitStagedBg)...)
segments = append(segments, addRepoStatsSegment(r.notStaged, p.symbolTemplates.RepoNotStaged, p.theme.GitNotStagedFg, p.theme.GitNotStagedBg)...)
segments = append(segments, addRepoStatsSegment(r.untracked, p.symbolTemplates.RepoUntracked, p.theme.GitUntrackedFg, p.theme.GitUntrackedBg)...)
segments = append(segments, addRepoStatsSegment(r.conflicted, p.symbolTemplates.RepoConflicted, p.theme.GitConflictedFg, p.theme.GitConflictedBg)...)
segments = append(segments, addRepoStatsSegment(r.stashed, p.symbolTemplates.RepoStashed, p.theme.GitStashedFg, p.theme.GitStashedBg)...)
return
}
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
}
func gitProcessEnv() []string {
home, _ := os.LookupEnv("HOME")
path, _ := os.LookupEnv("PATH")
env := map[string]string{
"LANG": "C",
"HOME": 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", "rev-parse", "--short", "HEAD")
if err != nil {
out, err := runGitCommand("git", "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.symbolTemplates.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", "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{}
}
indexSize, err := indexSize(p.cwd)
args := []string{
"status", "--porcelain", "-b", "--ignore-submodules",
}
if *p.args.GitAssumeUnchangedSize > 0 && indexSize > (*p.args.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.symbolTemplates.RepoBranch) > 0 {
branch = fmt.Sprintf("%s %s", p.symbolTemplates.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
}
out, err = runGitCommand("git", "rev-list", "-g", "refs/stash")
if err == nil && len(out) > 0 {
stats.stashed = len(strings.Split(out, "\n")) - 1
}
segments := []pwl.Segment{{
Name: "git-branch",
Content: branch,
Foreground: foreground,
Background: background,
}}
segments = append(segments, stats.GitSegments(p)...)
return segments
}