Skip to content

Commit 13c2136

Browse files
committed
Store fromHash/toHash in Pipe struct as pointers
Now that commit hashes are stored in a pool and referenced by pointer by the commits, we can use those same pointers in the pipes.
1 parent 8d834e2 commit 13c2136

File tree

7 files changed

+126
-112
lines changed

7 files changed

+126
-112
lines changed

pkg/commands/models/commit.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ func (c *Commit) Hash() string {
9393
return *c.hash
9494
}
9595

96+
func (c *Commit) HashPtr() *string {
97+
return c.hash
98+
}
99+
96100
func (c *Commit) ShortHash() string {
97101
return utils.ShortHash(c.Hash())
98102
}
@@ -120,6 +124,10 @@ func (c *Commit) Parents() []string {
120124
return lo.Map(c.parents, func(s *string, _ int) string { return *s })
121125
}
122126

127+
func (c *Commit) ParentPtrs() []*string {
128+
return c.parents
129+
}
130+
123131
func (c *Commit) IsFirstCommit() bool {
124132
return len(c.parents) == 0
125133
}

pkg/gui/context/local_commits_context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
3232
)
3333

3434
getDisplayStrings := func(startIdx int, endIdx int) [][]string {
35-
selectedCommitHash := ""
35+
var selectedCommitHashPtr *string
3636

3737
if c.Context().Current().GetKey() == LOCAL_COMMITS_CONTEXT_KEY {
3838
selectedCommit := viewModel.GetSelected()
3939
if selectedCommit != nil {
40-
selectedCommitHash = selectedCommit.Hash()
40+
selectedCommitHashPtr = selectedCommit.HashPtr()
4141
}
4242
}
4343

@@ -57,7 +57,7 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
5757
c.UserConfig().Gui.ShortTimeFormat,
5858
time.Now(),
5959
c.UserConfig().Git.ParseEmoji,
60-
selectedCommitHash,
60+
selectedCommitHashPtr,
6161
startIdx,
6262
endIdx,
6363
shouldShowGraph(c),

pkg/gui/context/sub_commits_context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ func NewSubCommitsContext(
4646
return [][]string{}
4747
}
4848

49-
selectedCommitHash := ""
49+
var selectedCommitHashPtr *string
5050
if c.Context().Current().GetKey() == SUB_COMMITS_CONTEXT_KEY {
5151
selectedCommit := viewModel.GetSelected()
5252
if selectedCommit != nil {
53-
selectedCommitHash = selectedCommit.Hash()
53+
selectedCommitHashPtr = selectedCommit.HashPtr()
5454
}
5555
}
5656
branches := []*models.Branch{}
@@ -72,7 +72,7 @@ func NewSubCommitsContext(
7272
c.UserConfig().Gui.ShortTimeFormat,
7373
time.Now(),
7474
c.UserConfig().Git.ParseEmoji,
75-
selectedCommitHash,
75+
selectedCommitHashPtr,
7676
startIdx,
7777
endIdx,
7878
shouldShowGraph(c),

pkg/gui/presentation/commits.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func GetCommitListDisplayStrings(
5151
shortTimeFormat string,
5252
now time.Time,
5353
parseEmoji bool,
54-
selectedCommitHash string,
54+
selectedCommitHashPtr *string,
5555
startIdx int,
5656
endIdx int,
5757
showGraph bool,
@@ -102,7 +102,7 @@ func GetCommitListDisplayStrings(
102102
graphLines := graph.RenderAux(
103103
graphPipeSets,
104104
graphCommits,
105-
selectedCommitHash,
105+
selectedCommitHashPtr,
106106
)
107107
allGraphLines = append(allGraphLines, graphLines...)
108108
}
@@ -119,7 +119,7 @@ func GetCommitListDisplayStrings(
119119
graphLines := graph.RenderAux(
120120
graphPipeSets,
121121
graphCommits,
122-
selectedCommitHash,
122+
selectedCommitHashPtr,
123123
)
124124
allGraphLines = append(allGraphLines, graphLines...)
125125
}
@@ -140,7 +140,7 @@ func GetCommitListDisplayStrings(
140140
graphLines := graph.RenderAux(
141141
graphPipeSets,
142142
graphCommits,
143-
selectedCommitHash,
143+
selectedCommitHashPtr,
144144
)
145145
getGraphLine = func(idx int) string {
146146
if idx >= graphOffset {

pkg/gui/presentation/commits_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
3636
shortTimeFormat string
3737
now time.Time
3838
parseEmoji bool
39-
selectedCommitHash string
39+
selectedCommitHashPtr *string
4040
startIdx int
4141
endIdx int
4242
showGraph bool
@@ -563,7 +563,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) {
563563
s.shortTimeFormat,
564564
s.now,
565565
s.parseEmoji,
566-
s.selectedCommitHash,
566+
s.selectedCommitHashPtr,
567567
s.startIdx,
568568
s.endIdx,
569569
s.showGraph,

pkg/gui/presentation/graph/graph.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ const (
2525
type Pipe struct {
2626
fromPos int
2727
toPos int
28-
fromHash string
29-
toHash string
28+
fromHash *string
29+
toHash *string
3030
kind PipeKind
3131
style style.TextStyle
3232
}
3333

34-
var highlightStyle = style.FgLightWhite.SetBold()
34+
var (
35+
highlightStyle = style.FgLightWhite.SetBold()
36+
EmptyTreeCommitHash = models.EmptyTreeCommitHash
37+
StartCommitHash = "START"
38+
)
3539

3640
func (self Pipe) left() int {
3741
return min(self.fromPos, self.toPos)
@@ -41,13 +45,13 @@ func (self Pipe) right() int {
4145
return max(self.fromPos, self.toPos)
4246
}
4347

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 {
4549
pipeSets := GetPipeSets(commits, getStyle)
4650
if len(pipeSets) == 0 {
4751
return nil
4852
}
4953

50-
lines := RenderAux(pipeSets, commits, selectedCommitHash)
54+
lines := RenderAux(pipeSets, commits, selectedCommitHashPtr)
5155

5256
return lines
5357
}
@@ -57,15 +61,15 @@ func GetPipeSets(commits []*models.Commit, getStyle func(c *models.Commit) style
5761
return nil
5862
}
5963

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}}
6165

6266
return lo.Map(commits, func(commit *models.Commit, _ int) []*Pipe {
6367
pipes = getNextPipes(pipes, commit, getStyle)
6468
return pipes
6569
})
6670
}
6771

68-
func RenderAux(pipeSets [][]*Pipe, commits []*models.Commit, selectedCommitHash string) []string {
72+
func RenderAux(pipeSets [][]*Pipe, commits []*models.Commit, selectedCommitHashPtr *string) []string {
6973
maxProcs := runtime.GOMAXPROCS(0)
7074

7175
// 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
8993
if k > 0 {
9094
prevCommit = commits[k-1]
9195
}
92-
line := renderPipeSet(pipeSet, selectedCommitHash, prevCommit)
96+
line := renderPipeSet(pipeSet, selectedCommitHashPtr, prevCommit)
9397
innerLines = append(innerLines, line)
9498
}
9599
chunks[i] = innerLines
@@ -116,12 +120,12 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
116120
return pipe.kind != TERMINATES
117121
})
118122

119-
newPipes := make([]*Pipe, 0, len(currentPipes)+len(commit.Parents()))
123+
newPipes := make([]*Pipe, 0, len(currentPipes)+len(commit.ParentPtrs()))
120124
// start by assuming that we've got a brand new commit not related to any preceding commit.
121125
// (this only happens when we're doing `git log --all`). These will be tacked onto the far end.
122126
pos := maxPos + 1
123127
for _, pipe := range currentPipes {
124-
if equalHashes(pipe.toHash, commit.Hash()) {
128+
if equalHashes(pipe.toHash, commit.HashPtr()) {
125129
// turns out this commit does have a descendant so we'll place it right under the first instance
126130
pos = pipe.toPos
127131
break
@@ -133,24 +137,24 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
133137
// a traversed spot is one where a current pipe is starting on, ending on, or passing through
134138
traversedSpots := set.New[int]()
135139

136-
var toHash string
140+
var toHash *string
137141
if commit.IsFirstCommit() {
138-
toHash = models.EmptyTreeCommitHash
142+
toHash = &EmptyTreeCommitHash
139143
} else {
140-
toHash = commit.Parents()[0]
144+
toHash = commit.ParentPtrs()[0]
141145
}
142146
newPipes = append(newPipes, &Pipe{
143147
fromPos: pos,
144148
toPos: pos,
145-
fromHash: commit.Hash(),
149+
fromHash: commit.HashPtr(),
146150
toHash: toHash,
147151
kind: STARTS,
148152
style: getStyle(commit),
149153
})
150154

151155
traversedSpotsForContinuingPipes := set.New[int]()
152156
for _, pipe := range currentPipes {
153-
if !equalHashes(pipe.toHash, commit.Hash()) {
157+
if !equalHashes(pipe.toHash, commit.HashPtr()) {
154158
traversedSpotsForContinuingPipes.Add(pipe.toPos)
155159
}
156160
}
@@ -189,7 +193,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
189193
}
190194

191195
for _, pipe := range currentPipes {
192-
if equalHashes(pipe.toHash, commit.Hash()) {
196+
if equalHashes(pipe.toHash, commit.HashPtr()) {
193197
// terminating here
194198
newPipes = append(newPipes, &Pipe{
195199
fromPos: pipe.toPos,
@@ -216,13 +220,13 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
216220
}
217221

218222
if commit.IsMerge() {
219-
for _, parent := range commit.Parents()[1:] {
223+
for _, parent := range commit.ParentPtrs()[1:] {
220224
availablePos := getNextAvailablePosForNewPipe()
221225
// need to act as if continuing pipes are going to continue on the same line.
222226
newPipes = append(newPipes, &Pipe{
223227
fromPos: pos,
224228
toPos: availablePos,
225-
fromHash: commit.Hash(),
229+
fromHash: commit.HashPtr(),
226230
toHash: parent,
227231
kind: STARTS,
228232
style: getStyle(commit),
@@ -233,7 +237,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
233237
}
234238

235239
for _, pipe := range currentPipes {
236-
if !equalHashes(pipe.toHash, commit.Hash()) && pipe.toPos > pos {
240+
if !equalHashes(pipe.toHash, commit.HashPtr()) && pipe.toPos > pos {
237241
// continuing on, potentially moving left to fill in a blank spot
238242
last := pipe.toPos
239243
for i := pipe.toPos; i > pos; i-- {
@@ -268,7 +272,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod
268272

269273
func renderPipeSet(
270274
pipes []*Pipe,
271-
selectedCommitHash string,
275+
selectedCommitHashPtr *string,
272276
prevCommit *models.Commit,
273277
) string {
274278
maxPos := 0
@@ -315,10 +319,10 @@ func renderPipeSet(
315319
// we don't want to highlight two commits if they're contiguous. We only want
316320
// to highlight multiple things if there's an actual visible pipe involved.
317321
highlight := true
318-
if prevCommit != nil && equalHashes(prevCommit.Hash(), selectedCommitHash) {
322+
if prevCommit != nil && equalHashes(prevCommit.HashPtr(), selectedCommitHashPtr) {
319323
highlight = false
320324
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) {
322326
highlight = true
323327
}
324328
}
@@ -327,7 +331,7 @@ func renderPipeSet(
327331
// so we have our commit pos again, now it's time to build the cells.
328332
// we'll handle the one that's sourced from our selected commit last so that it can override the other cells.
329333
selectedPipes, nonSelectedPipes := utils.Partition(pipes, func(pipe *Pipe) bool {
330-
return highlight && equalHashes(pipe.fromHash, selectedCommitHash)
334+
return highlight && equalHashes(pipe.fromHash, selectedCommitHashPtr)
331335
})
332336

333337
for _, pipe := range nonSelectedPipes {
@@ -370,13 +374,13 @@ func renderPipeSet(
370374
return writer.String()
371375
}
372376

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 {
376380
return false
377381
}
378382

379-
length := min(len(a), len(b))
383+
length := min(len(*a), len(*b))
380384
// 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]
382386
}

0 commit comments

Comments
 (0)