Visual Studio 代码中的“Git:gpg 未能对数据进行签名”

djw*_*ver 9 linux git gnupg visual-studio-code

在全新安装 Linux 后,我正在尝试设置我的环境,但Git: gpg failed to sign the data在本地提交更改时不断收到错误消息。我正在使用 Visual Studio Code,专有的,而不是开源版本。

.gitconfig:

[user]
    name = djweaver-dev
    email = djweaver@djweaver.dev
    signingkey = 37A0xxxx...
[core]
    excludesfile = /home/dweaver/.gitignore_global
[commit]
    gpgSign = true
Run Code Online (Sandbox Code Playgroud)

哎呀。此外,我找不到复制输出日志的方法,也找不到该日志的位置,所以这是一张图片:

输出日志

到目前为止我采取的步骤:

  • 在 gnugp 中生成新密钥 (RSA 4096)
  • 将签名密钥添加到全局 .gitconfig
  • 设置"git.enableCommitSigning": true在Visual Studio代码设置
  • 从 github克隆我的 repo

通常,当我过去提交时,我会在提交时收到一个请求 GPG 身份验证的对话框。我现在没有得到这个,只是错误对话框。

更新:好的,现在我真的困惑。我重新启动了 vscode(在这个过程中我不是第一次这样做了),瞧,它起作用了。我唯一能想到的可能是我以某种方式对目录进行了分类?无论哪种方式,它现在都有效。

更新:奇怪的是,在全新的 Arch 安装后差不多一个月后,我又回到了同样的问题。我已经尝试了我能在这个网站上找到的所有东西,但没有任何效果。

我试过添加export GPG_TTY=$(tty)到 .bash_profile 和 .bashrc

Git日志:

Looking for git in: git
Using git 2.26.2 from git
> git rev-parse --show-toplevel
> git rev-parse --git-dir
Open repository: /home/dw/dev/website
> git status -z -u
> git symbolic-ref --short HEAD
> git rev-parse master
> git rev-parse --symbolic-full-name master@{u}
> git rev-list --left-right master...refs/remotes/origin/master
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose
Failed to watch ref '/home/dw/dev/website/.git/refs/remotes/origin/master', is most likely packed.
Error: ENOENT: no such file or directory, watch '/home/dw/dev/website/.git/refs/remotes/origin/master'
    at FSWatcher.start (internal/fs/watchers.js:165:26)
    at Object.watch (fs.js:1270:11)
    at Object.t.watch (/usr/lib/code/extensions/git/dist/main.js:1:604919)
    at T.updateTransientWatchers (/usr/lib/code/extensions/git/dist/main.js:1:83965)
    at e.fire (/usr/lib/code/out/vs/workbench/services/extensions/node/extensionHostProcess.js:46:87)
    at e.updateModelState (/usr/lib/code/extensions/git/dist/main.js:1:103179)
> git config --get commit.template
> git check-ignore -v -z --stdin
> git check-ignore -v -z --stdin
> git commit --quiet --allow-empty-message --file - -S
error: gpg failed to sign the data
fatal: failed to write commit object
> git config --get-all user.name
> git config --get-all user.email
Run Code Online (Sandbox Code Playgroud)

与上次相同的配置,user.name并且user.email都匹配我一直在尝试的每个键...user.signingkey匹配。不知道还有什么地方可以使用这个,因为我已经在新初始化的本地存储库以及我从 github 中使用官方 MS vscode (AUR) 和 OSS 版本在 vscode 终端仿真器中提取的存储库中尝试过它以及具有相同的结果侏儒终端,从而它具有为任一个git事情或此GnuGP事情。

我注意到的是,在没有签名的情况下提交后,它将立即工作:我第一次收到我的密钥密码的提示,然后它在随后的提交中工作,直到看似随机的几分钟后,它就不再工作了并且必须重复该过程。

有一些 macos 用户发布了关于gpg-agent在后台运行停止的帖子,并为他们修复了它,但是,我看到:

[dw@dwLinux website]$ gpg-agent
gpg-agent[2870]: gpg-agent running and available
Run Code Online (Sandbox Code Playgroud)

有趣的是,通过这样做,echo "test" | gpg --clearsign我得到了相同的结果:它在短时间内有效,然后我无法再签名。

更新

好的,尝试解决这个问题的第 2 天。为了排除此处描述的 gpg-agent 理论,我遵循了有关如何使用Arch Linux Wiki$ gpg-connect-agent reloadagent /bye上演示的命令重新加载 gpg-agent 的说明

这没有效果

因此,由于我可以在 vscode 官方、oss 代码和 vscodium 以及 bash 上重现这个问题,我认为这可能是与权限相关的问题,因为 linux 的许多问题通常是. 我将我的用户添加到各种组,包括 root,这也没有影响,所以我想我可以安全地排除以下情况:

  • VS代码
  • GnuGP
  • gpg-agent
  • Linux 权限

因此,我的下一个重点是配置文件本身,但正如在凭据与密钥匹配之前所述.gitconfig,我.bash_profile已正确配置了export GPG_TTY=$(tty).

