Repository: jesseduffield/lazygit
File: pkg/commands/git_commands/commit_loader.go (Go)

Code before:
	// note that we're not filtering these as we do non-rebasing commits just because
	// I suspect that will cause some damage
	cmdObj := self.cmd.New(
		NewGitCmd("show").
			Config("log.showSignature=false").
			Arg("--no-patch", "--oneline", "--abbrev=20", prettyFormat).
			Arg(commitShas...).
			ToArgv(),
	).DontLog()

	fullCommits := []*models.Commit{}
	err = cmdObj.RunAndProcessLines(func(line string) (bool, error) {
		commit := self.extractCommitFromLine(line)
		fullCommits = append(fullCommits, 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, found := lo.Find(fullCommits,
			func(c *models.Commit) bool { return strings.HasPrefix(c.Sha, rebasingCommit.Sha) }); found {
			commit.Action = rebasingCommit.Action
			commit.Status = rebasingCommit.Status
			hydratedCommits = append(hydratedCommits, commit)
		}
	}
	return hydratedCommits, nil
}

// getRebasingCommits obtains the commits that we're in the process of rebasing
func (self *CommitLoader) getRebasingCommits(rebaseMode enums.RebaseMode) ([]*models.Commit, error) {
	switch rebaseMode {
	case enums.REBASE_MODE_MERGING:
		return self.getNormalRebasingCommits()
	case enums.REBASE_MODE_INTERACTIVE:
		return self.getInteractiveRebasingCommits()
	default:
		return nil, nil
	}
}

func (self *CommitLoader) getNormalRebasingCommits() ([]*models.Commit, error) {
	rewrittenCount := 0
	bytesContent, err := self.readFile(filepath.Join(self.dotGitDir, "rebase-apply/rewritten"))
	if err == nil {
		content := string(bytesContent)

Review comment (performance):
I didn't do any measurements, but I doubt that the performance impact of the linear search is significant. If we are concerned about this, we could do the slice/linear search version only if the git version is older than 2.25.2, and use the map otherwise. But this would make the code more complex, so I wouldn't be in favor of doing that.

Code after:
		return commit.Sha, commit.Sha != ""
	})

	// note that we're not filtering these as we do non-rebasing commits just because
	// I suspect that will cause some damage
	cmdObj := self.cmd.New(
		NewGitCmd("show").
			Config("log.showSignature=false").
			Arg("--no-patch", "--oneline", "--abbrev=20", prettyFormat).
			Arg(commitShas...).
			ToArgv(),
	).DontLog()

	fullCommits := map[string]*models.Commit{}
	err = cmdObj.RunAndProcessLines(func(line string) (bool, error) {
		commit := self.extractCommitFromLine(line)
		fullCommits[commit.Sha] = commit
		return false, nil
	})
	if err != nil {
		return nil, err
	}

	findFullCommit := lo.Ternary(self.version.IsOlderThan(2, 25, 2),
		func(sha string) *models.Commit {
			for s, c := range fullCommits {
				if strings.HasPrefix(s, sha) {
					return c
				}
			}
			return nil
		},
		func(sha string) *models.Commit {
			return fullCommits[sha]
		})

	hydratedCommits := make([]*models.Commit, 0, len(commits))
	for _, rebasingCommit := range commits {
		if rebasingCommit.Sha == "" {
			hydratedCommits = append(hydratedCommits, rebasingCommit)
		} else if commit := findFullCommit(rebasingCommit.Sha); commit != nil {
			commit.Action = rebasingCommit.Action
			commit.Status = rebasingCommit.Status
			hydratedCommits = append(hydratedCommits, commit)
		}
	}
	return hydratedCommits, nil
}

// getRebasingCommits obtains the commits that we're in the process of rebasing
func (self *CommitLoader) getRebasingCommits(rebaseMode enums.RebaseMode) ([]*models.Commit, error) {