]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
meson: generate proper version tag when git fails on permission errors
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 19 Oct 2023 10:55:59 +0000 (12:55 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 19 Oct 2023 15:52:44 +0000 (17:52 +0200)
When building with mkosi I would get the following:

    [1/477] Generating version.h with a custom command
    fatal: detected dubious ownership in repository at '/work/src'
    To add an exception for this directory, call:

            git config --global --add safe.directory /work/src

and then the tag would be generated as 'v254-'. This is obviously some problem
with the setup, but we should handle this gracefully. Let's fall back to 'v254'
instead.

In the case where we have a repo but no tags, use --dirty=^ too, as in the case
with tags.

I tested four cases:
- normal checkout
- checkout with .git removed
- checkout with .git chowned to root
- checkout wiht all tags removed

tools/meson-vcs-tag.sh

index 65564bba85378020eab7eb6ed9201c3b7a74b999..b0ab0978c86f75fc9f4c8eb9618b687cbeaa2307 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env bash
 # SPDX-License-Identifier: LGPL-2.1-or-later
 
-set -eu
+set -u
 set -o pipefail
 
 dir="${1:?}"
@@ -18,11 +18,16 @@ else
     #
     # If the working tree has no tags (CI builds), the first git-describe will fail
     # and we fall back to project_version-commitid instead.
+
+    c=''
     if [ -e "${dir}/.git" ]; then
-        c="$(git -C "$dir" describe --abbrev=7 --dirty=^ 2>/dev/null ||
-                    echo "${fallback}-$(git -C "$dir" describe --always --abbrev=7)")"
-    else
-        c="${fallback}"
+        c="$(git -C "$dir" describe --abbrev=7 --dirty=^ 2>/dev/null)"
+        if [ -z "$c" ]; then
+            # This call might still fail with permission issues
+            suffix="$(git -C "$dir" describe --always --abbrev=7 --dirty=^ 2>/dev/null)"
+            [ -n "$suffix" ] && c="${fallback}-${suffix}"
+        fi
     fi
+    [ -z "$c" ] && c="${fallback}"
     echo "$c" | sed 's/^v//; s/-rc/~rc/'
 fi