野声

Hey, 野声!

谁有天大力气可以拎着自己飞呀
twitter
github

Using GPG to sign Git commits in VSCode WSL

Recently, I configured GPG for git, but when I tried to use VSCode WSL to commit git, it prompted:

Git: gpg failed to sign the data

The detailed output is as follows:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
error: gpg failed to sign the data
fatal: failed to write commit object

Reason#

The reason is that you did not "enter" the password for the GPG key you set, because when using VSCode WSL, there is no graphical interface for you to enter the password, so GPG cannot sign your commit.

In the Windows graphical interface, GPG will pop up a Pinentry dialog box for you to enter the password. However, when we use VSCode WSL, WSL does not use a graphical interface (XServer).

Solution#

The solution is simple:

GPG will cache the password we entered for the first time, so we don't need to enter the password repeatedly when signing the data multiple times within a period of time. Therefore, we can write a shell function to manually enter the GPG password first, and then use VSCode to commit the commit.

Here are two functions:

gpg-login() {
    export GPG_TTY=$TTY
    # Sign the string "test" with GPG, which requires entering the password.
    # Then the password will be cached and you don't need to enter it next time.
    # Redirect the output to null so it won't be displayed in the terminal.
    echo "test" | gpg --clearsign > /dev/null 2>&1
}

gpg-logout() {
    echo RELOADAGENT | gpg-connect-agent
}

Enter gpg-login in the terminal, and a password prompt will appear. After entering the password, the password will be cached, and you don't need to enter it when committing in VSCode.

Enter gpg-logout to clear the cache of your GPG password.

Although it is not a solution, it is still convenient to use and bearable.

For some other configurations of my shell, see: ubuntu_wsl/zshrc.

Configure GPG cache time#

Edit this file ~/.gnupg/gpg-agent.conf:

Add the following content to cache the password for 1 hour.

default-cache-ttl 3600
max-cache-ttl 3600

The unit is seconds.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.