diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-06-07 17:31:44 +0200 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-06-10 12:00:24 +0200 |
commit | f3718ddfb0fbb97951092b2c3470ab2fdc0ac57f (patch) | |
tree | f36ba7e1091650ca70d0969d3b8ddd501cb2c199 /pkg | |
parent | f9ba2dac9d161d73b5c308a94aa1e03fb00df826 (diff) |
Don't reference Model().Commits multiple times
Copy the slice into a variable and use that throughout the whole operation; this
makes us a little more robust against the model refreshing concurrently.
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/controllers/helpers/fixup_helper.go | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/pkg/gui/controllers/helpers/fixup_helper.go b/pkg/gui/controllers/helpers/fixup_helper.go index 7177398bb..c3ef28af5 100644 --- a/pkg/gui/controllers/helpers/fixup_helper.go +++ b/pkg/gui/controllers/helpers/fixup_helper.go @@ -50,6 +50,8 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error { deletedLineHunks, addedLineHunks := parseDiff(diff) + commits := self.c.Model().Commits + var hashes []string warnAboutAddedLines := false @@ -57,7 +59,7 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error { hashes, err = self.blameDeletedLines(deletedLineHunks) warnAboutAddedLines = len(addedLineHunks) > 0 } else if len(addedLineHunks) > 0 { - hashes, err = self.blameAddedLines(addedLineHunks) + hashes, err = self.blameAddedLines(commits, addedLineHunks) } else { return errors.New(self.c.Tr.NoChangedFiles) } @@ -81,9 +83,8 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error { return fmt.Errorf("%s\n\n%s", message, subjects) } - commit, index, ok := self.findCommit(hashes[0]) + commit, index, ok := self.findCommit(commits, hashes[0]) if !ok { - commits := self.c.Model().Commits if commits[len(commits)-1].Status == models.StatusMerged { // If the commit is not found, it's most likely because it's already // merged, and more than 300 commits away. Check if the last known @@ -225,7 +226,7 @@ func (self *FixupHelper) blameDeletedLines(deletedLineHunks []*hunk) ([]string, return result.ToSlice(), errg.Wait() } -func (self *FixupHelper) blameAddedLines(addedLineHunks []*hunk) ([]string, error) { +func (self *FixupHelper) blameAddedLines(commits []*models.Commit, addedLineHunks []*hunk) ([]string, error) { errg := errgroup.Group{} hashesChan := make(chan []string) @@ -288,8 +289,8 @@ func (self *FixupHelper) blameAddedLines(addedLineHunks []*hunk) ([]string, erro if hashes[0] == hashes[1] { result.Add(hashes[0]) } else { - _, index1, ok1 := self.findCommit(hashes[0]) - _, index2, ok2 := self.findCommit(hashes[1]) + _, index1, ok1 := self.findCommit(commits, hashes[0]) + _, index2, ok2 := self.findCommit(commits, hashes[1]) if ok1 && ok2 { result.Add(lo.Ternary(index1 < index2, hashes[0], hashes[1])) } else if ok1 { @@ -306,8 +307,8 @@ func (self *FixupHelper) blameAddedLines(addedLineHunks []*hunk) ([]string, erro return result.ToSlice(), errg.Wait() } -func (self *FixupHelper) findCommit(hash string) (*models.Commit, int, bool) { - return lo.FindIndexOf(self.c.Model().Commits, func(commit *models.Commit) bool { +func (self *FixupHelper) findCommit(commits []*models.Commit, hash string) (*models.Commit, int, bool) { + return lo.FindIndexOf(commits, func(commit *models.Commit) bool { return commit.Hash == hash }) } |