`git -S -m commit` 无法请求密码 — 从 GPG mac 迁移到 GPG shell 后的签名问题

9
我正在为网站开发设置新的机器(macOS Sierra),我已经执行了brew install gpg,它已经安装了gpg2gpg-agent。我已经从旧的mac上复制了我的密钥~.gnupg。我不会像在旧机器上一样安装mac接口GPG Suite,因为我更喜欢使用命令行。

我已经根据需要设置了我的git全局变量。

    git config --global user.name "Christopher Allen"
    git config --global user.email "ChristopherA@LifeWithAlacrity.com"
    git config --global user.mail "ChristopherA@LifeWithAlacrity.com"
    git config --global user.signingKey F8D36C91357405ED

当我尝试向一个需要git config commit.gpgsign=true的git仓库提交更改时,使用旧版的GPG Suite会弹出一个窗口要求输入密码。然而,只使用GPG时,它可以正确找到我的公钥,但不会提示我输入签名密码。

    $ git commit -S -m "changed code"

    You need a passphrase to unlock the secret key for
    user: "Christopher Allen <ChristopherA@LifeWithAlacrity.com>"
    4096-bit RSA key, ID 357405ED, created 2015-04-16

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

在这里进行研究,我唯一看到的提到问题是在“我无法让`git tag -s`要求我的GPG密码”中,它表明问题出现在gpg-agent的环境变量上(没有建议的解决方案),或者使用gpg-preset-passphrase函数(我不想使用该函数)。
进一步检查发现,gpg-agent没有运行:
    $ gpg-agent
    gpg-agent: no gpg-agent running in this session

我发现了这个网页 https://blog.chendry.org/2015/03/13/starting-gpg-agent-in-osx.html,建议将以下脚本添加到 .bash_profile 中:
    [ -f ~/.gpg-agent-info ] && source ~/.gpg-agent-info
    if [ -S "${GPG_AGENT_INFO%%:*}" ]; then
        export GPG_AGENT_INFO
    else
      eval $( gpg-agent --daemon --write-env-file ~/.gpg-agent-info )
    fi 

在获取此脚本后,gpg-agent会提示:

    $ gpg-agent
    gpg-agent: gpg-agent running and available

然而,我仍然面临着同样的问题。

有没有什么想法可以解决这个问题呢?我不想使用旧的GPG Suite,回退到GPG 1.0,或者使用gpg-preset-passphrase。

谢谢!

-- Christopher Allen

4个回答

8

我实际上解决这个问题的方法是:

安装 pinentry

brew install pinentry

如果那样不起作用,可以尝试以下方法:

告诉 GPG 在需要密码时使用哪个 tty

export GPG_TTY=$(tty)

这实际上对我有用。

您还可以将此导出加入到~/.bashrc中,以便它会自动导出 别忘了重新加载文件或启动新会话。

一个简单的方法是:echo "export GPG_TTY=$(tty)" >> ~/.bashrc

如果你遇到这个错误:

gpg-agent: no gpg-agent running in this session

也将问题中提到的脚本添加到~/.bashrc 文件中。

[ -f ~/.gpg-agent-info ] && source ~/.gpg-agent-info
if [ -S "${GPG_AGENT_INFO%%:*}" ]; then
    export GPG_AGENT_INFO
else
    eval $( gpg-agent --daemon --write-env-file ~/.gpg-agent-info )
fi 

测试gpg是否正常工作

echo "你好" | gpg -s

有时它仍然不会要求输入密码

当git不要求我输入密码时,有时我使用上面的测试命令来提示输入密码,这将被缓存,然后我尝试提交我的更改。

增加密码缓存时间

如果您想缓存更长时间的密码,可以将以下行添加到配置文件中:~/.gnupg/gpg-agent.conf

default-cache-ttl       86400

我在zsh中看到了WARNING: "--write-env-file" is an obsolete option - it has no effect - 你不知道有什么替代方法吗? - Fyodor
1
export GPG_TTY=$(tty) 对我很有用,谢谢! - abukaj

4
原来问题在于我复制了所有的文件从`~.gnupg`,这覆盖了由 `brew install gpg` 创建的文件(很可能是其中一个`.conf`文件)。
我卸载了gpg以及其所有相关子包(有很多),只复制了`pubring.gpg`、`secring.gpg`和`trustdb.gpg`到`~.gnupg`目录下,并先执行了`brew install gpg`。新的`gpg.conf`和`gpg-agent.conf`被创建出来。
-- Christopher Allen

1

对我来说,上面的答案都没有用。

我不得不杀掉代理,然后使用 -S 调用 commit:

gpgconf --kill gpg-agent

git commit -S -am "一些消息"


0

通过在守护程序模式下启动gpg-agent,然后应用它呈现的GPG_AGENT_INFO环境变量,我能够解决这个问题。

gpg-agent --daemon
GPG_AGENT_INFO=/Users/.../.gnupg/S.gpg-agent:58895:1; export GPG_AGENT_INFO;

在同一会话中,我执行了git tag -s(注意小写的's'),然后正确地提示我输入GPG密码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接