1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-06 22:33:07 +02:00
Files
.devcontainer
.github
.vscode
cmd
demo
docs
pkg
app
cheatsheet
commands
common
config
constants
env
fakes
gui
context
controllers
helpers
amend_helper.go
app_status_helper.go
bisect_helper.go
branches_helper.go
cherry_pick_helper.go
commits_helper.go
commits_helper_test.go
confirmation_helper.go
credentials_helper.go
diff_helper.go
files_helper.go
fixup_helper.go
gpg_helper.go
helpers.go
host_helper.go
inline_status_helper.go
merge_and_rebase_helper.go
merge_conflicts_helper.go
mode_helper.go
patch_building_helper.go
record_directory_helper.go
refresh_helper.go
refs_helper.go
repos_helper.go
search_helper.go
snake_helper.go
staging_helper.go
sub_commits_helper.go
suggestions_helper.go
tags_helper.go
update_helper.go
upstream_helper.go
upstream_helper_test.go
view_helper.go
window_arrangement_helper.go
window_arrangement_helper_test.go
window_helper.go
working_tree_helper.go
worktree_helper.go
attach.go
base_controller.go
basic_commits_controller.go
bisect_controller.go
branches_controller.go
command_log_controller.go
commit_description_controller.go
commit_message_controller.go
commits_files_controller.go
common.go
confirmation_controller.go
context_lines_controller.go
custom_command_action.go
custom_patch_options_menu_action.go
diffing_menu_action.go
files_controller.go
filter_controller.go
filtering_menu_action.go
git_flow_controller.go
global_controller.go
jump_to_side_window_controller.go
list_controller.go
list_controller_trait.go
local_commits_controller.go
local_commits_controller_test.go
menu_controller.go
merge_conflicts_controller.go
options_menu_action.go
patch_building_controller.go
patch_explorer_controller.go
quit_actions.go
reflog_commits_controller.go
remote_branches_controller.go
remotes_controller.go
screen_mode_actions.go
scroll_off_margin.go
scroll_off_margin_test.go
search_controller.go
search_prompt_controller.go
side_window_controller.go
snake_controller.go
staging_controller.go
stash_controller.go
status_controller.go
sub_commits_controller.go
submodules_controller.go
suggestions_controller.go
switch_to_diff_files_controller.go
switch_to_sub_commits_controller.go
sync_controller.go
tags_controller.go
toggle_whitespace_action.go
types.go
undo_controller.go
vertical_scroll_controller.go
workspace_reset_controller.go
worktree_options_controller.go
worktrees_controller.go
filetree
keybindings
mergeconflicts
modes
patch_exploring
popup
presentation
services
status
style
types
background.go
command_log_panel.go
context.go
context_config.go
controllers.go
dummies.go
editors.go
extras_panel.go
global_handlers.go
gui.go
gui_common.go
gui_driver.go
information_panel.go
keybindings.go
layout.go
main_panels.go
menu_panel.go
options_map.go
pty.go
pty_windows.go
recent_repos_panel.go
tasks_adapter.go
test_mode.go
view_helpers.go
views.go
i18n
integration
jsonschema
logs
snake
tasks
theme
updates
utils
schema
scripts
test
vendor
.editorconfig
.gitignore
.golangci.yml
.goreleaser.yml
CODE-OF-CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE
Makefile
README.md
go.mod
go.sum
main.go
lazygit/pkg/gui/controllers/helpers/credentials_helper.go
Jesse Duffield 26ca41a40e 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.
2023-07-08 22:54:52 +10:00

64 lines
1.7 KiB
Go

package helpers
import (
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type CredentialsHelper struct {
c *HelperCommon
}
func NewCredentialsHelper(
c *HelperCommon,
) *CredentialsHelper {
return &CredentialsHelper{
c: c,
}
}
// promptUserForCredential wait for a username, password or passphrase input from the credentials popup
// 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)
return self.c.Prompt(types.PromptOpts{
Title: title,
Mask: mask,
HandleConfirm: func(input string) error {
ch <- input + "\n"
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
},
HandleClose: func() error {
ch <- "\n"
return nil
},
})
})
return ch
}
func (self *CredentialsHelper) getTitleAndMask(passOrUname oscommands.CredentialType) (string, bool) {
switch passOrUname {
case oscommands.Username:
return self.c.Tr.CredentialsUsername, false
case oscommands.Password:
return self.c.Tr.CredentialsPassword, true
case oscommands.Passphrase:
return self.c.Tr.CredentialsPassphrase, true
case oscommands.PIN:
return self.c.Tr.CredentialsPIN, true
}
// should never land here
panic("unexpected credential request")
}