From 1b8166b1987a8bad5198dfbb620b5a8067cd733b Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 31 Aug 2015 01:51:01 +0100 Subject: [PATCH] Enable embedding versioning info into the repo Add a script to write a .ottdrev style file: .ottdrev-vc, which also includes a SHA-256 hash of the source tree. Modify findversion.sh to use this file if no VCS info can be found, and use the source tree hash to set the modified status. Include leading digits of hash in version string if different, this is to make it easier to distinguish between modified or non-release versions. Add function to create release tag and create/commit .ottdrev-vc in one go. --- findversion.sh | 24 ++++++++ version_utils.sh | 145 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100755 version_utils.sh diff --git a/findversion.sh b/findversion.sh index 6be52b696f..2fd738fde6 100755 --- a/findversion.sh +++ b/findversion.sh @@ -122,6 +122,30 @@ elif [ -d "$ROOT_DIR/.hg" ]; then # No rev? Maybe it is a custom hgsubversion clone REV_NR=`LC_ALL=C HGPLAIN= hg parent --template="{svnrev}"` fi +elif [ -f "$ROOT_DIR/.ottdrev-vc" ]; then + VERSION_DATA="`cat "$ROOT_DIR/.ottdrev-vc" | sed -n -e '1 p;'`" + HASH_DATA="`cat "$ROOT_DIR/.ottdrev-vc" | sed -n -e '2 p;'`" + REV="`echo "$VERSION_DATA" | cut -f 1 -d' '`" + REV_NR="`echo "$VERSION_DATA" | cut -f 2 -d' '`" + MODIFIED="`echo "$VERSION_DATA" | cut -f 3 -d' '`" + CLEAN_REV="`echo "$VERSION_DATA" | cut -f 4 -d' '`" + if [ "$MODIFIED" = "2" ]; then + CLEAN_REV="`echo "$CLEAN_REV" | sed -e 's/M$//'`" + fi + BRANCH="`echo "$REV" | sed -n -e "s|^${CLEAN_REV}M\?-\(.\+\)$|\1|p"`" + REV="$CLEAN_REV" + if ! $ROOT_DIR/version_utils.sh -o; then + MODIFIED="1" + else + CURRENT_HASH="`$ROOT_DIR/version_utils.sh -s`" + if [ "$CURRENT_HASH" != "$HASH_DATA" ]; then + MODIFIED="2" + if [ -n "$BRANCH" ]; then + BRANCH="$BRANCH-" + fi + BRANCH="${BRANCH}H`echo "$CURRENT_HASH" | cut -c 1-8`" + fi + fi elif [ -f "$ROOT_DIR/.ottdrev" ]; then # We are an exported source bundle cat $ROOT_DIR/.ottdrev diff --git a/version_utils.sh b/version_utils.sh new file mode 100755 index 0000000000..550e608207 --- /dev/null +++ b/version_utils.sh @@ -0,0 +1,145 @@ +#!/bin/bash + +# $Id$ + +# This file is part of OpenTTD. +# OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. +# OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + +function show_help { + echo "Usage: version_utils.sh OPTION" >&2 + echo "-s: Output a SHA-256 of the source tree" >&2 + echo "-l: Output the names of all files in the source tree with their SHA-256 hash" >&2 + echo "-n: Output the names of all files in the source tree without a hash" >&2 + echo "-o: Return true (0) if SHA-256 utility can be found" >&2 + echo "-w: Write ./.ottdrev-vc" >&2 + echo "-r TAGNAME: Create a tag, write ./.ottdrev-vc referencing the tag," >&2 + echo " commit it and move the tag to point to the new revision. Requires git." >&2 + echo "-h: Show this help" >&2 +} + +NAMES= +HASHLIST= +HASH= +TESTOK= +WRITE= +RELEASETAG= +while getopts ":hslnowr:" opt; do + case $opt in + s) + HASH=1 + ;; + l) + HASHLIST=1 + ;; + n) + NAMES=1 + ;; + o) + TESTOK=1 + ;; + w) + WRITE=1 + ;; + r) + RELEASETAG="$OPTARG" + ;; + h | \?) + show_help + exit 1 + ;; + esac +done + +HASH_CMD= + +function handle_source { + if [ -n "$2" ]; then + $HASH_CMD "$1" + else + echo "$1" + fi +} + +function find_hasher { + if [ "`echo -n "test" | sha256sum 2> /dev/null`" == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 -" ]; then + HASH_CMD=sha256sum + elif [ "`echo -n "test" | shasum -a 256 2> /dev/null`" == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 -" ]; then + HASH_CMD=shasum -a 256 + elif [ "`echo -n "test" | shasum -a 256 -p 2> /dev/null`" == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 -" ]; then + HASH_CMD=shasum -a 256 -p + else + echo "Could not generate SHA-256" >&2 + exit 1 + fi +} + +function output_hash_list { + read_source "1" +} + +function read_source { + handle_source "source.list" "$1" + handle_source "config.lib" "$1" + handle_source "configure" "$1" + handle_source "Makefile.in" "$1" + handle_source "Makefile.bundle.in" "$1" + handle_source "Makefile.grf.in" "$1" + handle_source "Makefile.setting.in" "$1" + handle_source "Makefile.src.in" "$1" + while IFS=$'\n' read -r line; do + handle_source "src/$line" "$1" + done < <( sed -e 's/^[ \t]*//; s/[ \t]*$//;' -e '/^$/ d;' -e '/^#/ d;' -e '/^..\// d;' "source.list" ) +} + +if [ -z "$HASH" -a -z "$NAMES" -a -z "$HASHLIST" -a -z "$TESTOK" -a -z "$WRITE" -a -z "$RELEASETAG" ]; then + show_help + exit 1 +fi + +if [ -n "$NAMES" ]; then + read_source +fi + +if [ -n "$HASHLIST" ]; then + find_hasher + output_hash_list +fi + +if [ -n "$HASH" ]; then + find_hasher + output_hash_list | $HASH_CMD +fi + +if [ -n "$WRITE" ]; then + find_hasher + ./findversion.sh > .ottdrev-vc-tmp + output_hash_list | $HASH_CMD >> .ottdrev-vc-tmp + mv .ottdrev-vc-tmp .ottdrev-vc +fi + +if [ -n "$RELEASETAG" ]; then + if ! git diff-index --quiet HEAD; then + echo "Repo is dirty, aborting" >&2 + exit 1 + fi + if ! git diff-index --quiet --cached HEAD; then + echo "Repo is dirty, aborting" >&2 + exit 1 + fi + if ! git tag "$RELEASETAG"; then + echo "Tag already exists or is not valid, aborting" >&2 + exit 1 + fi + if ! ./version_utils.sh -w; then + exit 1 + fi + git add .ottdrev-vc + git commit -m "Version: Committing version data for tag: $RELEASETAG" + git tag -f "$RELEASETAG" +fi + +if [ -n "$TESTOK" ]; then + find_hasher 2> /dev/null +fi