Fix loading customCommands from per-repo config file (#3835)

- **PR Description**

Any newly loaded custom command coming from the per-repo config file should add
to the global ones (or override an existing one in the global one), rather than
replace all global ones.
This commit is contained in:
Stefan Haller 2024-08-24 11:04:56 +02:00 committed by GitHub
commit 8e71df3e53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 66 additions and 1 deletions

View file

@ -196,10 +196,14 @@ func loadUserConfig(configFiles []*ConfigFile, base *UserConfig) (*UserConfig, e
return nil, err
}
existingCustomCommands := base.CustomCommands
if err := yaml.Unmarshal(content, base); err != nil {
return nil, fmt.Errorf("The config at `%s` couldn't be parsed, please inspect it before opening up an issue.\n%w", path, err)
}
base.CustomCommands = append(base.CustomCommands, existingCustomCommands...)
if err := base.Validate(); err != nil {
return nil, fmt.Errorf("The config at `%s` has a validation error.\n%w", path, err)
}

View file

@ -30,7 +30,7 @@ func (self *FileSystem) FileContent(path string, matcher *TextMatcher) {
self.assertWithRetries(func() (bool, string) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false, fmt.Sprintf("Expected path '%s' to not exist, but it does", path)
return false, fmt.Sprintf("Expected path '%s' to exist, but it does not", path)
}
output, err := os.ReadFile(path)

View file

@ -0,0 +1,60 @@
package config
import (
"path/filepath"
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var CustomCommandsInPerRepoConfig = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Custom commands in per-repo config add to the global ones instead of replacing them",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(cfg *config.AppConfig) {
otherRepo, _ := filepath.Abs("../other")
cfg.GetAppState().RecentRepos = []string{otherRepo}
cfg.GetUserConfig().CustomCommands = []config.CustomCommand{
{
Key: "X",
Context: "global",
Command: "printf 'global X' > file.txt",
},
{
Key: "Y",
Context: "global",
Command: "printf 'global Y' > file.txt",
},
}
},
SetupRepo: func(shell *Shell) {
shell.CloneNonBare("other")
shell.CreateFile("../other/.git/lazygit.yml", `
customCommands:
- key: Y
context: global
command: printf 'local Y' > file.txt
- key: Z
context: global
command: printf 'local Z' > file.txt`)
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.GlobalPress(keys.Universal.OpenRecentRepos)
t.ExpectPopup().Menu().Title(Equals("Recent repositories")).
Lines(
Contains("other").IsSelected(),
Contains("Cancel"),
).Confirm()
t.Views().Status().Content(Contains("other → master"))
t.GlobalPress("X")
t.FileSystem().FileContent("../other/file.txt", Equals("global X"))
t.GlobalPress("Y")
t.FileSystem().FileContent("../other/file.txt", Equals("local Y"))
t.GlobalPress("Z")
t.FileSystem().FileContent("../other/file.txt", Equals("local Z"))
},
})

View file

@ -110,6 +110,7 @@ var tests = []*components.IntegrationTest{
commit.Staged,
commit.StagedWithoutHooks,
commit.Unstaged,
config.CustomCommandsInPerRepoConfig,
config.RemoteNamedStar,
conflicts.Filter,
conflicts.ResolveExternally,