From a2bdab21357483cc3f95b1c868cb749d35bf6170 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 31 May 2023 14:09:23 +0200 Subject: [PATCH 1/4] Remove unused fetch options RemoteName and BranchName These were never used, since there are separate functions for fetching a remote and for fast-forwarding a branch. --- pkg/commands/git_commands/sync.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pkg/commands/git_commands/sync.go b/pkg/commands/git_commands/sync.go index 6b336d175..515d0e4d8 100644 --- a/pkg/commands/git_commands/sync.go +++ b/pkg/commands/git_commands/sync.go @@ -50,16 +50,11 @@ func (self *SyncCommands) Push(opts PushOpts) error { type FetchOptions struct { Background bool - RemoteName string - BranchName string } // Fetch fetch git repo func (self *SyncCommands) Fetch(opts FetchOptions) error { - cmdArgs := NewGitCmd("fetch"). - ArgIf(opts.RemoteName != "", opts.RemoteName). - ArgIf(opts.BranchName != "", opts.BranchName). - ToArgv() + cmdArgs := NewGitCmd("fetch").ToArgv() cmdObj := self.cmd.New(cmdArgs) if opts.Background { From ee4b9d20b14d1b856fed3889f574a4e8f0fa661f Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 30 May 2023 18:54:32 +0200 Subject: [PATCH 2/4] Extract a FetchCmdObj function so that we can test it No change in behavior. --- pkg/commands/git_commands/sync.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/commands/git_commands/sync.go b/pkg/commands/git_commands/sync.go index 515d0e4d8..c56422029 100644 --- a/pkg/commands/git_commands/sync.go +++ b/pkg/commands/git_commands/sync.go @@ -53,7 +53,7 @@ type FetchOptions struct { } // Fetch fetch git repo -func (self *SyncCommands) Fetch(opts FetchOptions) error { +func (self *SyncCommands) FetchCmdObj(opts FetchOptions) oscommands.ICmdObj { cmdArgs := NewGitCmd("fetch").ToArgv() cmdObj := self.cmd.New(cmdArgs) @@ -62,7 +62,12 @@ func (self *SyncCommands) Fetch(opts FetchOptions) error { } else { cmdObj.PromptOnCredentialRequest() } - return cmdObj.WithMutex(self.syncMutex).Run() + return cmdObj.WithMutex(self.syncMutex) +} + +func (self *SyncCommands) Fetch(opts FetchOptions) error { + cmdObj := self.FetchCmdObj(opts) + return cmdObj.Run() } type PullOptions struct { From 697157f5d57ef674eca2d2ee5236d6f2736b2f61 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 30 May 2023 20:30:47 +0200 Subject: [PATCH 3/4] Add tests for Fetch --- pkg/commands/git_commands/sync_test.go | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pkg/commands/git_commands/sync_test.go b/pkg/commands/git_commands/sync_test.go index 23058eb92..07fe8f932 100644 --- a/pkg/commands/git_commands/sync_test.go +++ b/pkg/commands/git_commands/sync_test.go @@ -92,3 +92,40 @@ func TestSyncPush(t *testing.T) { }) } } + +func TestSyncFetch(t *testing.T) { + type scenario struct { + testName string + opts FetchOptions + test func(oscommands.ICmdObj) + } + + scenarios := []scenario{ + { + testName: "Fetch in foreground", + opts: FetchOptions{Background: false}, + test: func(cmdObj oscommands.ICmdObj) { + assert.True(t, cmdObj.ShouldLog()) + assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT) + assert.Equal(t, cmdObj.Args(), []string{"git", "fetch"}) + }, + }, + { + testName: "Fetch in background", + opts: FetchOptions{Background: true}, + test: func(cmdObj oscommands.ICmdObj) { + assert.False(t, cmdObj.ShouldLog()) + assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL) + assert.Equal(t, cmdObj.Args(), []string{"git", "fetch"}) + }, + }, + } + + for _, s := range scenarios { + s := s + t.Run(s.testName, func(t *testing.T) { + instance := buildSyncCommands(commonDeps{}) + s.test(instance.FetchCmdObj(s.opts)) + }) + } +} From 31a2ea1f19184a10239d8c02314d46321a0e0ae3 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 1 Mar 2023 09:09:35 +0100 Subject: [PATCH 4/4] Add --all to "git fetch" command when not fetching a specific remote --- docs/Config.md | 1 + pkg/commands/git_commands/sync.go | 4 ++- pkg/commands/git_commands/sync_test.go | 38 +++++++++++++++++++++----- pkg/config/user_config.go | 2 ++ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index 7424dd222..052d65957 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -94,6 +94,7 @@ git: mainBranches: [master, main] autoFetch: true autoRefresh: true + fetchAll: true # Pass --all flag when running git fetch. Set to false to fetch only origin (or the current branch's upstream remote if there is one) branchLogCmd: 'git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --' allBranchesLogCmd: 'git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium' overrideGpg: false # prevents lazygit from spawning a separate process when using GPG diff --git a/pkg/commands/git_commands/sync.go b/pkg/commands/git_commands/sync.go index c56422029..dc0a0c68c 100644 --- a/pkg/commands/git_commands/sync.go +++ b/pkg/commands/git_commands/sync.go @@ -54,7 +54,9 @@ type FetchOptions struct { // Fetch fetch git repo func (self *SyncCommands) FetchCmdObj(opts FetchOptions) oscommands.ICmdObj { - cmdArgs := NewGitCmd("fetch").ToArgv() + cmdArgs := NewGitCmd("fetch"). + ArgIf(self.UserConfig.Git.FetchAll, "--all"). + ToArgv() cmdObj := self.cmd.New(cmdArgs) if opts.Background { diff --git a/pkg/commands/git_commands/sync_test.go b/pkg/commands/git_commands/sync_test.go index 07fe8f932..f5eb0d403 100644 --- a/pkg/commands/git_commands/sync_test.go +++ b/pkg/commands/git_commands/sync_test.go @@ -95,15 +95,17 @@ func TestSyncPush(t *testing.T) { func TestSyncFetch(t *testing.T) { type scenario struct { - testName string - opts FetchOptions - test func(oscommands.ICmdObj) + testName string + opts FetchOptions + fetchAllConfig bool + test func(oscommands.ICmdObj) } scenarios := []scenario{ { - testName: "Fetch in foreground", - opts: FetchOptions{Background: false}, + testName: "Fetch in foreground (all=false)", + opts: FetchOptions{Background: false}, + fetchAllConfig: false, test: func(cmdObj oscommands.ICmdObj) { assert.True(t, cmdObj.ShouldLog()) assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT) @@ -111,20 +113,42 @@ func TestSyncFetch(t *testing.T) { }, }, { - testName: "Fetch in background", - opts: FetchOptions{Background: true}, + testName: "Fetch in foreground (all=true)", + opts: FetchOptions{Background: false}, + fetchAllConfig: true, + test: func(cmdObj oscommands.ICmdObj) { + assert.True(t, cmdObj.ShouldLog()) + assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.PROMPT) + assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all"}) + }, + }, + { + testName: "Fetch in background (all=false)", + opts: FetchOptions{Background: true}, + fetchAllConfig: false, test: func(cmdObj oscommands.ICmdObj) { assert.False(t, cmdObj.ShouldLog()) assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL) assert.Equal(t, cmdObj.Args(), []string{"git", "fetch"}) }, }, + { + testName: "Fetch in background (all=true)", + opts: FetchOptions{Background: true}, + fetchAllConfig: true, + test: func(cmdObj oscommands.ICmdObj) { + assert.False(t, cmdObj.ShouldLog()) + assert.Equal(t, cmdObj.GetCredentialStrategy(), oscommands.FAIL) + assert.Equal(t, cmdObj.Args(), []string{"git", "fetch", "--all"}) + }, + }, } for _, s := range scenarios { s := s t.Run(s.testName, func(t *testing.T) { instance := buildSyncCommands(commonDeps{}) + instance.UserConfig.Git.FetchAll = s.fetchAllConfig s.test(instance.FetchCmdObj(s.opts)) }) } diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 9f46948b5..295c8178d 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -81,6 +81,7 @@ type GitConfig struct { SkipHookPrefix string `yaml:"skipHookPrefix"` AutoFetch bool `yaml:"autoFetch"` AutoRefresh bool `yaml:"autoRefresh"` + FetchAll bool `yaml:"fetchAll"` BranchLogCmd string `yaml:"branchLogCmd"` AllBranchesLogCmd string `yaml:"allBranchesLogCmd"` OverrideGpg bool `yaml:"overrideGpg"` @@ -453,6 +454,7 @@ func GetDefaultConfig() *UserConfig { MainBranches: []string{"master", "main"}, AutoFetch: true, AutoRefresh: true, + FetchAll: true, BranchLogCmd: "git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --", AllBranchesLogCmd: "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium", DisableForcePushing: false,