gpg: do show gpg's error message upon failure

There are few things more frustrating when signing a commit fails than reading a terse "error: gpg failed to sign the data" message followed by the unsurprising "fatal: failed to write commit object" message. In many cases where signing a commit or tag fails, `gpg` actually said something helpful, on its stderr, and Git even consumed that, but then keeps mum about it. Teach Git to stop withholding that rather important information. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Feb 15, 2023 at 05:58 UTC ad6b320756d8d9150291c696a02c86d1c2f0f4b2
2 files changed +15 -3
gpg-interface.c
+6 -2
@@ -977,9 +977,13 @@ static int sign_buffer_gpg(struct strbuf *buffer, struct strbuf *signature,
977 break; /* found */
978 }
979 ret |= !cp;
980 + if (ret) {
981 + error(_("gpg failed to sign the data:\n%s"),
982 + gpg_status.len ? gpg_status.buf : "(no gpg output)");
983 + strbuf_release(&gpg_status);
984 + return -1;
985 + }
986 strbuf_release(&gpg_status);
981 - if (ret)
982 - return error(_("gpg failed to sign the data"));
987
988 /* Strip CR from the line endings, in case we are on Windows. */
989 remove_cr_after(signature, bottom);
t/t7510-signed-commit.sh
+9 -1
@@ -399,6 +399,10 @@ test_expect_success 'custom `gpg.program`' '
399
400 case "$1" in
401 -bsau)
402 + test -z "$LET_GPG_PROGRAM_FAIL" || {
403 + echo "zOMG signing failed!" >&2
404 + exit 1
405 + }
406 cat >sign.file
407 echo "[GNUPG:] SIG_CREATED $args" >&2
408 echo "-----BEGIN PGP MESSAGE-----"
@@ -420,7 +424,11 @@ test_expect_success 'custom `gpg.program`' '
424 git commit -S --allow-empty -m signed-commit &&
425 test_path_exists sign.file &&
426 git show --show-signature &&
423 - test_path_exists verify.file
427 + test_path_exists verify.file &&
428 +
429 + test_must_fail env LET_GPG_PROGRAM_FAIL=1 \
430 + git commit -S --allow-empty -m must-fail 2>err &&
431 + grep zOMG err
432 '
433
434 test_done