@@ -25,13 +25,17 @@ const (
25
25
type Pipe struct {
26
26
fromPos int
27
27
toPos int
28
- fromHash string
29
- toHash string
28
+ fromHash * string
29
+ toHash * string
30
30
kind PipeKind
31
31
style style.TextStyle
32
32
}
33
33
34
- var highlightStyle = style .FgLightWhite .SetBold ()
34
+ var (
35
+ highlightStyle = style .FgLightWhite .SetBold ()
36
+ EmptyTreeCommitHash = models .EmptyTreeCommitHash
37
+ StartCommitHash = "START"
38
+ )
35
39
36
40
func (self Pipe ) left () int {
37
41
return min (self .fromPos , self .toPos )
@@ -41,13 +45,13 @@ func (self Pipe) right() int {
41
45
return max (self .fromPos , self .toPos )
42
46
}
43
47
44
- func RenderCommitGraph (commits []* models.Commit , selectedCommitHash string , getStyle func (c * models.Commit ) style.TextStyle ) []string {
48
+ func RenderCommitGraph (commits []* models.Commit , selectedCommitHashPtr * string , getStyle func (c * models.Commit ) style.TextStyle ) []string {
45
49
pipeSets := GetPipeSets (commits , getStyle )
46
50
if len (pipeSets ) == 0 {
47
51
return nil
48
52
}
49
53
50
- lines := RenderAux (pipeSets , commits , selectedCommitHash )
54
+ lines := RenderAux (pipeSets , commits , selectedCommitHashPtr )
51
55
52
56
return lines
53
57
}
@@ -57,15 +61,15 @@ func GetPipeSets(commits []*models.Commit, getStyle func(c *models.Commit) style
57
61
return nil
58
62
}
59
63
60
- pipes := []* Pipe {{fromPos : 0 , toPos : 0 , fromHash : "START" , toHash : commits [0 ].Hash (), kind : STARTS , style : style .FgDefault }}
64
+ pipes := []* Pipe {{fromPos : 0 , toPos : 0 , fromHash : & StartCommitHash , toHash : commits [0 ].HashPtr (), kind : STARTS , style : style .FgDefault }}
61
65
62
66
return lo .Map (commits , func (commit * models.Commit , _ int ) []* Pipe {
63
67
pipes = getNextPipes (pipes , commit , getStyle )
64
68
return pipes
65
69
})
66
70
}
67
71
68
- func RenderAux (pipeSets [][]* Pipe , commits []* models.Commit , selectedCommitHash string ) []string {
72
+ func RenderAux (pipeSets [][]* Pipe , commits []* models.Commit , selectedCommitHashPtr * string ) []string {
69
73
maxProcs := runtime .GOMAXPROCS (0 )
70
74
71
75
// splitting up the rendering of the graph into multiple goroutines allows us to render the graph in parallel
@@ -89,7 +93,7 @@ func RenderAux(pipeSets [][]*Pipe, commits []*models.Commit, selectedCommitHash
89
93
if k > 0 {
90
94
prevCommit = commits [k - 1 ]
91
95
}
92
- line := renderPipeSet (pipeSet , selectedCommitHash , prevCommit )
96
+ line := renderPipeSet (pipeSet , selectedCommitHashPtr , prevCommit )
93
97
innerLines = append (innerLines , line )
94
98
}
95
99
chunks [i ] = innerLines
@@ -116,12 +120,12 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
116
120
return pipe .kind != TERMINATES
117
121
})
118
122
119
- newPipes := make ([]* Pipe , 0 , len (currentPipes )+ len (commit .Parents ()))
123
+ newPipes := make ([]* Pipe , 0 , len (currentPipes )+ len (commit .ParentPtrs ()))
120
124
// start by assuming that we've got a brand new commit not related to any preceding commit.
121
125
// (this only happens when we're doing `git log --all`). These will be tacked onto the far end.
122
126
pos := maxPos + 1
123
127
for _ , pipe := range currentPipes {
124
- if equalHashes (pipe .toHash , commit .Hash ()) {
128
+ if equalHashes (pipe .toHash , commit .HashPtr ()) {
125
129
// turns out this commit does have a descendant so we'll place it right under the first instance
126
130
pos = pipe .toPos
127
131
break
@@ -133,24 +137,24 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
133
137
// a traversed spot is one where a current pipe is starting on, ending on, or passing through
134
138
traversedSpots := set .New [int ]()
135
139
136
- var toHash string
140
+ var toHash * string
137
141
if commit .IsFirstCommit () {
138
- toHash = models . EmptyTreeCommitHash
142
+ toHash = & EmptyTreeCommitHash
139
143
} else {
140
- toHash = commit .Parents ()[0 ]
144
+ toHash = commit .ParentPtrs ()[0 ]
141
145
}
142
146
newPipes = append (newPipes , & Pipe {
143
147
fromPos : pos ,
144
148
toPos : pos ,
145
- fromHash : commit .Hash (),
149
+ fromHash : commit .HashPtr (),
146
150
toHash : toHash ,
147
151
kind : STARTS ,
148
152
style : getStyle (commit ),
149
153
})
150
154
151
155
traversedSpotsForContinuingPipes := set .New [int ]()
152
156
for _ , pipe := range currentPipes {
153
- if ! equalHashes (pipe .toHash , commit .Hash ()) {
157
+ if ! equalHashes (pipe .toHash , commit .HashPtr ()) {
154
158
traversedSpotsForContinuingPipes .Add (pipe .toPos )
155
159
}
156
160
}
@@ -189,7 +193,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
189
193
}
190
194
191
195
for _ , pipe := range currentPipes {
192
- if equalHashes (pipe .toHash , commit .Hash ()) {
196
+ if equalHashes (pipe .toHash , commit .HashPtr ()) {
193
197
// terminating here
194
198
newPipes = append (newPipes , & Pipe {
195
199
fromPos : pipe .toPos ,
@@ -216,13 +220,13 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
216
220
}
217
221
218
222
if commit .IsMerge () {
219
- for _ , parent := range commit .Parents ()[1 :] {
223
+ for _ , parent := range commit .ParentPtrs ()[1 :] {
220
224
availablePos := getNextAvailablePosForNewPipe ()
221
225
// need to act as if continuing pipes are going to continue on the same line.
222
226
newPipes = append (newPipes , & Pipe {
223
227
fromPos : pos ,
224
228
toPos : availablePos ,
225
- fromHash : commit .Hash (),
229
+ fromHash : commit .HashPtr (),
226
230
toHash : parent ,
227
231
kind : STARTS ,
228
232
style : getStyle (commit ),
@@ -233,7 +237,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
233
237
}
234
238
235
239
for _ , pipe := range currentPipes {
236
- if ! equalHashes (pipe .toHash , commit .Hash ()) && pipe .toPos > pos {
240
+ if ! equalHashes (pipe .toHash , commit .HashPtr ()) && pipe .toPos > pos {
237
241
// continuing on, potentially moving left to fill in a blank spot
238
242
last := pipe .toPos
239
243
for i := pipe .toPos ; i > pos ; i -- {
@@ -268,7 +272,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
268
272
269
273
func renderPipeSet (
270
274
pipes []* Pipe ,
271
- selectedCommitHash string ,
275
+ selectedCommitHashPtr * string ,
272
276
prevCommit * models.Commit ,
273
277
) string {
274
278
maxPos := 0
@@ -315,10 +319,10 @@ func renderPipeSet(
315
319
// we don't want to highlight two commits if they're contiguous. We only want
316
320
// to highlight multiple things if there's an actual visible pipe involved.
317
321
highlight := true
318
- if prevCommit != nil && equalHashes (prevCommit .Hash (), selectedCommitHash ) {
322
+ if prevCommit != nil && equalHashes (prevCommit .HashPtr (), selectedCommitHashPtr ) {
319
323
highlight = false
320
324
for _ , pipe := range pipes {
321
- if equalHashes (pipe .fromHash , selectedCommitHash ) && (pipe .kind != TERMINATES || pipe .fromPos != pipe .toPos ) {
325
+ if equalHashes (pipe .fromHash , selectedCommitHashPtr ) && (pipe .kind != TERMINATES || pipe .fromPos != pipe .toPos ) {
322
326
highlight = true
323
327
}
324
328
}
@@ -327,7 +331,7 @@ func renderPipeSet(
327
331
// so we have our commit pos again, now it's time to build the cells.
328
332
// we'll handle the one that's sourced from our selected commit last so that it can override the other cells.
329
333
selectedPipes , nonSelectedPipes := utils .Partition (pipes , func (pipe * Pipe ) bool {
330
- return highlight && equalHashes (pipe .fromHash , selectedCommitHash )
334
+ return highlight && equalHashes (pipe .fromHash , selectedCommitHashPtr )
331
335
})
332
336
333
337
for _ , pipe := range nonSelectedPipes {
@@ -370,13 +374,13 @@ func renderPipeSet(
370
374
return writer .String ()
371
375
}
372
376
373
- func equalHashes (a , b string ) bool {
374
- // if our selectedCommitHash is an empty string we treat that as meaning there is no selected commit hash
375
- if a == "" || b == "" {
377
+ func equalHashes (a , b * string ) bool {
378
+ // if our selectedCommitHashPtr is nil, there is no selected commit
379
+ if a == nil || b == nil {
376
380
return false
377
381
}
378
382
379
- length := min (len (a ), len (b ))
383
+ length := min (len (* a ), len (* b ))
380
384
// parent hashes are only stored up to 20 characters for some reason so we'll truncate to that for comparison
381
- return a [:length ] == b [:length ]
385
+ return ( * a ) [:length ] == ( * b ) [:length ]
382
386
}
0 commit comments