Put version info into /etc/os-release

We will use these data for showing meaningful and (mostly) accurate
version information via NETCONF, eventually.

I a;sp want the version info to be visible in actual binaries as well,
so let's instruct Buildroot to keep .git around. That is not as simple
as, e.g.:

 CLA_SYSREPO_OVERRIDE_SRCDIR_RSYNC_EXCLUSIONS = --include .git

...because in the CI setup, these directories are already git submodules
of something, and therefore their .git uses a relative path, and one
cannot easily copy it around. Instead, query the actual original path
and put the result into a new .gitdir file. That should work on both the
CI system and a random developer's setup which might not be using
submodules for projects they are working on.

Change-Id: I4760d0ecaa23a1ea5cf591a6cf76bc76d5139b9b
diff --git a/board/czechlight/common/os-release.sh b/board/czechlight/common/os-release.sh
new file mode 100755
index 0000000..516852b
--- /dev/null
+++ b/board/czechlight/common/os-release.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+function czechlight_describe_git {
+	echo $(git --git-dir=${1}/.git --work-tree=${1} describe --dirty 2>/dev/null || git --git-dir=${1}/.git rev-parse --short HEAD)
+}
+
+function czechlight_query_local_make_var {
+	echo $(sed -n -e "s/\\s*$1\\s*=\\s*\\(.*\\)/\\1/p" ${BASE_DIR}/local.mk)
+}
+
+CLA_BR2_EXTERNAL_REV=$(czechlight_describe_git ${BR2_EXTERNAL_CZECHLIGHT_PATH})
+CLA_SYSREPO_REV=$(czechlight_describe_git $(czechlight_query_local_make_var CLA_SYSREPO_OVERRIDE_SRCDIR))
+NETCONF_CLI_REV=$(czechlight_describe_git $(czechlight_query_local_make_var NETCONF_CLI_OVERRIDE_SRCDIR))
+CLA_CPP_DEPENDENCIES_REV=$(czechlight_describe_git $(czechlight_query_local_make_var CLA_SYSREPO_OVERRIDE_SRCDIR)/submodules/dependencies)
+
+sed -i \
+	-e 's/^VERSION_ID=/BUILDROOT_VERSION_ID=/' \
+	-e 's/^VERSION=/BUILDROOT_VERSION=/' \
+	-e '/^NAME=/d' -e '/^PRETTY_NAME=/d' \
+	$TARGET_DIR/etc/os-release
+
+cat >> ${TARGET_DIR}/etc/os-release <<EOF
+NAME=CzechLight
+# When building under CI, these git revisions might not necessarily refer to
+# something that is available from Gerrit's git repositories. If the job which
+# produced this image is a result of a Zuul job tree with speculatively merged
+# changes, then these refs are private to Zuul mergers.
+PRETTY_NAME="Czech Light ${CLA_BR2_EXTERNAL_REV}"
+VERSION=${CLA_BR2_EXTERNAL_REV}
+CLA_SYSREPO_VERSION=${CLA_SYSREPO_REV}
+NETCONF_CLI_VERSION=${NETCONF_CLI_REV}
+CPP_DEPENDENCIES_VERSION=${CLA_CPP_DEPENDENCIES_REV}
+EOF
diff --git a/configs/czechlight_beaglebone_defconfig b/configs/czechlight_beaglebone_defconfig
index 66b4eb6..7a5fd22 100644
--- a/configs/czechlight_beaglebone_defconfig
+++ b/configs/czechlight_beaglebone_defconfig
@@ -17,7 +17,7 @@
 # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
 BR2_ROOTFS_USERS_TABLES="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/users"
 BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/overlay/ $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/beaglebone/overlay/"
-BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/beaglebone/create-uEnv.sh"
+BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/beaglebone/create-uEnv.sh $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/os-release.sh"
 BR2_ROOTFS_POST_IMAGE_SCRIPT="support/scripts/genimage.sh"
 BR2_ROOTFS_POST_SCRIPT_ARGS="-c $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/beaglebone/genimage.cfg"
 BR2_LINUX_KERNEL=y
diff --git a/configs/czechlight_clearfog_defconfig b/configs/czechlight_clearfog_defconfig
index 56741f3..9c24c20 100644
--- a/configs/czechlight_clearfog_defconfig
+++ b/configs/czechlight_clearfog_defconfig
@@ -20,7 +20,7 @@
 # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
 BR2_ROOTFS_USERS_TABLES="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/users"
 BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/overlay/ $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/overlay/"
-BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/copy-boot-scr.sh"
+BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/copy-boot-scr.sh $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/os-release.sh"
 BR2_ROOTFS_POST_IMAGE_SCRIPT="support/scripts/genimage.sh ${BR2_EXTERNAL_CZECHLIGHT_PATH}/board/czechlight/clearfog/usb-flash-genimage.sh"
 BR2_ROOTFS_POST_SCRIPT_ARGS="-c $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/genimage.cfg"
 BR2_LINUX_KERNEL=y
diff --git a/dev-setup-git.sh b/dev-setup-git.sh
index 9cfb294..9f44a09 100755
--- a/dev-setup-git.sh
+++ b/dev-setup-git.sh
@@ -22,6 +22,14 @@
 NETCONF_CLI_OVERRIDE_SRCDIR = ${CZECHLIGHT_BR2_EXT_LOC}/submodules/netconf-cli
 GAMMARUS_OVERRIDE_SRCDIR = ${CZECHLIGHT_BR2_EXT_LOC}/submodules/gammarus
 PYBIND11_OVERRIDE_SRCDIR = ${CZECHLIGHT_BR2_EXT_LOC}/submodules/pybind11
+
+define CZECHLIGHT_GIT_FIX_GITDIR
+	echo "gitdir: \$\$(git rev-parse --resolve-git-dir \$(SRCDIR)/.git)" > \$(@D)/.git
+endef
+CLA_SYSREPO_POST_RSYNC_HOOKS += CZECHLIGHT_GIT_FIX_GITDIR
+NETCONF_CLI_POST_RSYNC_HOOKS += CZECHLIGHT_GIT_FIX_GITDIR
+GAMMARUS_POST_RSYNC_HOOKS += CZECHLIGHT_GIT_FIX_GITDIR
+
 EOF
 
 # We have to run make first so that the proxy Makefile is created and the BR2_EXTERNAL is remembered