I'll help you understand how Magit handles password prompts so you can make your wrapper work properly.
Magit uses Emacs's process handling mechanisms to detect and intercept password prompts. Here's how it works:
How Magit Handles Password Prompts
Magit relies on the process-connection-type and process filters to handle subprocess I/O. The key components are:
-
Process Filter Setup: When Magit runs Git commands, it sets up process filters that monitor the output for password prompts.
-
Password Detection: The detection happens in magit-process-filter which looks for patterns that indicate password requests. This is typically done through:
;; In magit-process.el
(defun magit-process-password-prompt-regexps ()
(append magit-process-password-prompt-regexps
magit-process-find-password-functions))
The default regex patterns include things like:
"^\\(Enter \\)?[Pp]assphrase\\( for .+\\)?: ?$"
"^\\([Oo]ld \\|[Nn]ew \\|'s \\|login \\|Kerberos \\|CVS \\|UNIX \\| SMB \\|LDAP \\)?[Pp]assword\\( (again)\\)?\\( for .+\\)?: ?$"
- Password Reading: When a password prompt is detected, Magit uses
read-passwd to securely read the password from the minibuffer.
Making Your Wrapper Work
For your ssh-add wrapper to work properly with Magit, you need to ensure that:
- Use a PTY: Your wrapper should allocate a pseudo-terminal for ssh-add. Magit expects password prompts to come through a PTY connection:
#!/bin/bash
# Force PTY allocation
SSH_ASKPASS_REQUIRE=force ssh-add "$@"
-
Ensure Proper Output: Make sure ssh-add's password prompt is written to stderr and matches one of Magit's expected patterns. The standard ssh-add prompt should work, but if it doesn't, you might need to ensure it ends with : (colon space).
-
Alternative: Use SSH_ASKPASS: You could configure ssh-add to use an askpass program that Magit can intercept:
#!/bin/bash
export SSH_ASKPASS="${SSH_ASKPASS:-ssh-askpass}"
export SSH_ASKPASS_REQUIRE=prefer
ssh-add "$@" </dev/null
- Debug the Process Filter: To see what Magit is receiving, you can temporarily add debugging to the process filter:
(defun my-debug-magit-process-filter (process string)
(message "Process output: %S" string)
(magit-process-filter process string))
;; Temporarily advice the filter
(advice-add 'magit-process-filter :around
(lambda (orig-fun process string)
(message "Magit process filter sees: %S" string)
(funcall orig-fun process string)))
- Custom Password Prompt Handler: If needed, you can add your own password prompt pattern:
(add-to-list 'magit-process-password-prompt-regexps
"Enter passphrase for.*: ?$")
Recommended Solution
The most reliable approach is to ensure your wrapper script properly handles TTY allocation:
#!/bin/bash
# Your wrapper script
# Get the original ssh-keygen path (adjust as needed)
REAL_SSH_KEYGEN="/usr/bin/ssh-keygen"
# Run ssh-add with proper TTY handling
if [ -t 0 ]; then
# If we have a TTY, use it
ssh-add
else
# Force TTY allocation for password prompt
SSH_ASKPASS_REQUIRE=force ssh-add
fi
# Now run the actual ssh-keygen command
exec "$REAL_SSH_KEYGEN" "$@"
This ensures that ssh-add's password prompt will be properly detected by Magit's process filter mechanism.