From a0d179b6dcb1dc27690c447dd55f264bcc9cbcb0 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 7 Apr 2023 19:28:31 +0200 Subject: [PATCH] Make getHydratedRebasingCommits more robust So far the algorithm worked on the assumption that the output of the "git show" command corresponds one-to-one to the lines of the rebase-todo file. This assumption doesn't hold once we start to include todo lines that don't have a sha (like update-ref), or when the todo file contains multiple entries for the same sha. This should never happen normally, but it can if users manually edit the todo file and duplicate a line. --- pkg/commands/git_commands/commit_loader.go | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/pkg/commands/git_commands/commit_loader.go b/pkg/commands/git_commands/commit_loader.go index 3b2e52ec4..6e409d717 100644 --- a/pkg/commands/git_commands/commit_loader.go +++ b/pkg/commands/git_commands/commit_loader.go @@ -194,8 +194,8 @@ func (self *CommitLoader) getHydratedRebasingCommits(rebaseMode enums.RebaseMode return nil, nil } - commitShas := slices.Map(commits, func(commit *models.Commit) string { - return commit.Sha + commitShas := slices.FilterMap(commits, func(commit *models.Commit) (string, bool) { + return commit.Sha, commit.Sha != "" }) // note that we're not filtering these as we do non-rebasing commits just because @@ -209,20 +209,26 @@ func (self *CommitLoader) getHydratedRebasingCommits(rebaseMode enums.RebaseMode ), ).DontLog() - hydratedCommits := make([]*models.Commit, 0, len(commits)) - i := 0 + fullCommits := map[string]*models.Commit{} err = cmdObj.RunAndProcessLines(func(line string) (bool, error) { commit := self.extractCommitFromLine(line) - matchingCommit := commits[i] - commit.Action = matchingCommit.Action - commit.Status = matchingCommit.Status - hydratedCommits = append(hydratedCommits, commit) - i++ + fullCommits[commit.Sha] = commit return false, nil }) if err != nil { return nil, err } + + hydratedCommits := make([]*models.Commit, 0, len(commits)) + for _, rebasingCommit := range commits { + if rebasingCommit.Sha == "" { + hydratedCommits = append(hydratedCommits, rebasingCommit) + } else if commit := fullCommits[rebasingCommit.Sha]; commit != nil { + commit.Action = rebasingCommit.Action + commit.Status = rebasingCommit.Status + hydratedCommits = append(hydratedCommits, commit) + } + } return hydratedCommits, nil }