e5b9b80008a2fd71b441ef39fe620ed47dad82e5

Author
Dapeng Gao <31944689+dpgao@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

fix(gpg-agent): correctly overwrite `$SSH_AUTH_SOCK` and other improvements (#7059)

Diff

 1diff --git a/plugins/gpg-agent/README.md b/plugins/gpg-agent/README.md
 2index 9c1e6d2cca73f15f286820a7dc3cb9f25282309f..8eeb94f925350f61c53178392377e468d8544f4d 100644
 3--- a/plugins/gpg-agent/README.md
 4+++ b/plugins/gpg-agent/README.md
 5@@ -2,7 +2,7 @@
 6 
 7 Enables [GPG's gpg-agent](https://www.gnupg.org/documentation/manuals/gnupg/) if it is not running.
 8 
 9-To use it, add gpg-agent to the plugins array of your zshrc file:
10+To use it, add `gpg-agent` to the plugins array of your zshrc file:
11 
12 ```zsh
13 plugins=(... gpg-agent)
14diff --git a/plugins/gpg-agent/gpg-agent.plugin.zsh b/plugins/gpg-agent/gpg-agent.plugin.zsh
15index 3e24c25278a3d708da7e2db068e9fb54f8255dfa..0adc8de5dba08cb654c0e950ae650121875dba1e 100644
16--- a/plugins/gpg-agent/gpg-agent.plugin.zsh
17+++ b/plugins/gpg-agent/gpg-agent.plugin.zsh
18@@ -1,16 +1,17 @@
19-# Enable gpg-agent if it is not running-
20-# --use-standard-socket will work from version 2 upwards
21-
22-AGENT_SOCK=$(gpgconf --list-dirs | grep agent-socket | cut -d : -f 2)
23-
24-if [[ ! -S $AGENT_SOCK ]]; then
25-  gpg-agent --daemon --use-standard-socket &>/dev/null
26-fi
27 export GPG_TTY=$TTY
28 
29-# Set SSH to use gpg-agent if it's enabled
30-GNUPGCONFIG="${GNUPGHOME:-"$HOME/.gnupg"}/gpg-agent.conf"
31-if [[ -r $GNUPGCONFIG ]] && command grep -q enable-ssh-support "$GNUPGCONFIG"; then
32-  export SSH_AUTH_SOCK="$AGENT_SOCK.ssh"
33+# Fix for passphrase prompt on the correct tty
34+# See https://www.gnupg.org/documentation/manuals/gnupg/Agent-Options.html#option-_002d_002denable_002dssh_002dsupport
35+function _gpg-agent_update-tty_preexec {
36+  gpg-connect-agent updatestartuptty /bye &>/dev/null
37+}
38+autoload -U add-zsh-hook
39+add-zsh-hook preexec _gpg-agent_update-tty_preexec
40+
41+# If enable-ssh-support is set, fix ssh agent integration
42+if [[ $(gpgconf --list-options gpg-agent | awk -F: '$1=="enable-ssh-support" {print $10}') = 1 ]]; then
43   unset SSH_AGENT_PID
44+  if [[ "${gnupg_SSH_AUTH_SOCK_by:-0}" -ne $$ ]]; then
45+    export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
46+  fi
47 fi