对于使用 WSL2 签名的 git 提交,Windows 10 上的 Visual Studio Code 中没有 GPG 密码提示

39d*_*its 3 git gnupg visual-studio-code wsl-2

从 Windows 10 在 WSL2 中提交代码时,我无法在 Visual Studio Code 中使用签名提交。

问题

Windows 10 中未显示密码提示,导致 git 失败 error: gpg failed to sign the data


设置

视窗设置

带有 WSL2 和 Windows 终端的最新版 Windows 10。

Visual Studio Code 安装在 Windows 10 中,是最新版本 (1.48.0),包括最新的远程 WSL 扩展 (v0.44.4)。

我的代码在运行 Ubuntu 20.04 的 WSL2 中,所有软件包都已更新。

Visual Studio Code 是通过命令行使用code .WSL2 中的命令打开的。

Git 设置

Git (2.25.1) 安装在 WSL2 中,全局配置如下:

[user]
    name = My Name
    email = my.email@example.com
    signingkey = A1B2C3D4E5F67890
[commit]
    gpgsign = true
Run Code Online (Sandbox Code Playgroud)

git 存储库是使用 WSL2 中的命令行启动的。

GPG 设置

gnupg (2.2.19) 安装在 WSL2 中。

我有~/.gnupg/gpg.conf设置

use-agent
default-key A1B2C3D4E5F67890
Run Code Online (Sandbox Code Playgroud)

~/.gnupg/gpg-agent.conf

default-cache-ttl 34560000
max-cache-ttl 34560000
pinentry-program /usr/bin/pinentry-curses
Run Code Online (Sandbox Code Playgroud)

和我的 ~/.zprofile

export GPG_TTY=$(tty)
export GPG_AGENT_INFO=${HOME}/.gnupg/S.gpg-agent:0:1
Run Code Online (Sandbox Code Playgroud)

什么有效

如果我git commit在 Windows 终端的 WSL2 中运行 a ,它会提示我输入密码。

??????????????????????????????????????????????????????????????????
? Please enter the passphrase to unlock the OpenPGP secret key:  ?
? "My Name <my.email@example.com>"                               ?
? 4096-bit RSA key, ID A1B2C3D4E5F67890,                         ?
? created 2020-08-10.                                            ?
?                                                                ?
?                                                                ?
? Passphrase: __________________________________________________ ?
?                                                                ?
?         <OK>                                    <Cancel>       ?
??????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

然后,该密码短语会在我的 Windows 会话的其余部分中缓存,直到我重新启动我的笔记本电脑。此后的任何提交不再提示输入密码。

我现在可以使用 Visual Studio Code git UI 和 VSCode 的内置终端来提交更改。


问题

如果我重新启动我的笔记本电脑,那么在我第一次在 WSL2 中缓存 GPG 密码之前,来自 Visual Studio Code 的提交将不起作用。

也许值得注意,但如果我没有缓存密码并在 Visual Studio Code 中打开代码并尝试从内置 VSCode 终端进行提交,它也会失败。仅当首先通过 Windows 终端在 WSL2 中输入密码短语时才有效。

我很欣赏 Windows 10 和 WSL2 实际上是两个不同的系统,但是有什么方法可以让 Windows 为 WSL2 中运行的 git 显示 GPG 密码的提示?

还是我需要以完全不同的方式进行设置?我已经使用 MacOS 进行开发很长时间了,所以不确定 Windows 10 上的最佳路线可能是什么。

编辑:2020-08-17 我找到了https://github.com/diablodale/pinentry-wsl-ps1并使用脚本作为 pinentry。当密码短语未缓存时,这会起作用并在 Windows 10 中显示 GUI。但我想找到一种不依赖于可能停止运行的脚本的解决方案。

小智 29

非常感谢@39digits 的有用回答!

截至 2021 年 8 月的一些更新:

~/.gnupg/gpg-agent.conf使用最新版本的 gpg4win,您需要在 WSL2 ubuntu 的文件中设置以下配置:

pinentry-program "/mnt/c/Program Files (x86)/Gpg4win/bin/pinentry.exe"
Run Code Online (Sandbox Code Playgroud)

之后,您可能需要通过运行以下命令重新启动 GPG 代理:

pinentry-program "/mnt/c/Program Files (x86)/Gpg4win/bin/pinentry.exe"
Run Code Online (Sandbox Code Playgroud)

此外,您不再需要订阅 Windows Insider 轨道。

  • 现在,如果安装了 Windows 版 Git,`pinentry-program "/mnt/c/Program Files/Git/usr/bin/pinentry.exe"` 也可以工作。 (5认同)

39d*_*its 8

我找到了一个解决方案......但在撰写本文时它有警告。

由于 Visual Studio Code 在 Windows 10 中运行并且 git & gpg 在 WSL2 中,我想在 Windows 10 端尝试一个可视化的 pinentry。

直到我订阅了 Windows Insider 的轨道并更新到最新的 Dev Build (2004 Build 20190.1000),这才起作用。

所需步骤:

加入 Windows Insider 计划并将 Windows 10 更新到 2004 Build 20190.1000(可能适用于早期版本,但这是我最先收到的版本)。这将为 WSL2 带来一些有趣的附加功能,增强互操作性——您现在还会注意到 Windows 资源管理器中的 Linux 选项。

https://www.gpg4win.org安装 GPG4Win 。除了默认的 gnupg 之外什么都不需要,但我也安装了 Kleopatra,以防它在其他地方派上用场。

编辑~/.gnupg/gpg-agent.conf和更改 pinentry

pinentry-program "/mnt/c/Program Files (x86)/GnuPG/bin/pinentry-basic.exe"
Run Code Online (Sandbox Code Playgroud)

现在,当您被要求输入密码时,GPG GUI 就会出现。

我已经测试了重新启动机器以确保密码短语没有被缓存并且它适用于:

  • 从 WSL2 命令行提交
  • 使用 Visual Studio Code git UI 提交
  • 使用 Visual Studio Code 内置终端提交

这符合我的要求,但很高兴听到是否有任何我可能没有考虑的部分。