This is an interesting issue! Magit uses Emacs's process handling mechanisms to capture password prompts. Let me help you understand how this works and how to fix your mixin.
How Magit Handles Password Prompts
Magit relies on several mechanisms to detect and handle password prompts:
- Process filters: Magit sets up process filters that watch for password prompt patterns
process-connection-type: Uses PTY (pseudo-terminal) connections for proper interactive behavior
- Password prompt detection: Uses regex patterns to detect when a password is being requested
The key code is in magit-process.el:
;; Magit watches for these patterns (simplified):
(defcustom magit-process-password-prompt-regexps
'("^\\(Enter \\)?[Pp]assphrase\\( for \\(RSA \\)?key '.*'\\)?: ?$"
"^\\(Enter \\)?[Pp]assword\\( for '?\\(https?://\\)?\\(?99:.*\\)'?\\)?: ?$"
"Password for '\\(https?://\\)?\\(?99:.*\\)':"
"^.*'s password: ?$"
"^Token: $" "^Yubikey for .*: ?$"
"^Enter PIN for .*: ?$"
"^Enter passphrase for .*: ?$"
"^Enter password for .*: ?$")
"List of regexps matching password prompts of Git and its subprocesses.")
The Problem with Your Mixin
When you wrap ssh-keygen with a script that calls ssh-add, the password prompt is likely:
- Not being sent to the correct file descriptor (stderr vs stdout)
- Not flushing the output properly
- Not maintaining the PTY connection properly
Solutions
Solution 1: Ensure Proper Output Handling in Your Mixin
Make sure your wrapper script properly forwards all I/O. Here's an example wrapper that should work:
#!/bin/bash
# ssh-keygen-wrapper
# First, try to add the key to ssh-agent
# Make sure to forward stderr properly
ssh-add "$SSH_KEY_PATH" 2>&1
# Then call the real ssh-keygen with all arguments
exec /usr/bin/ssh-keygen "$@"
Solution 2: Use a More Sophisticated Wrapper
Create a wrapper that properly handles PTY and signals:
#!/usr/bin/env python3
import os
import sys
import subprocess
import pty
# Try to add key to agent first
try:
# This preserves the TTY connection
subprocess.run(['ssh-add', os.environ.get('SSH_KEY_PATH', '')],
check=False)
except:
pass
# Execute the real ssh-keygen preserving the PTY
os.execvp('/usr/bin/ssh-keygen', ['/usr/bin/ssh-keygen'] + sys.argv[1:])
Solution 3: Configure SSH to Avoid the Problem
Instead of wrapping ssh-keygen, configure SSH properly:
# In ~/.ssh/config
Host *
AddKeysToAgent yes
UseKeychain yes # macOS only
IdentityFile ~/.ssh/id_rsa
Solution 4: Use Emacs's auth-source
Configure Emacs to handle SSH passwords through auth-source:
;; In your Emacs config
(setq auth-sources '("~/.authinfo.gpg"))
;; Make sure magit uses auth-source
(setq magit-process-find-password-functions
'(magit-process-password-auth-source))
Solution 5: Fix the Mixin to Work with Magit
If you must use a wrapper, ensure it works with Magit's expectations:
#!/bin/bash
# ssh-keygen-magit-wrapper
# Check if we need to add the key
if ! ssh-add -l | grep -q "$SSH_KEY_PATH"; then
# Use script to maintain PTY if available
if command -v script >/dev/null 2>&1; then
script -q -c "ssh-add $SSH_KEY_PATH" /dev/null
else
ssh-add "$SSH_KEY_PATH" </dev/tty
fi
fi
# Execute the real command
exec /usr/bin/ssh-keygen "$@"
Debugging Tips
To debug what's happening:
;; Enable magit process debugging
(setq magit-process-popup-time 0) ; Show process buffer immediately
(setq magit-process-log-max 100) ; Keep more history
;; Check if your prompt matches
(add-to-list 'magit-process-password-prompt-regexps
"Enter passphrase for.*:") ; Add your specific prompt pattern
Recommended Approach
The cleanest solution is to avoid the wrapper entirely and use SSH agent forwarding properly:
- Start ssh-agent at login
- Use
AddKeysToAgent yes in SSH config
- Let GPG/SSH handle the agent interaction naturally
Would you like me to help you implement any of these solutions or debug your specific wrapper script?