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.
This commit is contained in:
Stefan Haller 2025-04-26 19:58:03 +02:00
parent e27bc15bbd
commit 0f1f455edb
4 changed files with 13 additions and 9 deletions

View file

@ -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

View file

@ -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{

View file

@ -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

View file

@ -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(),
}
}