Skip to content

Commit 88dae1d

Browse files
committed
Refactor commit loading and reflog commit loading to extract shared code
These are very similar in that they call RunAndProcessLines on a git log command and then process lines to create commits. Extract this into a common helper function. At the moment this doesn't gain us much, but in the next commit we will extend this helper function considerably with filter path logic, which would otherwise have to be duplicated in both places.
1 parent e7c33a7 commit 88dae1d

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

pkg/commands/git_commands/commit_loader.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ func (self *CommitLoader) GetCommits(opts GetCommitsOptions) ([]*models.Commit,
8989
go utils.Safe(func() {
9090
defer wg.Done()
9191

92-
logErr = self.getLogCmd(opts).RunAndProcessLines(func(line string) (bool, error) {
93-
commit := self.extractCommitFromLine(opts.HashPool, line, opts.RefToShowDivergenceFrom != "")
94-
commits = append(commits, commit)
95-
return false, nil
92+
var realCommits []*models.Commit
93+
realCommits, logErr = loadCommits(self.getLogCmd(opts), func(line string) (*models.Commit, bool) {
94+
return self.extractCommitFromLine(opts.HashPool, line, opts.RefToShowDivergenceFrom != ""), false
9695
})
96+
if logErr == nil {
97+
commits = append(commits, realCommits...)
98+
}
9799
})
98100

99101
var ancestor string
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package git_commands
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/commands/models"
5+
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
6+
)
7+
8+
func loadCommits(
9+
cmd *oscommands.CmdObj,
10+
parseLogLine func(string) (*models.Commit, bool),
11+
) ([]*models.Commit, error) {
12+
commits := []*models.Commit{}
13+
14+
err := cmd.RunAndProcessLines(func(line string) (bool, error) {
15+
commit, stop := parseLogLine(line)
16+
if stop {
17+
return true, nil
18+
}
19+
commits = append(commits, commit)
20+
return false, nil
21+
})
22+
return commits, err
23+
}

pkg/commands/git_commands/reflog_commit_loader.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ func NewReflogCommitLoader(common *common.Common, cmd oscommands.ICmdObjBuilder)
2525
// GetReflogCommits only returns the new reflog commits since the given lastReflogCommit
2626
// if none is passed (i.e. it's value is nil) then we get all the reflog commits
2727
func (self *ReflogCommitLoader) GetReflogCommits(hashPool *utils.StringPool, lastReflogCommit *models.Commit, filterPath string, filterAuthor string) ([]*models.Commit, bool, error) {
28-
commits := make([]*models.Commit, 0)
29-
3028
cmdArgs := NewGitCmd("log").
3129
Config("log.showSignature=false").
3230
Arg("-g").
@@ -38,10 +36,11 @@ func (self *ReflogCommitLoader) GetReflogCommits(hashPool *utils.StringPool, las
3836
cmdObj := self.cmd.New(cmdArgs).DontLog()
3937

4038
onlyObtainedNewReflogCommits := false
41-
err := cmdObj.RunAndProcessLines(func(line string) (bool, error) {
39+
40+
commits, err := loadCommits(cmdObj, func(line string) (*models.Commit, bool) {
4241
commit, ok := self.parseLine(hashPool, line)
4342
if !ok {
44-
return false, nil
43+
return nil, false
4544
}
4645

4746
// note that the unix timestamp here is the timestamp of the COMMIT, not the reflog entry itself,
@@ -51,11 +50,10 @@ func (self *ReflogCommitLoader) GetReflogCommits(hashPool *utils.StringPool, las
5150
if lastReflogCommit != nil && self.sameReflogCommit(commit, lastReflogCommit) {
5251
onlyObtainedNewReflogCommits = true
5352
// after this point we already have these reflogs loaded so we'll simply return the new ones
54-
return true, nil
53+
return nil, true
5554
}
5655

57-
commits = append(commits, commit)
58-
return false, nil
56+
return commit, false
5957
})
6058
if err != nil {
6159
return nil, false, err

0 commit comments

Comments
 (0)