mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-09-15 18:16: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.
100 lines
2.7 KiB
Go
100 lines
2.7 KiB
Go
package git_commands
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCommitStoreLoaderLoad(t *testing.T) {
|
|
commitsOutputStore := strings.Replace(`a|b
|
|
b|c d
|
|
c|e
|
|
d|e
|
|
e|`, "|", "\x00", -1)
|
|
|
|
args := []string{"log", "--all", "--pretty=format:%H%x00%P"}
|
|
|
|
type scenario struct {
|
|
testName string
|
|
runner *oscommands.FakeCmdObjRunner
|
|
startingCommits []models.ImmutableCommit
|
|
expectedCommits []models.ImmutableCommit
|
|
expectedError error
|
|
}
|
|
|
|
scenarios := []scenario{
|
|
{
|
|
testName: "should return no commits if there are none",
|
|
runner: oscommands.NewFakeRunner(t).
|
|
ExpectGitArgs(args, "", nil),
|
|
|
|
startingCommits: []models.ImmutableCommit{},
|
|
expectedCommits: []models.ImmutableCommit{},
|
|
expectedError: nil,
|
|
},
|
|
{
|
|
testName: "properly processes commits",
|
|
runner: oscommands.NewFakeRunner(t).
|
|
ExpectGitArgs(args, commitsOutputStore, nil),
|
|
|
|
startingCommits: []models.ImmutableCommit{},
|
|
expectedCommits: []models.ImmutableCommit{
|
|
models.NewImmutableCommit("a", []string{"b"}),
|
|
models.NewImmutableCommit("b", []string{"c", "d"}),
|
|
models.NewImmutableCommit("c", []string{"e"}),
|
|
models.NewImmutableCommit("d", []string{"e"}),
|
|
models.NewImmutableCommit("e", []string{}),
|
|
},
|
|
expectedError: nil,
|
|
},
|
|
{
|
|
testName: "merges into exising commits",
|
|
runner: oscommands.NewFakeRunner(t).
|
|
ExpectGitArgs(args, commitsOutputStore, nil),
|
|
|
|
startingCommits: []models.ImmutableCommit{
|
|
// this one is present in the command output
|
|
models.NewImmutableCommit("a", []string{"b"}),
|
|
// this one isn't
|
|
models.NewImmutableCommit("f", []string{"g"}),
|
|
},
|
|
expectedCommits: []models.ImmutableCommit{
|
|
models.NewImmutableCommit("a", []string{"b"}),
|
|
models.NewImmutableCommit("b", []string{"c", "d"}),
|
|
models.NewImmutableCommit("c", []string{"e"}),
|
|
models.NewImmutableCommit("d", []string{"e"}),
|
|
models.NewImmutableCommit("e", []string{}),
|
|
models.NewImmutableCommit("f", []string{"g"}),
|
|
},
|
|
expectedError: nil,
|
|
},
|
|
}
|
|
|
|
for _, scenario := range scenarios {
|
|
scenario := scenario
|
|
t.Run(scenario.testName, func(t *testing.T) {
|
|
common := utils.NewDummyCommon()
|
|
|
|
builder := &CommitStoreLoader{
|
|
Common: common,
|
|
cmd: oscommands.NewDummyCmdObjBuilder(scenario.runner),
|
|
}
|
|
|
|
commitStore := models.NewCommitStore()
|
|
commitStore.AddSlice(scenario.startingCommits)
|
|
|
|
err := builder.Load(commitStore)
|
|
|
|
assert.EqualValues(t, scenario.expectedCommits, commitStore.Slice())
|
|
assert.Equal(t, scenario.expectedError, err)
|
|
|
|
scenario.runner.CheckForMissingCalls()
|
|
})
|
|
}
|
|
}
|