From b3d086bdc1d1b95f735d8807f425f4f2225038eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Marku=C5=A1i=C4=87?= Date: Fri, 25 Nov 2022 22:09:02 +0100 Subject: [PATCH] Resolve the prompt just before using it In case a later command depends on a prompt input from a previous one we need to evaluate it only after the previous prompt has been confirmed. --- .../custom_commands/handler_creator.go | 20 ++++- .../menu_from_commands_output.go | 69 ++++++++++++++++++ pkg/integration/tests/tests.go | 1 + .../expected/repo/.git_keep/COMMIT_EDITMSG | 1 + .../expected/repo/.git_keep/FETCH_HEAD | 0 .../expected/repo/.git_keep/HEAD | 1 + .../expected/repo/.git_keep/config | 12 +++ .../expected/repo/.git_keep/description | 1 + .../expected/repo/.git_keep/index | Bin 0 -> 65 bytes .../expected/repo/.git_keep/info/exclude | 6 ++ .../expected/repo/.git_keep/logs/HEAD | 6 ++ .../.git_keep/logs/refs/heads/feature/bar | 2 + .../.git_keep/logs/refs/heads/feature/foo | 2 + .../repo/.git_keep/logs/refs/heads/master | 1 + .../08/3b75d86104b3a7d89d9c355719b2aa9113cab9 | 3 + .../44/531ed59352b290ebe5d6bebeada267dff76fd5 | Bin 0 -> 116 bytes .../4b/825dc642cb6eb9a060e54bf8d69288fbee4904 | Bin 0 -> 15 bytes .../b3/518a56dbbd6df36eff0613aea30ab8e6659b26 | Bin 0 -> 146 bytes .../repo/.git_keep/refs/heads/feature/bar | 1 + .../repo/.git_keep/refs/heads/feature/foo | 1 + .../expected/repo/.git_keep/refs/heads/master | 1 + 21 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 pkg/integration/tests/custom_commands/menu_from_commands_output.go create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/COMMIT_EDITMSG create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/FETCH_HEAD create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/HEAD create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/config create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/description create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/index create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/info/exclude create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/HEAD create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/feature/bar create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/feature/foo create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/master create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/08/3b75d86104b3a7d89d9c355719b2aa9113cab9 create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/44/531ed59352b290ebe5d6bebeada267dff76fd5 create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/b3/518a56dbbd6df36eff0613aea30ab8e6659b26 create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/feature/bar create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/feature/foo create mode 100644 test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/master diff --git a/pkg/gui/services/custom_commands/handler_creator.go b/pkg/gui/services/custom_commands/handler_creator.go index bad972bd6..4a81b08b5 100644 --- a/pkg/gui/services/custom_commands/handler_creator.go +++ b/pkg/gui/services/custom_commands/handler_creator.go @@ -67,26 +67,38 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro } resolveTemplate := self.getResolveTemplateFn(form, promptResponses, sessionState) - resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) - if err != nil { - return self.c.Error(err) - } switch prompt.Type { case "input": f = func() error { + resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) + if err != nil { + return self.c.Error(err) + } return self.inputPrompt(resolvedPrompt, wrappedF) } case "menu": f = func() error { + resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) + if err != nil { + return self.c.Error(err) + } return self.menuPrompt(resolvedPrompt, wrappedF) } case "menuFromCommand": f = func() error { + resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) + if err != nil { + return self.c.Error(err) + } return self.menuPromptFromCommand(resolvedPrompt, wrappedF) } case "confirm": f = func() error { + resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) + if err != nil { + return self.c.Error(err) + } return self.confirmPrompt(resolvedPrompt, g) } default: diff --git a/pkg/integration/tests/custom_commands/menu_from_commands_output.go b/pkg/integration/tests/custom_commands/menu_from_commands_output.go new file mode 100644 index 000000000..fdd207c4c --- /dev/null +++ b/pkg/integration/tests/custom_commands/menu_from_commands_output.go @@ -0,0 +1,69 @@ +package custom_commands + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var MenuFromCommandsOutput = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Using prompt response in menuFromCommand entries", + ExtraCmdArgs: "", + Skip: false, + SetupRepo: func(shell *Shell) { + shell. + EmptyCommit("foo"). + NewBranch("feature/foo"). + EmptyCommit("bar"). + NewBranch("feature/bar"). + EmptyCommit("baz") + }, + SetupConfig: func(cfg *config.AppConfig) { + cfg.UserConfig.CustomCommands = []config.CustomCommand{ + { + Key: "a", + Context: "localBranches", + Command: "git checkout {{ index .PromptResponses 1 }}", + Prompts: []config.CustomCommandPrompt{ + { + Type: "input", + Title: "Which git command do you want to run?", + InitialValue: "branch", + }, + { + Type: "menuFromCommand", + Title: "Branch:", + Command: `git {{ index .PromptResponses 0 }} --format='%(refname:short)'`, + Filter: "(?P.*)", + ValueFormat: `{{ .branch }}`, + LabelFormat: `{{ .branch | green }}`, + }, + }, + }, + } + }, + Run: func( + shell *Shell, + input *Input, + assert *Assert, + keys config.KeybindingConfig, + ) { + assert.WorkingTreeFileCount(0) + input.SwitchToBranchesWindow() + + input.PressKeys("a") + + assert.InPrompt() + assert.MatchCurrentViewTitle(Equals("Which git command do you want to run?")) + assert.MatchSelectedLine(Equals("branch")) + input.Confirm() + + assert.InMenu() + assert.MatchCurrentViewTitle(Equals("Branch:")) + input.NextItem() + input.NextItem() + assert.MatchSelectedLine(Equals("master")) + input.Confirm() + + assert.CurrentBranchName("master") + }, +}) diff --git a/pkg/integration/tests/tests.go b/pkg/integration/tests/tests.go index f097b032d..6c074bb10 100644 --- a/pkg/integration/tests/tests.go +++ b/pkg/integration/tests/tests.go @@ -39,6 +39,7 @@ var tests = []*components.IntegrationTest{ custom_commands.Basic, custom_commands.FormPrompts, custom_commands.MenuFromCommand, + custom_commands.MenuFromCommandsOutput, custom_commands.MultiplePrompts, file.DirWithUntrackedFile, interactive_rebase.AmendMerge, diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/COMMIT_EDITMSG b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/COMMIT_EDITMSG new file mode 100644 index 000000000..76018072e --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/COMMIT_EDITMSG @@ -0,0 +1 @@ +baz diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/FETCH_HEAD b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/FETCH_HEAD new file mode 100644 index 000000000..e69de29bb diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/HEAD b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/config b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/config new file mode 100644 index 000000000..2b89b8630 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/config @@ -0,0 +1,12 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true +[user] + email = CI@example.com + name = CI +[commit] + gpgSign = false +[protocol "file"] + allow = always diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/description b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/index b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/index new file mode 100644 index 0000000000000000000000000000000000000000..65d675154f23ffb2d0196e017d44a5e7017550f5 GIT binary patch literal 65 zcmZ?q402{*U|<4bhL9jvS0E+HV4z^Y<=qr}%;|LA&IJiiy? 1669412559 +0100 commit (initial): foo +44531ed59352b290ebe5d6bebeada267dff76fd5 44531ed59352b290ebe5d6bebeada267dff76fd5 CI 1669412559 +0100 checkout: moving from master to feature/foo +44531ed59352b290ebe5d6bebeada267dff76fd5 b3518a56dbbd6df36eff0613aea30ab8e6659b26 CI 1669412559 +0100 commit: bar +b3518a56dbbd6df36eff0613aea30ab8e6659b26 b3518a56dbbd6df36eff0613aea30ab8e6659b26 CI 1669412559 +0100 checkout: moving from feature/foo to feature/bar +b3518a56dbbd6df36eff0613aea30ab8e6659b26 083b75d86104b3a7d89d9c355719b2aa9113cab9 CI 1669412559 +0100 commit: baz +083b75d86104b3a7d89d9c355719b2aa9113cab9 44531ed59352b290ebe5d6bebeada267dff76fd5 CI 1669412567 +0100 checkout: moving from feature/bar to master diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/feature/bar b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/feature/bar new file mode 100644 index 000000000..77d5c6099 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/feature/bar @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 b3518a56dbbd6df36eff0613aea30ab8e6659b26 CI 1669412559 +0100 branch: Created from HEAD +b3518a56dbbd6df36eff0613aea30ab8e6659b26 083b75d86104b3a7d89d9c355719b2aa9113cab9 CI 1669412559 +0100 commit: baz diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/feature/foo b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/feature/foo new file mode 100644 index 000000000..a09bd9f99 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/feature/foo @@ -0,0 +1,2 @@ +0000000000000000000000000000000000000000 44531ed59352b290ebe5d6bebeada267dff76fd5 CI 1669412559 +0100 branch: Created from HEAD +44531ed59352b290ebe5d6bebeada267dff76fd5 b3518a56dbbd6df36eff0613aea30ab8e6659b26 CI 1669412559 +0100 commit: bar diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/master b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/master new file mode 100644 index 000000000..d1b8c187e --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 44531ed59352b290ebe5d6bebeada267dff76fd5 CI 1669412559 +0100 commit (initial): foo diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/08/3b75d86104b3a7d89d9c355719b2aa9113cab9 b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/08/3b75d86104b3a7d89d9c355719b2aa9113cab9 new file mode 100644 index 000000000..e428309a2 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/08/3b75d86104b3a7d89d9c355719b2aa9113cab9 @@ -0,0 +1,3 @@ +xM +0@a9EL H1f +ƖA<=Ƿxe1#8]F>ՂAb@sn xװ˜T-+`ɊH<~yyKYd"ES"{`zL Y9 \ No newline at end of file diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/44/531ed59352b290ebe5d6bebeada267dff76fd5 b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/44/531ed59352b290ebe5d6bebeada267dff76fd5 new file mode 100644 index 0000000000000000000000000000000000000000..07469bc40aa39d674231bb80fe5c2f075bf34129 GIT binary patch literal 116 zcmV-)0E_>40gcT;3IZ_1$9PVN zOsBb<0oP1vYr@p3Sk;1pmbmu3iBitJnz15>@|h3cVOin2tatmC@qXE*`M5z8QI3?7 W!WkoCIO;K(_9qYBJ^TQIpDS$4z&geN literal 0 HcmV?d00001 diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/objects/4b/825dc642cb6eb9a060e54bf8d69288fbee4904 new file mode 100644 index 0000000000000000000000000000000000000000..adf64119a33d7621aeeaa505d30adb58afaa5559 GIT binary patch literal 15 Wcmbmr;+SG6d?Q^|}z3#-Hg3`!_96plWGDykR(&7Qixhef2xX`wig7au`IsjwVKC3&7p ztuxm`CVj?Z>vmb~by@HFO>KYaC2#F!9dktIiK0D&12Bg@riT8R8%f>#0AjH@&9eYN Ay#N3J literal 0 HcmV?d00001 diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/feature/bar b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/feature/bar new file mode 100644 index 000000000..9dfead794 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/feature/bar @@ -0,0 +1 @@ +083b75d86104b3a7d89d9c355719b2aa9113cab9 diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/feature/foo b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/feature/foo new file mode 100644 index 000000000..0fd10f171 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/feature/foo @@ -0,0 +1 @@ +b3518a56dbbd6df36eff0613aea30ab8e6659b26 diff --git a/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/master b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/master new file mode 100644 index 000000000..203202599 --- /dev/null +++ b/test/integration_new/custom_commands/menu_from_commands_output/expected/repo/.git_keep/refs/heads/master @@ -0,0 +1 @@ +44531ed59352b290ebe5d6bebeada267dff76fd5