mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-06 22:33:07 +02:00
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.
This commit is contained in:
@ -89,11 +89,13 @@ func (self *CommitLoader) GetCommits(opts GetCommitsOptions) ([]*models.Commit,
|
|||||||
go utils.Safe(func() {
|
go utils.Safe(func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
logErr = self.getLogCmd(opts).RunAndProcessLines(func(line string) (bool, error) {
|
var realCommits []*models.Commit
|
||||||
commit := self.extractCommitFromLine(opts.HashPool, line, opts.RefToShowDivergenceFrom != "")
|
realCommits, logErr = loadCommits(self.getLogCmd(opts), func(line string) (*models.Commit, bool) {
|
||||||
commits = append(commits, commit)
|
return self.extractCommitFromLine(opts.HashPool, line, opts.RefToShowDivergenceFrom != ""), false
|
||||||
return false, nil
|
|
||||||
})
|
})
|
||||||
|
if logErr == nil {
|
||||||
|
commits = append(commits, realCommits...)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
var ancestor string
|
var ancestor string
|
||||||
|
23
pkg/commands/git_commands/commit_loading_shared.go
Normal file
23
pkg/commands/git_commands/commit_loading_shared.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package git_commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||||
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||||
|
)
|
||||||
|
|
||||||
|
func loadCommits(
|
||||||
|
cmd *oscommands.CmdObj,
|
||||||
|
parseLogLine func(string) (*models.Commit, bool),
|
||||||
|
) ([]*models.Commit, error) {
|
||||||
|
commits := []*models.Commit{}
|
||||||
|
|
||||||
|
err := cmd.RunAndProcessLines(func(line string) (bool, error) {
|
||||||
|
commit, stop := parseLogLine(line)
|
||||||
|
if stop {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
commits = append(commits, commit)
|
||||||
|
return false, nil
|
||||||
|
})
|
||||||
|
return commits, err
|
||||||
|
}
|
@ -25,8 +25,6 @@ func NewReflogCommitLoader(common *common.Common, cmd oscommands.ICmdObjBuilder)
|
|||||||
// GetReflogCommits only returns the new reflog commits since the given lastReflogCommit
|
// GetReflogCommits only returns the new reflog commits since the given lastReflogCommit
|
||||||
// if none is passed (i.e. it's value is nil) then we get all the reflog commits
|
// if none is passed (i.e. it's value is nil) then we get all the reflog commits
|
||||||
func (self *ReflogCommitLoader) GetReflogCommits(hashPool *utils.StringPool, lastReflogCommit *models.Commit, filterPath string, filterAuthor string) ([]*models.Commit, bool, error) {
|
func (self *ReflogCommitLoader) GetReflogCommits(hashPool *utils.StringPool, lastReflogCommit *models.Commit, filterPath string, filterAuthor string) ([]*models.Commit, bool, error) {
|
||||||
commits := make([]*models.Commit, 0)
|
|
||||||
|
|
||||||
cmdArgs := NewGitCmd("log").
|
cmdArgs := NewGitCmd("log").
|
||||||
Config("log.showSignature=false").
|
Config("log.showSignature=false").
|
||||||
Arg("-g").
|
Arg("-g").
|
||||||
@ -38,10 +36,11 @@ func (self *ReflogCommitLoader) GetReflogCommits(hashPool *utils.StringPool, las
|
|||||||
cmdObj := self.cmd.New(cmdArgs).DontLog()
|
cmdObj := self.cmd.New(cmdArgs).DontLog()
|
||||||
|
|
||||||
onlyObtainedNewReflogCommits := false
|
onlyObtainedNewReflogCommits := false
|
||||||
err := cmdObj.RunAndProcessLines(func(line string) (bool, error) {
|
|
||||||
|
commits, err := loadCommits(cmdObj, func(line string) (*models.Commit, bool) {
|
||||||
commit, ok := self.parseLine(hashPool, line)
|
commit, ok := self.parseLine(hashPool, line)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false, nil
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// note that the unix timestamp here is the timestamp of the COMMIT, not the reflog entry itself,
|
// 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
|
|||||||
if lastReflogCommit != nil && self.sameReflogCommit(commit, lastReflogCommit) {
|
if lastReflogCommit != nil && self.sameReflogCommit(commit, lastReflogCommit) {
|
||||||
onlyObtainedNewReflogCommits = true
|
onlyObtainedNewReflogCommits = true
|
||||||
// after this point we already have these reflogs loaded so we'll simply return the new ones
|
// after this point we already have these reflogs loaded so we'll simply return the new ones
|
||||||
return true, nil
|
return nil, true
|
||||||
}
|
}
|
||||||
|
|
||||||
commits = append(commits, commit)
|
return commit, false
|
||||||
return false, nil
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
|
Reference in New Issue
Block a user