1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-20 05:19:24 +02:00

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.
This commit is contained in:
Luka Markušić 2022-11-25 22:09:02 +01:00
parent a6ebc5869e
commit b3d086bdc1
21 changed files with 124 additions and 4 deletions

View File

@ -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:

View File

@ -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<branch>.*)",
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")
},
})

View File

@ -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,

View File

@ -0,0 +1 @@
ref: refs/heads/master

View File

@ -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

View File

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

View File

@ -0,0 +1,6 @@
0000000000000000000000000000000000000000 44531ed59352b290ebe5d6bebeada267dff76fd5 CI <CI@example.com> 1669412559 +0100 commit (initial): foo
44531ed59352b290ebe5d6bebeada267dff76fd5 44531ed59352b290ebe5d6bebeada267dff76fd5 CI <CI@example.com> 1669412559 +0100 checkout: moving from master to feature/foo
44531ed59352b290ebe5d6bebeada267dff76fd5 b3518a56dbbd6df36eff0613aea30ab8e6659b26 CI <CI@example.com> 1669412559 +0100 commit: bar
b3518a56dbbd6df36eff0613aea30ab8e6659b26 b3518a56dbbd6df36eff0613aea30ab8e6659b26 CI <CI@example.com> 1669412559 +0100 checkout: moving from feature/foo to feature/bar
b3518a56dbbd6df36eff0613aea30ab8e6659b26 083b75d86104b3a7d89d9c355719b2aa9113cab9 CI <CI@example.com> 1669412559 +0100 commit: baz
083b75d86104b3a7d89d9c355719b2aa9113cab9 44531ed59352b290ebe5d6bebeada267dff76fd5 CI <CI@example.com> 1669412567 +0100 checkout: moving from feature/bar to master

View File

@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 b3518a56dbbd6df36eff0613aea30ab8e6659b26 CI <CI@example.com> 1669412559 +0100 branch: Created from HEAD
b3518a56dbbd6df36eff0613aea30ab8e6659b26 083b75d86104b3a7d89d9c355719b2aa9113cab9 CI <CI@example.com> 1669412559 +0100 commit: baz

View File

@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 44531ed59352b290ebe5d6bebeada267dff76fd5 CI <CI@example.com> 1669412559 +0100 branch: Created from HEAD
44531ed59352b290ebe5d6bebeada267dff76fd5 b3518a56dbbd6df36eff0613aea30ab8e6659b26 CI <CI@example.com> 1669412559 +0100 commit: bar

View File

@ -0,0 +1 @@
0000000000000000000000000000000000000000 44531ed59352b290ebe5d6bebeada267dff76fd5 CI <CI@example.com> 1669412559 +0100 commit (initial): foo

View File

@ -0,0 +1,3 @@
x�ÎM
Â0@a×9Eö‚Lþ† Hºê1f’
Æ–A<½=‚ÛÇ·xeíý1¬#8�]ÕFÉ>Õ‚ÑAb@Ð¥åŠäsn¢ ¢Ùx××°’Ëœ°ŠT¬- ¶è+`ÉŠ˜H<~�ûºÛy±×y¹é‡ûöÔKYûd"EçS"{`ŽzL ý“á¯ùY§9™

View File

@ -0,0 +1 @@
083b75d86104b3a7d89d9c355719b2aa9113cab9

View File

@ -0,0 +1 @@
b3518a56dbbd6df36eff0613aea30ab8e6659b26

View File

@ -0,0 +1 @@
44531ed59352b290ebe5d6bebeada267dff76fd5