From 0f1f455edba96c96592203b60722498495ac5f36 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 26 Apr 2025 19:58:03 +0200 Subject: [PATCH] Make Commit.Parents a getter for an unexported parents field This is exactly the same as what we did for Hash earlier. And for the same reason: we want to turn the parents field into a slice of pointers. --- pkg/commands/models/commit.go | 12 ++++++++---- pkg/gui/presentation/graph/graph.go | 6 +++--- pkg/gui/presentation/graph/graph_test.go | 2 +- .../services/custom_commands/session_state_loader.go | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/commands/models/commit.go b/pkg/commands/models/commit.go index bde7092db..1e2191e92 100644 --- a/pkg/commands/models/commit.go +++ b/pkg/commands/models/commit.go @@ -54,7 +54,7 @@ type Commit struct { Divergence Divergence // set to DivergenceNone unless we are showing the divergence view // Hashes of parent commits (will be multiple if it's a merge commit) - Parents []string + parents []string } type NewCommitOpts struct { @@ -83,7 +83,7 @@ func NewCommit(hashPool *utils.StringPool, opts NewCommitOpts) *Commit { AuthorEmail: opts.AuthorEmail, UnixTimestamp: opts.UnixTimestamp, Divergence: opts.Divergence, - Parents: opts.Parents, + parents: opts.Parents, } } @@ -114,8 +114,12 @@ func (c *Commit) ParentRefName() string { return c.RefName() + "^" } +func (c *Commit) Parents() []string { + return c.parents +} + func (c *Commit) IsFirstCommit() bool { - return len(c.Parents) == 0 + return len(c.parents) == 0 } func (c *Commit) ID() string { @@ -127,7 +131,7 @@ func (c *Commit) Description() string { } func (c *Commit) IsMerge() bool { - return len(c.Parents) > 1 + return len(c.parents) > 1 } // returns true if this commit is not actually in the git log but instead diff --git a/pkg/gui/presentation/graph/graph.go b/pkg/gui/presentation/graph/graph.go index 9f71bb26e..de46193ed 100644 --- a/pkg/gui/presentation/graph/graph.go +++ b/pkg/gui/presentation/graph/graph.go @@ -116,7 +116,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod return pipe.kind != TERMINATES }) - newPipes := make([]*Pipe, 0, len(currentPipes)+len(commit.Parents)) + newPipes := make([]*Pipe, 0, len(currentPipes)+len(commit.Parents())) // start by assuming that we've got a brand new commit not related to any preceding commit. // (this only happens when we're doing `git log --all`). These will be tacked onto the far end. pos := maxPos + 1 @@ -137,7 +137,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod if commit.IsFirstCommit() { toHash = models.EmptyTreeCommitHash } else { - toHash = commit.Parents[0] + toHash = commit.Parents()[0] } newPipes = append(newPipes, &Pipe{ fromPos: pos, @@ -216,7 +216,7 @@ func getNextPipes(prevPipes []*Pipe, commit *models.Commit, getStyle func(c *mod } if commit.IsMerge() { - for _, parent := range commit.Parents[1:] { + for _, parent := range commit.Parents()[1:] { availablePos := getNextAvailablePosForNewPipe() // need to act as if continuing pipes are going to continue on the same line. newPipes = append(newPipes, &Pipe{ diff --git a/pkg/gui/presentation/graph/graph_test.go b/pkg/gui/presentation/graph/graph_test.go index 63d8cb049..17914d8d5 100644 --- a/pkg/gui/presentation/graph/graph_test.go +++ b/pkg/gui/presentation/graph/graph_test.go @@ -576,7 +576,7 @@ func generateCommits(count int) []*models.Commit { // I need to pick a random number of parents to add parentCount := rnd.Intn(2) + 1 - parentHashes := currentCommit.Parents + parentHashes := currentCommit.Parents() for j := 0; j < parentCount; j++ { reuseParent := rnd.Intn(6) != 1 && j <= len(pool)-1 && j != 0 var newParent *models.Commit diff --git a/pkg/gui/services/custom_commands/session_state_loader.go b/pkg/gui/services/custom_commands/session_state_loader.go index b90ec185a..87465ec1a 100644 --- a/pkg/gui/services/custom_commands/session_state_loader.go +++ b/pkg/gui/services/custom_commands/session_state_loader.go @@ -37,7 +37,7 @@ func commitShimFromModelCommit(commit *models.Commit) *Commit { AuthorEmail: commit.AuthorEmail, UnixTimestamp: commit.UnixTimestamp, Divergence: commit.Divergence, - Parents: commit.Parents, + Parents: commit.Parents(), } }