]> git.ipfire.org Git - thirdparty/git.git/commitdiff
lib-gpg: allow tests with GPGSM or GPGSSH prereq first
authorChristian Couder <christian.couder@gmail.com>
Mon, 13 Oct 2025 08:48:54 +0000 (10:48 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 Oct 2025 15:51:41 +0000 (08:51 -0700)
When the 'GPG' prereq is lazily tested, `mkdir "$GNUPGHOME"` could
fail if the "$GNUPGHOME" directory already exists. This can happen if
the 'GPGSM' or the 'GPGSSH' prereq has been lazily tested before as they
already create "$GNUPGHOME".

To allow the GPGSM or the GPGSSH prereq to appear before the GPG prereq
in some test scripts, let's refactor the creation and setup of the
"$GNUPGHOME"` directory in a new prepare_gnupghome() function that uses
`mkdir -p "$GNUPGHOME"`.

This will be useful in a following commit.

Unfortunately the new prepare_gnupghome() function cannot be used when
lazily testing the GPG2 prereq, because that would expose existing,
hidden bugs in "t1016-compatObjectFormat.sh", so let's just document
that with a NEEDSWORK comment.

Helped-by: Todd Zullinger <tmz@pobox.com>
Helped-by: Collin Funk <collin.funk1@gmail.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/lib-gpg.sh

index 937b876bd05281c7ab6830d5930c63a1b1f906fe..b99ae39a06b68395e3288c15d494f6637c724cd4 100644 (file)
@@ -9,6 +9,16 @@
 GNUPGHOME="$(pwd)/gpghome"
 export GNUPGHOME
 
+# All the "test_lazy_prereq GPG*" below should use
+# `prepare_gnupghome()` either directly or through a call to
+# `test_have_prereq GPG*`. That's because `gpg` and `gpgsm`
+# only create the directory specified using "$GNUPGHOME" or
+# `--homedir` if it's the default (usually "~/.gnupg").
+prepare_gnupghome() {
+       mkdir -p "$GNUPGHOME" &&
+       chmod 0700 "$GNUPGHOME"
+}
+
 test_lazy_prereq GPG '
        gpg_version=$(gpg --version 2>&1)
        test $? != 127 || exit 1
@@ -38,8 +48,7 @@ test_lazy_prereq GPG '
                # To export ownertrust:
                #       gpg --homedir /tmp/gpghome --export-ownertrust \
                #               > lib-gpg/ownertrust
-               mkdir "$GNUPGHOME" &&
-               chmod 0700 "$GNUPGHOME" &&
+               prepare_gnupghome &&
                (gpgconf --kill all || : ) &&
                gpg --homedir "${GNUPGHOME}" --import \
                        "$TEST_DIRECTORY"/lib-gpg/keyring.gpg &&
@@ -63,6 +72,14 @@ test_lazy_prereq GPG2 '
                ;;
        *)
                (gpgconf --kill all || : ) &&
+
+               # NEEDSWORK: prepare_gnupghome() should definitely be
+               # called here, but it looks like it exposes a
+               # pre-existing, hidden bug by allowing some tests in
+               # t1016-compatObjectFormat.sh to run instead of being
+               # skipped. See:
+               # https://lore.kernel.org/git/ZoV8b2RvYxLOotSJ@teonanacatl.net/
+
                gpg --homedir "${GNUPGHOME}" --import \
                        "$TEST_DIRECTORY"/lib-gpg/keyring.gpg &&
                gpg --homedir "${GNUPGHOME}" --import-ownertrust \
@@ -132,8 +149,7 @@ test_lazy_prereq GPGSSH '
        test $? = 0 || exit 1;
 
        # Setup some keys and an allowed signers file
-       mkdir -p "${GNUPGHOME}" &&
-       chmod 0700 "${GNUPGHOME}" &&
+       prepare_gnupghome &&
        (setfacl -k "${GNUPGHOME}" 2>/dev/null || true) &&
        ssh-keygen -t ed25519 -N "" -C "git ed25519 key" -f "${GPGSSH_KEY_PRIMARY}" >/dev/null &&
        ssh-keygen -t rsa -b 2048 -N "" -C "git rsa2048 key" -f "${GPGSSH_KEY_SECONDARY}" >/dev/null &&