diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-02 10:34:33 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-09 14:09:53 +1100 |
commit | f503ff1ecbfda00dfa4e68e38d41aceaf9b4400c (patch) | |
tree | e113663684406ca893ebb9c223d1fddbc65abea9 /pkg/commands | |
parent | 4a1d23dc27f61e936fb3b582f02e2bba473c3b19 (diff) |
start breaking up git struct
Diffstat (limited to 'pkg/commands')
33 files changed, 1954 insertions, 1496 deletions
diff --git a/pkg/commands/branches.go b/pkg/commands/branches.go index 2276425cb..2980ae722 100644 --- a/pkg/commands/branches.go +++ b/pkg/commands/branches.go @@ -6,24 +6,47 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/utils" ) -// NewBranch create new branch -func (c *GitCommand) NewBranch(name string, base string) error { - return c.Cmd.New(fmt.Sprintf("git checkout -b %s %s", c.OSCommand.Quote(name), c.OSCommand.Quote(base))).Run() +// this takes something like: +// * (HEAD detached at 264fc6f5) +// remotes +// and returns '264fc6f5' as the second match +const CurrentBranchNameRegex = `(?m)^\*.*?([^ ]*?)\)?$` + +type BranchCommands struct { + *common.Common + + cmd oscommands.ICmdObjBuilder +} + +func NewBranchCommands( + common *common.Common, + cmd oscommands.ICmdObjBuilder, +) *BranchCommands { + return &BranchCommands{ + Common: common, + cmd: cmd, + } +} + +// New creates a new branch +func (self *BranchCommands) New(name string, base string) error { + return self.cmd.New(fmt.Sprintf("git checkout -b %s %s", self.cmd.Quote(name), self.cmd.Quote(base))).Run() } // CurrentBranchName get the current branch name and displayname. // the first returned string is the name and the second is the displayname // e.g. name is 123asdf and displayname is '(HEAD detached at 123asdf)' -func (c *GitCommand) CurrentBranchName() (string, string, error) { - branchName, err := c.Cmd.New("git symbolic-ref --short HEAD").DontLog().RunWithOutput() +func (self *BranchCommands) CurrentBranchName() (string, string, error) { + branchName, err := self.cmd.New("git symbolic-ref --short HEAD").DontLog().RunWithOutput() if err == nil && branchName != "HEAD\n" { trimmedBranchName := strings.TrimSpace(branchName) return trimmedBranchName, trimmedBranchName, nil } - output, err := c.Cmd.New("git branch --contains").DontLog().RunWithOutput() + output, err := self.cmd.New("git branch --contains").DontLog().RunWithOutput() if err != nil { return "", "", err } @@ -39,15 +62,15 @@ func (c *GitCommand) CurrentBranchName() (string, string, error) { return "HEAD", "HEAD", nil } -// DeleteBranch delete branch -func (c *GitCommand) DeleteBranch(branch string, force bool) error { +// Delete delete branch +func (self *BranchCommands) Delete(branch string, force bool) error { command := "git branch -d" if force { command = "git branch -D" } - return c.Cmd.New(fmt.Sprintf("%s %s", command, c.OSCommand.Quote(branch))).Run() + return self.cmd.New(fmt.Sprintf("%s %s", command, self.cmd.Quote(branch))).Run() } // Checkout checks out a branch (or commit), with --force if you set the force arg to true @@ -56,13 +79,13 @@ type CheckoutOptions struct { EnvVars []string } -func (c *GitCommand) Checkout(branch string, options CheckoutOptions) error { +func (self *BranchCommands) Checkout(branch string, options CheckoutOptions) error { forceArg := "" if options.Force { forceArg = " --force" } - return c.Cmd.New(fmt.Sprintf("git checkout%s %s", forceArg, c.OSCommand.Quote(branch))). + return self.cmd.New(fmt.Sprintf("git checkout%s %s", forceArg, self.cmd.Quote(branch))). // prevents git from prompting us for input which would freeze the program // TODO: see if this is actually needed here AddEnvVars("GIT_TERMINAL_PROMPT=0"). @@ -70,104 +93,84 @@ func (c *GitCommand) Checkout(branch string, options CheckoutOptions) error { Run() } -// GetBranchGraph gets the color-formatted graph of the log for the given branch +// GetGraph gets the color-formatted graph of the log for the given branch // Currently it limits the result to 100 commits, but when we get async stuff // working we can do lazy loading -func (c *GitCommand) GetBranchGraph(branchName string) (string, error) { - return c.GetBranchGraphCmdObj(branchName).DontLog().RunWithOutput() -} - -func (c *GitCommand) GetUpstreamForBranch(branchName string) (string, error) { - output, err := c.Cmd.New(fmt.Sprintf("git rev-parse --abbrev-ref --symbolic-full-name %s@{u}", c.OSCommand.Quote(branchName))).DontLog().RunWithOutput() - return strings.TrimSpace(output), err +func (self *BranchCommands) GetGraph(branchName string) (string, error) { + return self.GetGraphCmdObj(branchName).DontLog().RunWithOutput() } -func (c *GitCommand) GetBranchGraphCmdObj(branchName string) oscommands.ICmdObj { - branchLogCmdTemplate := c.UserConfig.Git.BranchLogCmd +func (self *BranchCommands) GetGraphCmdObj(branchName string) oscommands.ICmdObj { + branchLogCmdTemplate := self.UserConfig.Git.BranchLogCmd templateValues := map[string]string{ - "branchName": c.OSCommand.Quote(branchName), + "branchName": self.cmd.Quote(branchName), } - return c.Cmd.New(utils.ResolvePlaceholderString(branchLogCmdTemplate, templateValues)).DontLog() + return self.cmd.New(utils.ResolvePlaceholderString(branchLogCmdTemplate, templateValues)).DontLog() } -func (c *GitCommand) SetUpstreamBranch(upstream string) error { - return c.Cmd.New("git branch -u " + c.OSCommand.Quote(upstream)).Run() +func (self *BranchCommands) SetCurrentBranchUpstream(upstream string) error { + return self.cmd.New("git branch --set-upstream-to=" + self.cmd.Quote(upstream)).Run() } -func (c *GitCommand) SetBranchUpstream(remoteName string, remoteBranchName string, branchName string) error { - return c.Cmd.New(fmt.Sprintf("git branch --set-upstream-to=%s/%s %s", c.OSCommand.Quote(remoteName), c.OSCommand.Quote(remoteBranchName), c.OSCommand.Quote(branchName))).Run() +func (self *BranchCommands) GetUpstream(branchName string) (string, error) { + output, err := self.cmd.New(fmt.Sprintf("git rev-parse --abbrev-ref --symbolic-full-name %s@{u}", self.cmd.Quote(branchName))).DontLog().RunWithOutput() + return strings.TrimSpace(output), err } -func (c *GitCommand) GetCurrentBranchUpstreamDifferenceCount() (string, string) { - return c.GetCommitDifferences("HEAD", "HEAD@{u}") +func (self *BranchCommands) SetUpstream(remoteName string, remoteBranchName string, branchName string) error { + return self.cmd.New(fmt.Sprintf("git branch --set-upstream-to=%s/%s %s", self.cmd.Quote(remoteName), self.cmd.Quote(remoteBranchName), self.cmd.Quote(branchName))).Run() } -func (c *GitCommand) GetBranchUpstreamDifferenceCount(branchName string) (string, string) { - return c.GetCommitDifferences(branchName, branchName+"@{u}") +func (self *BranchCommands) GetCurrentBranchUpstreamDifferenceCount() (string, string) { + return self.GetCommitDifferences("HEAD", "HEAD@{u}") +} + +func (self *BranchCommands) GetUpstreamDifferenceCount(branchName string) (string, string) { + return self.GetCommitDifferences(branchName, branchName+"@{u}") } // GetCommitDifferences checks how many pushables/pullables there are for the // current branch -func (c *GitCommand) GetCommitDifferences(from, to string) (string, string) { +func (self *BranchCommands) GetCommitDifferences(from, to string) (string, string) { command := "git rev-list %s..%s --count" - pushableCount, err := c.Cmd.New(fmt.Sprintf(command, to, from)).DontLog().RunWithOutput() + pushableCount, err := self.cmd.New(fmt.Sprintf(command, to, from)).DontLog().RunWithOutput() if err != nil { return "?", "?" } - pullableCount, err := c.Cmd.New(fmt.Sprintf(command, from, to)).DontLog().RunWithOutput() + pullableCount, err := self.cmd.New(fmt.Sprintf(command, from, to)).DontLog().RunWithOutput() if err != nil { return "?", "?" } return strings.TrimSpace(pushableCount), strings.TrimSpace(pullableCount) } +func (self *BranchCommands) IsHeadDetached() bool { + err := self.cmd.New("git symbolic-ref -q HEAD").DontLog().Run() + return err != nil +} + +func (self *BranchCommands) Rename(oldName string, newName string) error { + return self.cmd.New(fmt.Sprintf("git branch --move %s %s", self.cmd.Quote(oldName), self.cmd.Quote(newName))).Run() +} + +func (self *BranchCommands) GetRawBranches() (string, error) { + return self.cmd.New(`git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`).DontLog().RunWithOutput() +} + type MergeOpts struct { FastForwardOnly bool } -// Merge merge -func (c *GitCommand) Merge(branchName string, opts MergeOpts) error { +func (self *BranchCommands) Merge(branchName string, opts MergeOpts) error { mergeArg := "" - if c.UserConfig.Git.Merging.Args != "" { - mergeArg = " " + c.UserConfig.Git.Merging.Args + if self.UserConfig.Git.Merging.Args != "" { + mergeArg = " " + self.UserConfig.Git.Merging.Args } - command := fmt.Sprintf("git merge --no-edit%s %s", mergeArg, c.OSCommand.Quote(branchName)) + command := fmt.Sprintf("git merge --no-edit%s %s", mergeArg, self.cmd.Quote(branchName)) if opts.FastForwardOnly { command = fmt.Sprintf("%s --ff-only", command) } - return c.OSCommand.Cmd.New(command).Run() -} - -// AbortMerge abort merge -func (c *GitCommand) AbortMerge() error { - return c.Cmd.New("git merge --abort").Run() -} - -func (c *GitCommand) IsHeadDetached() bool { - err := c.Cmd.New("git symbolic-ref -q HEAD").DontLog().Run() - return err != nil -} - -// ResetHardHead runs `git reset --hard` -func (c *GitCommand) ResetHard(ref string) error { - return c.Cmd.New("git reset --hard " + c.OSCommand.Quote(ref)).Run() -} - -// ResetSoft runs `git reset --soft HEAD` -func (c *GitCommand) ResetSoft(ref string) error { - return c.Cmd.New("git reset --soft " + c.OSCommand.Quote(ref)).Run() -} - -func (c *GitCommand) ResetMixed(ref string) error { - return c.Cmd.New("git reset --mixed " + c.OSCommand.Quote(ref)).Run() -} - -func (c *GitCommand) RenameBranch(oldName string, newName string) error { - return c.Cmd.New(fmt.Sprintf("git branch --move %s %s", c.OSCommand.Quote(oldName), c.OSCommand.Quote(newName))).Run() -} - -func (c *GitCommand) GetRawBranches() (string, error) { - return c.Cmd.New(`git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`).DontLog().RunWithOutput() + return self.cmd.New(command).Run() } diff --git a/pkg/commands/branches_test.go b/pkg/commands/branches_test.go index 633d4784e..6a2918a6b 100644 --- a/pkg/commands/branches_test.go +++ b/pkg/commands/branches_test.go @@ -42,7 +42,7 @@ func TestGitCommandGetCommitDifferences(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommandWithRunner(s.runner) - pushables, pullables := gitCmd.GetCommitDifferences("HEAD", "@{u}") + pushables, pullables := gitCmd.Branch.GetCommitDifferences("HEAD", "@{u}") assert.EqualValues(t, s.expectedPushables, pushables) assert.EqualValues(t, s.expectedPullables, pullables) s.runner.CheckForMissingCalls() @@ -55,7 +55,7 @@ func TestGitCommandNewBranch(t *testing.T) { Expect(`git checkout -b "test" "master"`, "", nil) gitCmd := NewDummyGitCommandWithRunner(runner) - assert.NoError(t, gitCmd.NewBranch("test", "master")) + assert.NoError(t, gitCmd.Branch.New("test", "master")) runner.CheckForMissingCalls() } @@ -90,7 +90,7 @@ func TestGitCommandDeleteBranch(t *testing.T) { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommandWithRunner(s.runner) - s.test(gitCmd.DeleteBranch("test", s.force)) + s.test(gitCmd.Branch.Delete("test", s.force)) s.runner.CheckForMissingCalls() }) } @@ -101,7 +101,7 @@ func TestGitCommandMerge(t *testing.T) { Expect(`git merge --no-edit "test"`, "", nil) gitCmd := NewDummyGitCommandWithRunner(runner) - assert.NoError(t, gitCmd.Merge("test", MergeOpts{})) + assert.NoError(t, gitCmd.Branch.Merge("test", MergeOpts{})) runner.CheckForMissingCalls() } @@ -135,7 +135,7 @@ func TestGitCommandCheckout(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommandWithRunner(s.runner) - s.test(gitCmd.Checkout("test", CheckoutOptions{Force: s.force})) + s.test(gitCmd.Branch.Checkout("test", CheckoutOptions{Force: s.force})) s.runner.CheckForMissingCalls() }) } @@ -146,7 +146,7 @@ func TestGitCommandGetBranchGraph(t *testing.T) { "log", "--graph", "--color=always", "--abbrev-commit", "--decorate", "--date=relative", "--pretty=medium", "test", "--", }, "", nil) gitCmd := NewDummyGitCommandWithRunner(runner) - _, err := gitCmd.GetBranchGraph("test") + _, err := gitCmd.Branch.GetGraph("test") assert.NoError(t, err) } @@ -215,36 +215,8 @@ func TestGitCommandCurrentBranchName(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommandWithRunner(s.runner) - s.test(gitCmd.CurrentBranchName()) + s.test(gitCmd.Branch.CurrentBranchName()) s.runner.CheckForMissingCalls() }) } } - -func TestGitCommandResetHard(t *testing.T) { - type scenario struct { - testName string - ref string - runner *oscommands.FakeCmdObjRunner - test func(error) - } - - scenarios := []scenario{ - { - "valid case", - "HEAD", - oscommands.NewFakeRunner(t). - Expect(`git reset --hard "HEAD"`, "", nil), - func(err error) { - assert.NoError(t, err) - }, - }, - } - - for _, s := range scenarios { - t.Run(s.testName, func(t *testing.T) { - gitCmd := NewDummyGitCommandWithRunner(s.runner) - s.test(gitCmd.ResetHard(s.ref)) - }) - } -} diff --git a/pkg/commands/commits.go b/pkg/commands/commits.go index ec58de297..ba61df5a5 100644 --- a/pkg/commands/commits.go +++ b/pkg/commands/commits.go @@ -4,18 +4,34 @@ import ( "fmt" "strings" - "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/common" ) -// RenameCommit renames the topmost commit with the given name -func (c *GitCommand) RenameCommit(name string) error { - return c.Cmd.New("git commit --allow-empty --amend --only -m " + c.OSCommand.Quote(name)).Run() +type CommitCommands struct { + *common.Common + + cmd oscommands.ICmdObjBuilder +} + +func NewCommitCommands( + common *common.Common, + cmd oscommands.ICmdObjBuilder, +) *CommitCommands { + return &CommitCommands{ + Common: common, + cmd: cmd, + } +} + +// RewordLastCommit renames the topmost commit with the given name +func (self *CommitCommands) RewordLastCommit(name string) error { + return self.cmd.New("git commit --allow-empty --amend --only -m " + self.cmd.Quote(name)).Run() } // ResetToCommit reset to commit -func (c *GitCommand) ResetToCommit(sha string, strength string, envVars []string) error { - return c.Cmd.New(fmt.Sprintf("git reset --%s %s", strength, sha)). +func (self *CommitCommands) ResetToCommit(sha string, strength string, envVars []string) error { + return self.cmd.New(fmt.Sprintf("git reset --%s %s", strength, sha)). // prevents git from prompting us for input which would freeze the program // TODO: see if this is actually needed here AddEnvVars("GIT_TERMINAL_PROMPT=0"). @@ -23,11 +39,11 @@ func (c *GitCommand) ResetToCommit(sha string, strength string, envVars []string Run() } -func (c *GitCommand) CommitCmdObj(message string, flags string) oscommands.ICmdObj { +func (self *CommitCommands) CommitCmdObj(message string, flags string) oscommands.ICmdObj { splitMessage := strings.Split(message, "\n") lineArgs := "" for _, line := range splitMessage { - lineArgs += fmt.Sprintf(" -m %s", c.OSCommand.Quote(line)) + lineArgs += fmt.Sprintf(" -m %s", self.cmd.Quote(line)) } flagsStr := "" @@ -35,71 +51,56 @@ func (c *GitCommand) CommitCmdObj(message string, flags string) oscommands.ICmdO flagsStr = fmt.Sprintf(" %s", flags) } - return c.Cmd.New(fmt.Sprintf("git commit%s%s", flagsStr, lineArgs)) + return self.cmd.New(fmt.Sprintf("git commit%s%s", flagsStr, lineArgs)) } // Get the subject of the HEAD commit -func (c *GitCommand) GetHeadCommitMessage() (string, error) { - message, err := c.Cmd.New("git log -1 --pretty=%s").DontLog().RunWithOutput() +func (self *CommitCommands) GetHeadCommitMessage() (string, error) { + message, err := self.cmd.New("git log -1 --pretty=%s").DontLog().RunWithOutput() return strings.TrimSpace(message), err } -func (c *GitCommand) GetCommitMessage(commitSha string) (string, error) { +func (self *CommitCommands) GetCommitMessage(commitSha string) (string, error) { cmdStr := "git rev-list --format=%B --max-count=1 " + commitSha - messageWithHeader, err := c.Cmd.New(cmdStr).DontLog().RunWithOutput() + messageWithHeader, err := self.cmd.New(cmdStr).DontLog().RunWithOutput() message := strings.Join(strings.SplitAfter(messageWithHeader, "\n")[1:], "\n") return strings.TrimSpace(message), err } -func (c *GitCommand) GetCommitMessageFirstLine(sha string) (string, error) { - return c.Cmd.New(fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", sha)).DontLog().RunWithOutput() +func (self *CommitCommands) GetCommitMessageFirstLine(sha string) (string, error) { + return self.cmd.New(fmt.Sprintf("git show --no-patch --pretty=format:%%s %s", sha)).DontLog().RunWithOutput() } // AmendHead amends HEAD with whatever is staged in your working tree -func (c *GitCommand) AmendHead() error { - return c.AmendHeadCmdObj().Run() +func (self *CommitCommands) AmendHead() error { + return self.AmendHeadCmdObj().Run() } -func (c *GitCommand) AmendHeadCmdObj() oscommands.ICmdObj { - return c.Cmd.New("git commit --amend --no-edit --allow-empty") +func (self *CommitCommands) AmendHeadCmdObj() oscommands.ICmdObj { + return self.cmd.New("git commit --amend --no-edit --allow-empty") } -func (c *GitCommand) ShowCmdObj(sha string, filterPath string) oscommands.ICmdObj { - contextSize := c.UserConfig.Git.DiffContextSize +func (self *CommitCommands) ShowCmdObj(sha string, filterPath string) oscommands.ICmdObj { + contextSize := self.UserConfig.Git.DiffContextSize filterPathArg := "" if filterPath != "" { - filterPathArg = fmt.Sprintf(" -- %s", c.OSCommand.Quote(filterPath)) + filterPathArg = fmt.Sprintf(" -- %s", self.cmd.Quote(filterPath)) } - cmdStr := fmt.Sprintf("git show --submodule --color=%s --unified=%d --no-renames --stat -p %s %s", c.colorArg(), contextSize, sha, filterPathArg) - return c.Cmd.New(cmdStr).DontLog() + cmdStr := fmt.Sprintf("git show --submodule --color=%s --unified=%d --no-renames --stat -p %s %s", self.UserConfig.Git.Paging.ColorArg, contextSize, sha, filterPathArg) + return self.cmd.New(cmdStr).DontLog() } // Revert reverts the selected commit by sha -func (c *GitCommand) Revert(sha string) error { - return c.Cmd.New(fmt.Sprintf("git revert %s", sha)).Run() +func (self *CommitCommands) Revert(sha string) error { + return self.cmd.New(fmt.Sprintf("git revert %s", sha)).Run() } -func (c *GitCommand) RevertMerge(sha string, parentNumber int) error { - return c.Cmd.New(fmt.Sprintf("git revert %s -m %d", sha, parentNumber)).Run() -} - -// CherryPickCommits begins an interactive rebase with the given shas being cherry picked onto HEAD -func (c *GitCommand) CherryPickCommits(commits []*models.Commit) error { - todo := "" - for _, commit := range commits { - todo = "pick " + commit.Sha + " " + commit.Name + "\n" + todo - } - - cmdObj, err := c.PrepareInteractiveRebaseCommand("HEAD", todo, false) - if err != nil { - return err - } - - return cmdObj.Run() +func (self *CommitCommands) RevertMerge(sha string, parentNumber int) error { + return self.cmd.New(fmt.Sprintf("git revert %s -m %d", sha, parentNumber)).Run() } // CreateFixupCommit creates a commit that fixes up a previous commit -func (c *GitCommand) CreateFixupCommit(sha string) error { - return c.Cmd.New(fmt.Sprintf("git commit --fixup=%s", sha)).Run() +func (self *CommitCommands) CreateFixupCommit(sha string) error { + return self.cmd.New(fmt.Sprintf("git commit --fixup=%s", sha)).Run() } diff --git a/pkg/commands/commits_test.go b/pkg/commands/commits_test.go index bdefc4c0e..c994a04f5 100644 --- a/pkg/commands/commits_test.go +++ b/pkg/commands/commits_test.go @@ -7,12 +7,12 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGitCommandRenameCommit(t *testing.T) { +func TestGitCommandRewordCommit(t *testing.T) { runner := oscommands.NewFakeRunner(t). ExpectGitArgs([]string{"commit", "--allow-empty", "--amend", "--only", "-m", "test"}, "", nil) gitCmd := NewDummyGitCommandWithRunner(runner) - assert.NoError(t, gitCmd.RenameCommit("test")) + assert.NoError(t, gitCmd.Commit.RewordLastCommit("test")) runner.CheckForMissingCalls() } @@ -21,7 +21,7 @@ func TestGitCommandResetToCommit(t *testing.T) { ExpectGitArgs([]string{"reset", "--hard", "78976bc"}, "", nil) gitCmd := NewDummyGitCommandWithRunner(runner) - assert.NoError(t, gitCmd.ResetToCommit("78976bc", "hard", []string{})) + assert.NoError(t, gitCmd.Commit.ResetToCommit("78976bc", "hard", []string{})) runner.CheckForMissingCalls() } @@ -57,7 +57,7 @@ func TestGitCommandCommitObj(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommand() - cmdStr := gitCmd.CommitCmdObj(s.message, s.flags).ToString() + cmdStr := gitCmd.Commit.CommitCmdObj(s.message, s.flags).ToString() assert.Equal(t, s.expected, cmdStr) }) } @@ -86,7 +86,7 @@ func TestGitCommandCreateFixupCommit(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommandWithRunner(s.runner) - s.test(gitCmd.CreateFixupCommit(s.sha)) + s.test(gitCmd.Commit.CreateFixupCommit(s.sha)) s.runner.CheckForMissingCalls() }) } @@ -125,7 +125,7 @@ func TestGitCommandShowCmdObj(t *testing.T) { t.Run(s.testName, func(t *testing.T) { gitCmd := NewDummyGitCommand() gitCmd.UserConfig.Git.DiffContextSize = s.contextSize - cmdStr := gitCmd.ShowCmdObj("1234567890", s.filterPath).ToString() + cmdStr := gitCmd.Commit.ShowCmdObj("1234567890", s.filterPath).ToString() assert.Equal(t, s.expected, cmdStr) }) } diff --git a/pkg/commands/config.go b/pkg/commands/config.go index 34126788d..db918ec86 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/config.go @@ -5,24 +5,42 @@ import ( "strconv" "strings" + "github.com/jesseduffield/lazygit/pkg/commands/git_config" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/utils" ) -func (c *GitCommand) ConfiguredPager() string { +type ConfigCommands struct { + *common.Common + + gitConfig git_config.IGitConfig +} + +func NewConfigCommands( + common *common.Common, + gitConfig git_config.IGitConfig, +) *ConfigCommands { + return &ConfigCommands{ + Common: common, + gitConfig: gitConfig, + } +} + +func (self *ConfigCommands) ConfiguredPager() string { if os.Getenv("GIT_PAGER") != "" { return os.Getenv("GIT_PAGER") } if os.Getenv("PAGER") != "" { return os.Getenv("PAGER") } - output := c.GitConfig.Get("core.pager") + output := self.gitConfig.Get("core.pager") return strings.Split(output, "\n")[0] } -func (c *GitCommand) GetPager(width int) string { - useConfig := c.UserConfig.Git.Paging.UseConfig +func (self *ConfigCommands) GetPager(width int) string { + useConfig := self.UserConfig.Git.Paging.UseConfig if useConfig { - pager := c.ConfiguredPager() + pager := self.ConfiguredPager() return strings.Split(pager, "| less")[0] } @@ -30,21 +48,35 @@ func (c *GitCommand) GetPager(width int) string { "columnWidth": strconv.Itoa(width/2 - 6), } - pagerTemplate := c.UserConfig.Git.Paging.Pager + pagerTemplate := self.UserConfig.Git.Paging.Pager return utils.ResolvePlaceholderString(pagerTemplate, templateValues) } -func (c *GitCommand) colorArg() string { - return c.UserConfig.Git.Paging.ColorArg -} - // UsingGpg tells us whether the user has gpg enabled so that we can know // whether we need to run a subprocess to allow them to enter their password -func (c *GitCommand) UsingGpg() bool { - overrideGpg := c.UserConfig.Git.OverrideGpg +func (self *ConfigCommands) UsingGpg() bool { + overrideGpg := self.UserConfig.Git.OverrideGpg if overrideGpg { return false } - return c.GitConfig.GetBool("commit.gpgsign") + return self.gitConfig.GetBool("commit.gpgsign") +} + +func (self *ConfigCommands) GetCoreEditor() string { + return self.gitConfig.Get("core.editor") +} + +// GetRemoteURL returns current repo remote url +func (self *ConfigCommands) GetRemoteURL() string { + return self.gitConfig.Get("remote.origin.url") +} + +func (self *ConfigCommands) GetShowUntrackedFiles() string { + return self.gitConfig.Get("status.showUntrackedFiles") +} + +// this determines whether the user has configured to push to the remote branch of the same name as the current or not +func (self *ConfigCommands) GetPushToCurrent() bool { + return self.gitConfig.Get("push.default") == "current" } diff --git a/pkg |