1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-28 16:02:01 +03:00

introduce Common struct for passing around common stuff

This commit is contained in:
Jesse Duffield
2021-12-29 11:37:15 +11:00
parent b4c078d565
commit 18ab086126
15 changed files with 99 additions and 74 deletions

View File

@ -165,3 +165,7 @@ func (c *GitCommand) ResetMixed(ref string) error {
func (c *GitCommand) RenameBranch(oldName string, newName string) error {
return c.Run(c.NewCmdObj(fmt.Sprintf("git branch --move %s %s", c.OSCommand.Quote(oldName), c.OSCommand.Quote(newName))))
}
func (c *GitCommand) GetRawBranches() (string, error) {
return c.RunWithOutput(c.NewCmdObj(`git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`))
}

View File

@ -7,7 +7,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils"
)
@ -20,9 +19,8 @@ func NewDummyGitCommand() *GitCommand {
func NewDummyGitCommandWithOSCommand(osCommand *oscommands.OSCommand) *GitCommand {
newAppConfig := config.NewDummyAppConfig()
return &GitCommand{
Log: utils.NewDummyLog(),
Common: utils.NewDummyCommon(),
OSCommand: osCommand,
Tr: i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language),
Config: newAppConfig,
GitConfig: git_config.NewFakeGitConfig(map[string]string{}),
GetCmdWriter: func() io.Writer { return ioutil.Discard },

View File

@ -14,11 +14,10 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
)
// this takes something like:
@ -29,10 +28,9 @@ const CurrentBranchNameRegex = `(?m)^\*.*?([^ ]*?)\)?$`
// GitCommand is our main git interface
type GitCommand struct {
Log *logrus.Entry
*common.Common
OSCommand *oscommands.OSCommand
Repo *gogit.Repository
Tr *i18n.TranslationSet
Config config.AppConfigurer
DotGitDir string
onSuccessfulContinue func() error
@ -50,9 +48,8 @@ type GitCommand struct {
// NewGitCommand it runs git commands
func NewGitCommand(
log *logrus.Entry,
cmn *common.Common,
osCommand *oscommands.OSCommand,
tr *i18n.TranslationSet,
config config.AppConfigurer,
gitConfig git_config.IGitConfig,
) (*GitCommand, error) {
@ -65,7 +62,7 @@ func NewGitCommand(
}
var err error
if repo, err = setupRepository(gogit.PlainOpen, tr.GitconfigParseErr); err != nil {
if repo, err = setupRepository(gogit.PlainOpen, cmn.Tr.GitconfigParseErr); err != nil {
return nil, err
}
@ -75,9 +72,8 @@ func NewGitCommand(
}
gitCommand := &GitCommand{
Log: log,
Common: cmn,
OSCommand: osCommand,
Tr: tr,
Repo: repo,
Config: config,
DotGitDir: dotGitDir,
@ -86,7 +82,7 @@ func NewGitCommand(
GetCmdWriter: func() io.Writer { return ioutil.Discard },
}
gitCommand.PatchManager = patch.NewPatchManager(log, gitCommand.ApplyPatch, gitCommand.ShowFileDiff)
gitCommand.PatchManager = patch.NewPatchManager(gitCommand.Log, gitCommand.ApplyPatch, gitCommand.ShowFileDiff)
return gitCommand, nil
}

View File

@ -5,6 +5,7 @@ import (
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
)
@ -22,23 +23,31 @@ import (
// BranchListBuilder returns a list of Branch objects for the current repo
type BranchListBuilder struct {
Log *logrus.Entry
GitCommand *GitCommand
ReflogCommits []*models.Commit
*common.Common
log *logrus.Entry
getRawBranches func() (string, error)
getCurrentBranchName func() (string, string, error)
reflogCommits []*models.Commit
}
// NewBranchListBuilder builds a new branch list builder
func NewBranchListBuilder(log *logrus.Entry, gitCommand *GitCommand, reflogCommits []*models.Commit) (*BranchListBuilder, error) {
// common things: log, user config, Tr.
func NewBranchListBuilder(
cmn *common.Common,
getRawBranches func() (string, error),
getCurrentBranchName func() (string, string, error),
reflogCommits []*models.Commit,
) *BranchListBuilder {
return &BranchListBuilder{
Log: log,
GitCommand: gitCommand,
ReflogCommits: reflogCommits,
}, nil
Common: cmn,
getRawBranches: getRawBranches,
getCurrentBranchName: getCurrentBranchName,
reflogCommits: reflogCommits,
}
}
func (b *BranchListBuilder) obtainBranches() []*models.Branch {
cmdStr := `git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads`
output, err := b.GitCommand.RunWithOutput(b.GitCommand.NewCmdObj(cmdStr))
output, err := b.getRawBranches()
if err != nil {
panic(err)
}
@ -134,7 +143,7 @@ outer:
}
}
if !foundHead {
currentBranchName, currentBranchDisplayName, err := b.GitCommand.CurrentBranchName()
currentBranchName, currentBranchDisplayName, err := b.getCurrentBranchName()
if err != nil {
panic(err)
}
@ -148,8 +157,8 @@ outer:
func (b *BranchListBuilder) obtainReflogBranches() []*models.Branch {
foundBranchesMap := map[string]bool{}
re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`)
reflogBranches := make([]*models.Branch, 0, len(b.ReflogCommits))
for _, commit := range b.ReflogCommits {
reflogBranches := make([]*models.Branch, 0, len(b.reflogCommits))
for _, commit := range b.reflogCommits {
if match := re.FindStringSubmatch(commit.Name); len(match) == 3 {
recency := utils.UnixToTimeAgo(commit.UnixTimestamp)
for _, branchName := range match[1:] {

View File

@ -1,11 +1,10 @@
package oscommands
import (
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/utils"
)
// NewDummyOSCommand creates a new dummy OSCommand for testing
func NewDummyOSCommand() *OSCommand {
return NewOSCommand(utils.NewDummyLog(), config.NewDummyAppConfig())
return NewOSCommand(utils.NewDummyCommon())
}

View File

@ -9,16 +9,14 @@ import (
"path/filepath"
"strings"
"sync"
"testing"
"github.com/go-errors/errors"
"github.com/atotto/clipboard"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/secureexec"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/mgutz/str"
"github.com/sirupsen/logrus"
)
// Platform stores the os state
@ -44,9 +42,8 @@ func (self *RealCommander) Run(cmdObj ICmdObj) error {
// OSCommand holds all the os commands
type OSCommand struct {
Log *logrus.Entry
*common.Common
Platform *Platform
Config config.AppConfigurer
Command func(string, ...string) *exec.Cmd
Getenv func(string) string
@ -92,11 +89,10 @@ func NewCmdLogEntry(cmdStr string, span string, commandLine bool) CmdLogEntry {
}
// NewOSCommand os command runner
func NewOSCommand(log *logrus.Entry, config config.AppConfigurer) *OSCommand {
func NewOSCommand(common *common.Common) *OSCommand {
c := &OSCommand{
Log: log,
Common: common,
Platform: getPlatform(),
Config: config,
Command: secureexec.Command,
Getenv: os.Getenv,
removeFile: os.RemoveAll,
@ -161,7 +157,7 @@ func (c *OSCommand) FileType(path string) string {
// OpenFile opens a file with the given
func (c *OSCommand) OpenFile(filename string) error {
commandTemplate := c.Config.GetUserConfig().OS.OpenCommand
commandTemplate := c.UserConfig.OS.OpenCommand
templateValues := map[string]string{
"filename": c.Quote(filename),
}
@ -173,7 +169,7 @@ func (c *OSCommand) OpenFile(filename string) error {
// OpenLink opens a file with the given
func (c *OSCommand) OpenLink(link string) error {
c.LogCommand(fmt.Sprintf("Opening link '%s'", link), false)
commandTemplate := c.Config.GetUserConfig().OS.OpenLinkCommand
commandTemplate := c.UserConfig.OS.OpenLinkCommand
templateValues := map[string]string{
"link": c.Quote(link),
}
@ -429,14 +425,6 @@ type IRunner interface {
type RunExpectation func(ICmdObj) (string, error)
type FakeRunner struct {
expectations []RunExpectation
}
func (self *RealRunner) Run(cmdObj ICmdObj) error {
}
type RealRunner struct {
c *OSCommand
}

View File

@ -62,7 +62,7 @@ func (c *GitCommand) PrepareInteractiveRebaseCommand(baseSha string, todo string
ex := c.OSCommand.GetLazygitPath()
debug := "FALSE"
if c.OSCommand.Config.GetDebug() {
if c.Debug {
debug = "TRUE"
}

View File

@ -9,7 +9,7 @@ func (c *GitCommand) CreateLightweightTag(tagName string, commitSha string) erro
}
func (c *GitCommand) CreateAnnotatedTag(tagName, commitSha, msg string) error {
return c.RunCommand("git tag %s %s -m %s", tagName, commitSha, c.OSCommand.Quote(msg))
return c.Run(c.NewCmdObj(fmt.Sprintf("git tag %s %s -m %s", tagName, commitSha, c.OSCommand.Quote(msg))))
}
func (c *GitCommand) DeleteTag(tagName string) error {