diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-08 14:17:54 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-07-08 22:54:52 +1000 |
commit | 26ca41a40e3ee79a3e84542ff6cf3fd3f2745679 (patch) | |
tree | 42df8e770a7a10147e5716d802ad90ba2e01f987 /pkg/gui/controllers/helpers | |
parent | 6c4e7ee9729ccfd65ac03073a37bd110a61be432 (diff) |
Handle pending actions properly in git commands that require credentials
I don't know if this is a hack or not: we run a git command and increment the pending action
count to 1 but at some point the command requests a username or password, so we need to prompt
the user to enter that. At that point we don't want to say that there is a pending action,
so we decrement the action count before prompting the user and then re-increment it again afterward.
Given that we panic when the counter goes below zero, it's important that it's not zero
when we run the git command (should be impossible anyway).
I toyed with a different approach using channels and a long-running goroutine that
handles all commands that request credentials but it feels over-engineered compared to this
commit's approach.
Diffstat (limited to 'pkg/gui/controllers/helpers')
-rw-r--r-- | pkg/gui/controllers/helpers/credentials_helper.go | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/pkg/gui/controllers/helpers/credentials_helper.go b/pkg/gui/controllers/helpers/credentials_helper.go index 0aed34110..20fb59052 100644 --- a/pkg/gui/controllers/helpers/credentials_helper.go +++ b/pkg/gui/controllers/helpers/credentials_helper.go @@ -1,8 +1,6 @@ package helpers import ( - "sync" - "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -20,11 +18,11 @@ func NewCredentialsHelper( } // promptUserForCredential wait for a username, password or passphrase input from the credentials popup -func (self *CredentialsHelper) PromptUserForCredential(passOrUname oscommands.CredentialType) string { - waitGroup := sync.WaitGroup{} - waitGroup.Add(1) - - userInput := "" +// We return a channel rather than returning the string directly so that the calling function knows +// when the prompt has been created (before the user has entered anything) so that it can +// note that we're now waiting on user input and lazygit isn't processing anything. +func (self *CredentialsHelper) PromptUserForCredential(passOrUname oscommands.CredentialType) <-chan string { + ch := make(chan string) self.c.OnUIThread(func() error { title, mask := self.getTitleAndMask(passOrUname) @@ -33,24 +31,19 @@ func (self *CredentialsHelper) PromptUserForCredential(passOrUname oscommands.Cr Title: title, Mask: mask, HandleConfirm: func(input string) error { - userInput = input - - waitGroup.Done() + ch <- input + "\n" return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) }, HandleClose: func() error { - waitGroup.Done() + ch <- "\n" return nil }, }) }) - // wait for username/passwords/passphrase input - waitGroup.Wait() - - return userInput + "\n" + return ch } func (self *CredentialsHelper) getTitleAndMask(passOrUname oscommands.CredentialType) (string, bool) { |