]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
scripts, doc: generate vcs info for archives, use for copyright year
authorOto Šťáva <oto.stava@nic.cz>
Thu, 8 Dec 2022 09:13:49 +0000 (10:13 +0100)
committerOto Šťáva <oto.stava@nic.cz>
Mon, 12 Dec 2022 07:22:11 +0000 (08:22 +0100)
.gitignore
doc/conf.py
meson.build
scripts/dist-copy-vcs-info.sh [new file with mode: 0644]
scripts/gen-vcs-info.sh [new file with mode: 0755]
scripts/make-archive.sh

index d4522a87b1541bc4718419273fd0f1d123e358f3..1e35155cb44ad2669813f9ac12ca4c12b11fd664 100644 (file)
@@ -22,6 +22,7 @@
 .dirstamp
 .libs
 .pytest_cache
+.kr-vcs-info
 /.build-depend
 /.cache
 /aclocal.m4
index ab7e6db2e687d48ed3233c0f53789bd71505d95c..d26383d5fae1ad26b4465648fb9b0a7565b9c9f2 100644 (file)
@@ -1,6 +1,8 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 # -*- coding: utf-8 -*-
 
+import errno
+import json
 import os
 import re
 import subprocess
@@ -25,16 +27,37 @@ breathe_domain_by_extension = {"h": "c"}
 source_suffix = '.rst'
 master_doc = 'index'
 
-# Get current year (preferably from Git commit)
+# Get current year in the order of preference (fallback to system time)
 commit_year = date.today().year
-
-try:
-    commit_date_str = subprocess.check_output(['git', 'show', '--no-patch', '--format=%cs'])\
-            .decode().strip()
-    commit_date = date.fromisoformat(commit_date_str)
-    commit_year = commit_date.year
-except BaseException as e:
-    print('Could not get current commit, using system time for copyright:', e)
+commit_year_src = 'system time'
+commit_year_got = False
+
+if not commit_year_got:
+    try:
+        commit_date_str = subprocess.check_output(['git', 'show', '--no-patch', '--format=%cs'])\
+                .decode().strip()
+        commit_date = date.fromisoformat(commit_date_str)
+        commit_year = commit_date.year
+        commit_year_src = 'Git'
+        commit_year_got = True
+    except subprocess.CalledProcessError as e:
+        pass # Kind of expected, just silently fall back to '.kr-vcs-info'
+
+if not commit_year_got:
+    try:
+        with open('../.kr-vcs-info', 'rb') as vcs_info_fp:
+            vcs_info = json.load(vcs_info_fp)
+
+        commit_date = date.fromisoformat(vcs_info['commitDate'])
+        commit_year = commit_date.year
+        commit_year_src = '.kr-vcs-info'
+        commit_year_got = True
+    except OSError as e:
+        if e.errno != errno.ENOENT:
+            raise e
+
+print('Using copyright year ({year}) from {year_src}'.format(
+    year=commit_year, year_src=commit_year_src))
 
 # General information about the project.
 project = u'Knot Resolver'
index 6a1b65b2ae350ddb77932cb99c5f0675218af7c0..79ce45096ef5f57092a940fb3993d872dcacf948 100644 (file)
@@ -43,6 +43,9 @@ if host_machine.system() == 'darwin'
   libext = '.dylib'
 endif
 
+# VCS info
+meson.add_dist_script('scripts/dist-copy-vcs-info.sh')
+
 ## Paths
 prefix = get_option('prefix')
 data_dir = prefix / get_option('datadir') / 'knot-resolver'
diff --git a/scripts/dist-copy-vcs-info.sh b/scripts/dist-copy-vcs-info.sh
new file mode 100644 (file)
index 0000000..502e812
--- /dev/null
@@ -0,0 +1,16 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0-or-later
+# Run on 'meson dist' to copy VCS information (e.g. to generate documentation
+# with correct copyright year)
+set -o errexit -o nounset -o xtrace
+
+if [ -z "$MESON_DIST_ROOT" ]; then
+       echo "MESON_DIST_ROOT is not set! Must be run from 'meson dist'!" >&2
+       exit 1
+fi
+if [ -z "$MESON_SOURCE_ROOT" ]; then
+       echo "MESON_SOURCE_ROOT is not set! Must be run from 'meson dist'!" >&2
+       exit 1
+fi
+
+cp "$MESON_SOURCE_ROOT/.kr-vcs-info" "$MESON_DIST_ROOT"
diff --git a/scripts/gen-vcs-info.sh b/scripts/gen-vcs-info.sh
new file mode 100755 (executable)
index 0000000..e70ac6f
--- /dev/null
@@ -0,0 +1,26 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0-or-later
+# Make a `.kr-vcs-info` file for archive purposes
+set -o errexit -o nounset -o xtrace
+
+cd "$(dirname ${0})/.."
+
+vcs_info_name='.kr-vcs-info'
+
+# make sure we don't accidentally add / overwrite forgotten changes in git
+#(git diff-index --quiet HEAD && git diff-index --cached --quiet HEAD) || \
+#    (echo 'git index has uncommitted changes!'; exit 1)
+
+rm -f "$vcs_info_name"
+
+commit_date="$(git show --no-patch --format=%cs)"
+commit_hash="$(git show --no-patch --format=%H)"
+tag="$(git describe --tags --exact-match || echo '')"
+
+cat > "$vcs_info_name" <<EOF
+{
+       $(test -n "$tag" && echo "\"tag\": \"$tag\",")
+       "commitDate": "$commit_date",
+       "commitHash": "$commit_hash"
+}
+EOF
index 827036747272269fa310026cd08335bc7b6219d1..e0a408e1e256a5287cdf9ed5a79532f9aa59b8fd 100755 (executable)
@@ -9,6 +9,8 @@ cd "$(dirname ${0})/.."
 (git diff-index --quiet HEAD && git diff-index --cached --quiet HEAD) || \
     (echo 'git index has uncommitted changes!'; exit 1)
 
+./scripts/gen-vcs-info.sh
+
 if ! git describe --tags --exact-match; then
     # devel version
     GIT_HASH=$(git rev-parse --short HEAD )