-
Notifications
You must be signed in to change notification settings - Fork 7
/
porcelain.go
201 lines (180 loc) · 4.09 KB
/
porcelain.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
package porcelain
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"github.com/robertgzr/color"
)
type GitArea struct {
modified int
added int
deleted int
renamed int
copied int
}
func (a *GitArea) HasChanged() bool {
var changed bool
if a.added != 0 {
changed = true
}
if a.deleted != 0 {
changed = true
}
if a.modified != 0 {
changed = true
}
if a.copied != 0 {
changed = true
}
if a.renamed != 0 {
changed = true
}
return changed
}
type PorcInfo struct {
workingDir string
branch string
commit string
remote string
upstream string
ahead int
behind int
untracked int
unmerged int
Unstaged GitArea
Staged GitArea
}
func (pi *PorcInfo) HasUnmerged(cwd string) bool {
if pi.unmerged > 0 {
return true
}
gitDir, err := PathToGitDir(cwd)
if err != nil {
log.Printf("error calling PathToGitDir: %s", err)
return false
}
// TODO figure out if output of MERGE_HEAD can be useful
if _, err := ioutil.ReadFile(path.Join(gitDir, "MERGE_HEAD")); err != nil {
if os.IsNotExist(err) {
return false
}
log.Printf("error reading MERGE_HEAD: %s", err)
return false
} else {
return true
}
}
func (pi *PorcInfo) HasModified() bool {
return pi.Unstaged.HasChanged()
}
func (pi *PorcInfo) IsDirty() bool {
return pi.Staged.HasChanged()
}
func (pi *PorcInfo) Debug() string {
return fmt.Sprintf("%#+v", pi)
}
// Fmt formats the output for the shell
// TODO should be configurable by the user
//
func (pi *PorcInfo) Fmt(cwd string, noColorFlag, bashFmtFlag, zshFmtFlag, tmuxFmtFlag bool) string {
log.Printf("formatting output: %s", pi.Debug())
var (
branchGlyph string = ""
modifiedGlyph string = "Δ"
// deletedGlyph string = "*"
dirtyGlyph string = "✘"
cleanGlyph string = "✔"
untrackedGlyph string = "?"
unmergedGlyph string = "‼"
aheadArrow string = "↑"
behindArrow string = "↓"
)
if noColorFlag {
color.NoColor = true
} else {
color.NoColor = false
color.EscapeBashPrompt = bashFmtFlag
color.EscapeZshPrompt = zshFmtFlag
color.TmuxMode = tmuxFmtFlag
}
branchFmt := color.New(color.FgBlue).SprintFunc()
commitFmt := color.New(color.FgGreen, color.Italic).SprintFunc()
aheadFmt := color.New(color.Faint, color.BgYellow, color.FgBlack).SprintFunc()
behindFmt := color.New(color.Faint, color.BgRed, color.FgWhite).SprintFunc()
modifiedFmt := color.New(color.FgBlue).SprintFunc()
// deletedFmt := color.New(color.FgYellow).SprintFunc()
dirtyFmt := color.New(color.FgRed).SprintFunc()
cleanFmt := color.New(color.FgGreen).SprintFunc()
untrackedFmt := color.New(color.Faint).SprintFunc()
unmergedFmt := color.New(color.FgCyan).SprintFunc()
return fmt.Sprintf("%s %s@%s %s %s %s",
branchGlyph,
branchFmt(pi.branch),
func() string {
if pi.commit == "(initial)" {
return commitFmt(pi.commit)
}
return commitFmt(pi.commit[:7])
}(),
func() string {
var buf bytes.Buffer
if pi.ahead > 0 {
buf.WriteString(aheadFmt(" ", aheadArrow, pi.ahead, " "))
}
if pi.behind > 0 {
buf.WriteString(behindFmt(" ", behindArrow, pi.behind, " "))
}
return buf.String()
}(),
func() string {
var buf bytes.Buffer
if pi.untracked > 0 {
buf.WriteString(untrackedFmt(untrackedGlyph))
} else {
buf.WriteRune(' ')
}
if pi.HasUnmerged(cwd) {
buf.WriteString(unmergedFmt(unmergedGlyph))
} else {
buf.WriteRune(' ')
}
if pi.HasModified() {
buf.WriteString(modifiedFmt(modifiedGlyph))
} else {
buf.WriteRune(' ')
}
// TODO star glyph
return buf.String()
}(),
// dirty/clean
func() string {
if pi.IsDirty() {
return dirtyFmt(dirtyGlyph)
} else {
return cleanFmt(cleanGlyph)
}
}(),
)
}
func Run(cwd string) *PorcInfo {
gitOut, err := GetGitOutput(cwd)
if err != nil {
log.Printf("error: %s", err)
if err == ErrNotAGitRepo {
os.Exit(0)
}
fmt.Printf("error: %s", err)
os.Exit(1)
}
var porcInfo = new(PorcInfo)
porcInfo.workingDir = cwd
if err := porcInfo.ParsePorcInfo(gitOut); err != nil {
log.Printf("error: %s", err)
fmt.Printf("error: %s", err)
os.Exit(1)
}
return porcInfo
}