Skip to content

Commit

Permalink
refactor to group up more commonly used git command stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Jan 18, 2022
1 parent 9706416 commit 3e80a9e
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 297 deletions.
39 changes: 17 additions & 22 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,27 @@ func NewGitCommandAux(
// This is admittedly messy, but allows us to test each command struct in isolation,
// and allows for better namespacing when compared to having every method living
// on the one struct.
// common ones are: cmn, osCommand, dotGitDir, configCommands
configCommands := git_commands.NewConfigCommands(cmn, gitConfig, repo)
statusCommands := git_commands.NewStatusCommands(cmn, osCommand, repo, dotGitDir)
gitCommon := git_commands.NewGitCommon(cmn, cmd, osCommand, dotGitDir, repo, configCommands)

statusCommands := git_commands.NewStatusCommands(gitCommon)
fileLoader := loaders.NewFileLoader(cmn, cmd, configCommands)
flowCommands := git_commands.NewFlowCommands(cmn, cmd, configCommands)
remoteCommands := git_commands.NewRemoteCommands(cmn, cmd)
branchCommands := git_commands.NewBranchCommands(cmn, cmd)
syncCommands := git_commands.NewSyncCommands(cmn, cmd)
tagCommands := git_commands.NewTagCommands(cmn, cmd)
commitCommands := git_commands.NewCommitCommands(cmn, cmd)
customCommands := git_commands.NewCustomCommands(cmn, cmd)
fileCommands := git_commands.NewFileCommands(cmn, cmd, configCommands, osCommand)
submoduleCommands := git_commands.NewSubmoduleCommands(cmn, cmd, dotGitDir)
workingTreeCommands := git_commands.NewWorkingTreeCommands(cmn, cmd, submoduleCommands, osCommand, fileLoader)
rebaseCommands := git_commands.NewRebaseCommands(
cmn,
cmd,
osCommand,
commitCommands,
workingTreeCommands,
configCommands,
dotGitDir,
)
stashCommands := git_commands.NewStashCommands(cmn, cmd, osCommand, fileLoader, workingTreeCommands)
flowCommands := git_commands.NewFlowCommands(gitCommon)
remoteCommands := git_commands.NewRemoteCommands(gitCommon)
branchCommands := git_commands.NewBranchCommands(gitCommon)
syncCommands := git_commands.NewSyncCommands(gitCommon)
tagCommands := git_commands.NewTagCommands(gitCommon)
commitCommands := git_commands.NewCommitCommands(gitCommon)
customCommands := git_commands.NewCustomCommands(gitCommon)
fileCommands := git_commands.NewFileCommands(gitCommon)
submoduleCommands := git_commands.NewSubmoduleCommands(gitCommon)
workingTreeCommands := git_commands.NewWorkingTreeCommands(gitCommon, submoduleCommands, fileLoader)
rebaseCommands := git_commands.NewRebaseCommands(gitCommon, commitCommands, workingTreeCommands)
stashCommands := git_commands.NewStashCommands(gitCommon, fileLoader, workingTreeCommands)
// TODO: have patch manager take workingTreeCommands in its entirety
patchManager := patch.NewPatchManager(cmn.Log, workingTreeCommands.ApplyPatch, workingTreeCommands.ShowFileDiff)
patchCommands := git_commands.NewPatchCommands(cmn, cmd, rebaseCommands, commitCommands, configCommands, statusCommands, patchManager)
patchCommands := git_commands.NewPatchCommands(gitCommon, rebaseCommands, commitCommands, statusCommands, stashCommands, patchManager)

return &GitCommand{
Branch: branchCommands,
Expand Down
13 changes: 3 additions & 10 deletions pkg/commands/git_commands/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/utils"
)

Expand All @@ -17,18 +16,12 @@ import (
const CurrentBranchNameRegex = `(?m)^\*.*?([^ ]*?)\)?$`

type BranchCommands struct {
*common.Common

cmd oscommands.ICmdObjBuilder
*GitCommon
}

func NewBranchCommands(
common *common.Common,
cmd oscommands.ICmdObjBuilder,
) *BranchCommands {
func NewBranchCommands(gitCommon *GitCommon) *BranchCommands {
return &BranchCommands{
Common: common,
cmd: cmd,
GitCommon: gitCommon,
}
}

Expand Down
22 changes: 8 additions & 14 deletions pkg/commands/git_commands/branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ import (

"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/stretchr/testify/assert"
)

func NewBranchCommandsWithRunner(runner *oscommands.FakeCmdObjRunner) *BranchCommands {
builder := oscommands.NewDummyCmdObjBuilder(runner)
return NewBranchCommands(utils.NewDummyCommon(), builder)
}

func TestBranchGetCommitDifferences(t *testing.T) {
type scenario struct {
testName string
Expand Down Expand Up @@ -48,7 +42,7 @@ func TestBranchGetCommitDifferences(t *testing.T) {
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := NewBranchCommandsWithRunner(s.runner)
instance := buildBranchCommands(commonDeps{runner: s.runner})
pushables, pullables := instance.GetCommitDifferences("HEAD", "@{u}")
assert.EqualValues(t, s.expectedPushables, pushables)
assert.EqualValues(t, s.expectedPullables, pullables)
Expand All @@ -60,7 +54,7 @@ func TestBranchGetCommitDifferences(t *testing.T) {
func TestBranchNewBranch(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
Expect(`git checkout -b "test" "master"`, "", nil)
instance := NewBranchCommandsWithRunner(runner)
instance := buildBranchCommands(commonDeps{runner: runner})

assert.NoError(t, instance.New("test", "master"))
runner.CheckForMissingCalls()
Expand Down Expand Up @@ -96,7 +90,7 @@ func TestBranchDeleteBranch(t *testing.T) {
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := NewBranchCommandsWithRunner(s.runner)
instance := buildBranchCommands(commonDeps{runner: s.runner})

s.test(instance.Delete("test", s.force))
s.runner.CheckForMissingCalls()
Expand All @@ -107,7 +101,7 @@ func TestBranchDeleteBranch(t *testing.T) {
func TestBranchMerge(t *testing.T) {
runner := oscommands.NewFakeRunner(t).
Expect(`git merge --no-edit "test"`, "", nil)
instance := NewBranchCommandsWithRunner(runner)
instance := buildBranchCommands(commonDeps{runner: runner})

assert.NoError(t, instance.Merge("test", MergeOpts{}))
runner.CheckForMissingCalls()
Expand Down Expand Up @@ -143,7 +137,7 @@ func TestBranchCheckout(t *testing.T) {
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := NewBranchCommandsWithRunner(s.runner)
instance := buildBranchCommands(commonDeps{runner: s.runner})
s.test(instance.Checkout("test", CheckoutOptions{Force: s.force}))
s.runner.CheckForMissingCalls()
})
Expand All @@ -154,7 +148,7 @@ func TestBranchGetBranchGraph(t *testing.T) {
runner := oscommands.NewFakeRunner(t).ExpectGitArgs([]string{
"log", "--graph", "--color=always", "--abbrev-commit", "--decorate", "--date=relative", "--pretty=medium", "test", "--",
}, "", nil)
instance := NewBranchCommandsWithRunner(runner)
instance := buildBranchCommands(commonDeps{runner: runner})
_, err := instance.GetGraph("test")
assert.NoError(t, err)
}
Expand All @@ -163,7 +157,7 @@ func TestBranchGetAllBranchGraph(t *testing.T) {
runner := oscommands.NewFakeRunner(t).ExpectGitArgs([]string{
"log", "--graph", "--all", "--color=always", "--abbrev-commit", "--decorate", "--date=relative", "--pretty=medium",
}, "", nil)
instance := NewBranchCommandsWithRunner(runner)
instance := buildBranchCommands(commonDeps{runner: runner})
err := instance.AllBranchesLogCmdObj().Run()
assert.NoError(t, err)
}
Expand Down Expand Up @@ -223,7 +217,7 @@ func TestBranchCurrentBranchName(t *testing.T) {
for _, s := range scenarios {
s := s
t.Run(s.testName, func(t *testing.T) {
instance := NewBranchCommandsWithRunner(s.runner)
instance := buildBranchCommands(commonDeps{runner: s.runner})
s.test(instance.CurrentBranchName())
s.runner.CheckForMissingCalls()
})
Expand Down
13 changes: 3 additions & 10 deletions pkg/commands/git_commands/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ import (
"strings"

"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
)

type CommitCommands struct {
*common.Common

cmd oscommands.ICmdObjBuilder
*GitCommon
}

func NewCommitCommands(
common *common.Common,
cmd oscommands.ICmdObjBuilder,
) *CommitCommands {
func NewCommitCommands(gitCommon *GitCommon) *CommitCommands {
return &CommitCommands{
Common: common,
cmd: cmd,
GitCommon: gitCommon,
}
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/commands/git_commands/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package git_commands

import (
gogit "github.com/jesseduffield/go-git/v5"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
)

type GitCommon struct {
*common.Common
cmd oscommands.ICmdObjBuilder
os *oscommands.OSCommand
dotGitDir string
repo *gogit.Repository
config *ConfigCommands
}

func NewGitCommon(
cmn *common.Common,
cmd oscommands.ICmdObjBuilder,
osCommand *oscommands.OSCommand,
dotGitDir string,
repo *gogit.Repository,
config *ConfigCommands,
) *GitCommon {
return &GitCommon{
Common: cmn,
cmd: cmd,
os: osCommand,
dotGitDir: dotGitDir,
repo: repo,
config: config,
}
}
17 changes: 3 additions & 14 deletions pkg/commands/git_commands/custom.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package git_commands

import (
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
)

type CustomCommands struct {
*common.Common

cmd oscommands.ICmdObjBuilder
*GitCommon
}

func NewCustomCommands(
common *common.Common,
cmd oscommands.ICmdObjBuilder,
) *CustomCommands {
func NewCustomCommands(gitCommon *GitCommon) *CustomCommands {
return &CustomCommands{
Common: common,
cmd: cmd,
GitCommon: gitCommon,
}
}

Expand Down
Loading

0 comments on commit 3e80a9e

Please sign in to comment.