How to: git与gpg配置排错

gpg这个东西实在是太烦人了。愉快地敲完代码准备commit时,一个error: gpg failed to sign the data把好心情都破坏了。简要记录一下这次的gpg排错流程。

众所周知,在git调用gpg出错时只会给你一个含糊不清的

error: gpg failed to sign the data
fatal: failed to write commit object

这东西的唯一作用就是升血压。

那么怎样让gpg提供更多调试信息呢?思路就是使用GIT_TRACE=1获取详细的调试信息,以此判断问题根源是在git还是gpg。

GIT_TRACE=1 git commit -m "Refactor: 调整了json结构"

运行后得到以下结果:

15:11:58.845189 exec-cmd.c:139          trace: resolved executable path from Darwin stack: /Library/Developer/CommandLineTools/usr/bin/git
15:11:58.846172 exec-cmd.c:238          trace: resolved executable dir: /Library/Developer/CommandLineTools/usr/bin
15:11:58.848688 git.c:455               trace: built-in: git commit -m 'Refactor: 调整了json结构'
15:11:58.858739 run-command.c:667       trace: run_command: gpg --status-fd=2 -bsau mykey
error: gpg failed to sign the data
fatal: failed to write commit object

可以看出,错误出在gpg --status-fd=2 -bsau mykey这里。那么,我们touch一个文件让gpg签名,以此确定出在gpg的问题是什么。命令如下:

touch /tmp/foo && gpg --status-fd=2 -bsau mykey /tmp/foo

我执行后得到的结果:

[GNUPG:] KEY_CONSIDERED **** 2
[GNUPG:] BEGIN_SIGNING H8
gpg: 签名时失败: No pinentry
[GNUPG:] FAILURE sign 67108949
gpg: signing failed: No pinentry

很明显,这次出错的原因是No pinentry,然而我之前就安装并配置了pinentry-mac。搜索得知可能是pinentry有修改,导致gpg-agent在调用pinentry时出现错误,只要重启gpg-agent即可:

gpgconf --kill gpg-agent

再次对刚才的/tmp/foo文件进行签名,成功。回到git,成功commit,问题解决。


参考链接:

  1. https://stackoverflow.com/questions/39494631/gpg-failed-to-sign-the-data-fatal-failed-to-write-commit-object-git-2-10-0/47561300#47561300
  2. https://superuser.com/questions/1628782/gpg-signing-failed-no-pinentry

本文链接:

https://www.direcore.xyz/archives/33/
1 + 9 =
快来做第一个评论的人吧~