mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-09-12 08:36:25 -04:00
I want an efficient way to get information about the commits of a repo. So far, our Commit model contains a mix of immutable and mutable fields, and this means we need to throw out commits whenever we refresh, because one of the mutable fields may have changed. The commit store will store a new model, ImmutableCommit which never changes so we can just continue adding commits to the store without worrying about invalidating any of it. One use case for this store is the ability to determine if one commit is an ancestor of another, which will help us colour the commits against each of our branches in the local branches view. Without an in-memory store, we would need to make one git call per commit which would be super slow. If this store proves useful, we could switch to using it as the source of truth for our commits, with mutable stuff handled separately.
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package git_commands
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
"github.com/jesseduffield/lazygit/pkg/common"
|
|
)
|
|
|
|
// CommitStoreLoader populates a commit store with commits from the git log.
|
|
type CommitStoreLoader struct {
|
|
*common.Common
|
|
cmd oscommands.ICmdObjBuilder
|
|
}
|
|
|
|
func NewCommitStoreLoader(
|
|
cmn *common.Common,
|
|
cmd oscommands.ICmdObjBuilder,
|
|
) *CommitStoreLoader {
|
|
return &CommitStoreLoader{
|
|
Common: cmn,
|
|
cmd: cmd,
|
|
}
|
|
}
|
|
|
|
// mutates the given commit store to add commits from the git log
|
|
func (self *CommitStoreLoader) Load(commitStore *models.CommitStore) error {
|
|
t := time.Now()
|
|
|
|
err := self.getLogCmd().RunAndProcessLines(func(line string) (bool, error) {
|
|
commit := self.extractCommitFromLine(line)
|
|
commitStore.Add(commit)
|
|
return false, nil
|
|
})
|
|
|
|
self.Log.Warnf("CommitStoreLoader Load took %s", time.Since(t))
|
|
|
|
return err
|
|
}
|
|
|
|
// getLog gets the git log.
|
|
func (self *CommitStoreLoader) getLogCmd() oscommands.ICmdObj {
|
|
cmdArgs := NewGitCmd("log").
|
|
Arg("--all").
|
|
Arg(`--pretty=format:%H%x00%P`).
|
|
ToArgv()
|
|
|
|
return self.cmd.New(cmdArgs).DontLog()
|
|
}
|
|
|
|
func (self *CommitStoreLoader) extractCommitFromLine(line string) models.ImmutableCommit {
|
|
split := strings.SplitN(line, "\x00", 2)
|
|
|
|
sha := split[0]
|
|
|
|
parentsStr := split[1]
|
|
parents := []string{}
|
|
if len(parentsStr) > 0 {
|
|
parents = strings.Split(parentsStr, " ")
|
|
}
|
|
|
|
return models.NewImmutableCommit(sha, parents)
|
|
}
|