forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercurial.go
169 lines (134 loc) · 3.47 KB
/
mercurial.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
package segments
import (
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
const (
MERCURIALCOMMAND = "hg"
hgLogTemplate = "{rev}|{node}|{branch}|{tags}|{bookmarks}"
)
type MercurialStatus struct {
ScmStatus
}
func (s *MercurialStatus) add(code string) {
switch code {
case "R", "!":
s.Deleted++
case "A":
s.Added++
case "?":
s.Untracked++
case "M":
s.Modified++
}
}
type Mercurial struct {
scm
Working *MercurialStatus
IsTip bool
LocalCommitNumber string
ChangeSetID string
ChangeSetIDShort string
Branch string
Bookmarks []string
Tags []string
}
func (hg *Mercurial) Template() string {
return "hg {{.Branch}} {{if .LocalCommitNumber}}({{.LocalCommitNumber}}:{{.ChangeSetIDShort}}){{end}}{{range .Bookmarks }} \uf02e {{.}}{{end}}{{range .Tags}} \uf02b {{.}}{{end}}{{if .Working.Changed}} \uf044 {{ .Working.String }}{{ end }}" //nolint: lll
}
func (hg *Mercurial) Enabled() bool {
if !hg.shouldDisplay() {
return false
}
statusFormats := hg.props.GetKeyValueMap(StatusFormats, map[string]string{})
hg.Working = &MercurialStatus{ScmStatus: ScmStatus{Formats: statusFormats}}
displayStatus := hg.props.GetBool(FetchStatus, false)
if displayStatus {
hg.setMercurialStatus()
}
return true
}
func (hg *Mercurial) shouldDisplay() bool {
if !hg.hasCommand(MERCURIALCOMMAND) {
return false
}
hgdir, err := hg.env.HasParentFilePath(".hg", false)
if err != nil {
return false
}
if hg.shouldIgnoreRootRepository(hgdir.ParentFolder) {
return false
}
hg.setDir(hgdir.ParentFolder)
hg.workingDir = hgdir.Path
hg.rootDir = hgdir.Path
// convert the worktree file path to a windows one when in a WSL shared folder
hg.realDir = strings.TrimSuffix(hg.convertToWindowsPath(hgdir.Path), "/.hg")
return true
}
func (hg *Mercurial) setDir(dir string) {
dir = runtime.ReplaceHomeDirPrefixWithTilde(hg.env, dir) // align with template PWD
if hg.env.GOOS() == runtime.WINDOWS {
hg.Dir = strings.TrimSuffix(dir, `\.hg`)
return
}
hg.Dir = strings.TrimSuffix(dir, "/.hg")
}
func (hg *Mercurial) setMercurialStatus() {
hg.Branch = hg.command
idString := hg.getHgCommandOutput("log", "-r", ".", "--template", hgLogTemplate)
if len(idString) == 0 {
return
}
idSplit := strings.Split(idString, "|")
if len(idSplit) != 5 {
return
}
hg.LocalCommitNumber = idSplit[0]
hg.ChangeSetID = idSplit[1]
if len(hg.ChangeSetID) >= 12 {
hg.ChangeSetIDShort = hg.ChangeSetID[:12]
}
hg.Branch = idSplit[2]
hg.Tags = doSplit(idSplit[3])
hg.Bookmarks = doSplit(idSplit[4])
hg.IsTip = false
tipIndex := 0
for i, tag := range hg.Tags {
if tag == "tip" {
hg.IsTip = true
tipIndex = i
break
}
}
if hg.IsTip {
hg.Tags = RemoveAtIndex(hg.Tags, tipIndex)
}
statusString := hg.getHgCommandOutput("status")
if len(statusString) == 0 {
return
}
statusLines := strings.Split(statusString, "\n")
for _, status := range statusLines {
hg.Working.add(status[:1])
}
}
func doSplit(s string) []string {
if len(s) == 0 {
return []string{}
}
return strings.Split(s, " ")
}
func RemoveAtIndex(s []string, index int) []string {
ret := make([]string, 0)
ret = append(ret, s[:index]...)
return append(ret, s[index+1:]...)
}
func (hg *Mercurial) getHgCommandOutput(command string, args ...string) string {
args = append([]string{"-R", hg.realDir, command}, args...)
val, err := hg.env.RunCommand(hg.command, args...)
if err != nil {
return ""
}
return strings.TrimSpace(val)
}