在这一个有趣的注意从官方GnuPG的文档显示他们的方式之间的语法discrepency,你被指示的方式追加这对GitHub的文档对.bash_profile这里

来自 GnuPG: “最常见的原因是环境变量 GPG_TTY 未正确设置。确保它已设置为真实的 tty 设备,而不仅仅是 '/dev/tty';即 'GPG_TTY= tty' 显然是错误的;你想要的是 'GPG_TTY=tty' - 注意后面的勾号。还要确保这个环境变量被导出,也就是说你应该用'export GPG_TTY'来跟进设置

正如我$(whatever)在 bash 中理解的那样是执行命令,但为了安全起见,我使用两种方式附加了 .bash_profile 并且都没有解决问题。

最后一件事

这篇文章进程化和GPG访问正在由其他应用程序(如像VSCode一个IDE),它解释了如何我开始的时候不被提供关于GPG代理认证的用户会谈暂时犯下一个随机文件或做后签署的提交echo "test" | gpg --clearsign和正在通过身份验证……但是,就像该主题的大多数其他“解决方案”一样,他们透露他们最终要做的就是将export GPG_TTY=$(tty)其添加到我已经尝试过的 .bash_profile 中。

然后去哪儿?

我仍然无法解释为什么它在我之前的安装中有效,坦率地说,afaik 并没有改变很多。我通常经常进行全新安装,并且每次 w/base-devel 和 nodejs/python/git/vscode/firefox/discord 几乎是我的整个工作流程时,都会使用 lts 内核保持非常小的 arch linux 构建。 我都没有想法了。

Vit*_*oru 36

首先确保添加

export GPG_TTY=$(tty)
Run Code Online (Sandbox Code Playgroud)

在你的 .bashrc 中

显然 VSCode 不要求输入密码,这就是它给出错误的原因。我不知道原因。我个人的解决方案首先进行控制台提交或运行以下行

echo "test" | gpg --clearsign
Run Code Online (Sandbox Code Playgroud)

编辑

为了避免在每次提交时输入密码,您可以让 GPG 记住它 8 小时或直到下次重新启动:

mkdir -p ~/.gnupg
echo "default-cache-ttl 28800" >> ~/.gnupg/gpg-agent.conf
Run Code Online (Sandbox Code Playgroud)

GitHub 指南

  • 这是一个很好的方法来测试你的 gpg 密钥是否可以在不使用 git 的情况下使用。 (4认同)
  • 这个答案需要一个非常重要的缺失位。也就是说,在 VSCode 中,打开集成终端窗口,然后运行此答案中的命令。系统会要求您提供密码,然后在该会话中所有签名的提交都将起作用。如果再次关闭并打开 VSCode,则需要再次执行此操作,打开集成终端窗口... (3认同)

Jan*_*ita 9

Maybe git cannot find gpg? That was my problem with working with VSCode and using Remote-Containers to create development containers. Try running this in the Terminal within VSCode (in the container)

git config --global --unset gpg.program
git config --global --add gpg.program /usr/bin/gpg
Run Code Online (Sandbox Code Playgroud)

or wherever your gpg is located. You can find out by typing

which gpg
Run Code Online (Sandbox Code Playgroud)

If that works then you can put it in your Dockerfile for your development container.


Ayk*_*maz 7

几天前,我在使用 VS Code 和 WSL 时遇到了同样的问题。问题是 VS Code 无法.profile正确加载文件(以及其中的所有环境变量)(尝试运行此命令,但没有得到正确的结果:)echo $GPG_TTY。幸运的是,在 VS Code 首选项中为 shell 参数设置“-l”选项对我有用。这可确保.profile(或.zprofile)文件成功加载。我将这些行添加到settings.json

"terminal.integrated.shellArgs.linux": [
    "-l"
]
Run Code Online (Sandbox Code Playgroud)

确保添加export GPG_TTY=$(tty)文件.profile并重新启动终端和 VS Code。


更新:由于 VSCode 不赞成使用shellArgsoprion,请使用以下代码片段作为替代方案。

"terminal.integrated.profiles.linux": {
    "bash": {
        "path": "bash",
        "args": ["-l"],
        "icon": "terminal-bash"
    },
    "zsh": {
        "path": "zsh",
        "args": ["-l"],
    },
    "fish": {
        "path": "fish",
        "args": ["-l"],
    },
    "tmux": {
        "path": "tmux",
        "args": ["-l"],
        "icon": "terminal-tmux"
    },
    "pwsh": {
        "path": "pwsh",
        "args": ["-l"],
        "icon": "terminal-powershell"
    }
},
"terminal.integrated.defaultProfile.linux": "bash"
Run Code Online (Sandbox Code Playgroud)

-l选项已添加到上面的所有终端配置文件中,删除未使用的配置文件并根据您的意愿设置默认配置文件。

  • 请注意,这已被弃用,请改用“#terminal.integrated.defaultProfile.linux#” (3认同)