首页 > 解决方案 > 对于使用 WSL2 进行签名的 git 提交,Windows 10 上的 Visual Studio Code 中没有 GPG 密码提示

问题描述

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

问题

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


设置

Windows 安装程序

带有 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

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

GPG 设置

gnupg (2.2.19) 安装在 WSL2 中。

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

use-agent
default-key A1B2C3D4E5F67890

~/.gnupg/gpg-agent.conf

default-cache-ttl 34560000
max-cache-ttl 34560000
pinentry-program /usr/bin/pinentry-curses

和我的~/.zprofile

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

什么有效

如果我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>       │
└────────────────────────────────────────────────────────────────┘

然后,在我重新启动笔记本电脑之前,此密码将被缓存用于我的 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。但我想找到一个不依赖可能停止运行的脚本的解决方案。

标签: gitvisual-studio-codegnupgwsl-2

解决方案


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

由于 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"

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

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

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

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


推荐阅读