Merge branch 'master' into jgrpp
[openttd-jgr.git] / version_utils.sh
blobcafda1b59f68305c5a7be7d266ad2b6e45a8dbb5
1 #!/bin/bash
3 # $Id$
5 # This file is part of OpenTTD.
6 # 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.
7 # 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.
8 # 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 <http://www.gnu.org/licenses/>.
10 function show_help {
11 echo "Usage: version_utils.sh OPTION" >&2
12 echo "-s: Output a SHA-256 of the source tree" >&2
13 echo "-l: Output the names of all files in the source tree with their SHA-256 hash" >&2
14 echo "-n: Output the names of all files in the source tree without a hash" >&2
15 echo "-o: Return true (0) if SHA-256 utility can be found" >&2
16 echo "-w: Write ./.ottdrev-vc" >&2
17 echo "-r TAGNAME: Create a tag, write ./.ottdrev-vc referencing the tag, possibly update README.md," >&2
18 echo " commit it and move the tag to point to the new revision. Requires git." >&2
19 echo "-h: Show this help" >&2
22 NAMES=
23 HASHLIST=
24 HASH=
25 TESTOK=
26 WRITE=
27 RELEASETAG=
28 while getopts ":hslnowr:" opt; do
29 case $opt in
31 HASH=1
34 HASHLIST=1
37 NAMES=1
40 TESTOK=1
43 WRITE=1
46 RELEASETAG="$OPTARG"
48 h | \?)
49 show_help
50 exit 1
52 esac
53 done
55 HASH_CMD=
57 function handle_source {
58 if [ -n "$2" ]; then
59 $HASH_CMD "$1"
60 else
61 echo "$1"
65 function find_hasher {
66 if [ "`echo -n "test" | sha256sum 2> /dev/null`" == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 -" ]; then
67 HASH_CMD=sha256sum
68 elif [ "`echo -n "test" | shasum -a 256 2> /dev/null`" == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 -" ]; then
69 HASH_CMD="shasum -a 256"
70 elif [ "`echo -n "test" | shasum -a 256 -p 2> /dev/null`" == "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 -" ]; then
71 HASH_CMD="shasum -a 256 -p"
72 else
73 echo "Could not generate SHA-256" >&2
74 exit 1
78 function output_hash_list {
79 read_source "1"
82 function read_source {
83 handle_source "CMakeLists.txt" "$1"
84 while IFS=$'\n' read -r line; do
85 handle_source "$line" "$1"
86 done < <( find -L src -type f \( -name 'CMakeLists.txt' -o -name '*.cpp' -o -name '*.c' -o -name '*.hpp' -o -name '*.h' -o -name '*.sq' -o -name '*.mm' -o -name '*.in' \) -print | LC_ALL=C sort )
87 while IFS=$'\n' read -r line; do
88 handle_source "$line" "$1"
89 done < <( find -L src/lang -type f -name '*.txt' -print | LC_ALL=C sort )
92 if [ -z "$HASH" -a -z "$NAMES" -a -z "$HASHLIST" -a -z "$TESTOK" -a -z "$WRITE" -a -z "$RELEASETAG" ]; then
93 show_help
94 exit 1
97 if [ -n "$NAMES" ]; then
98 read_source
101 if [ -n "$HASHLIST" ]; then
102 find_hasher
103 output_hash_list
106 if [ -n "$HASH" ]; then
107 find_hasher
108 output_hash_list | $HASH_CMD
111 if [ -n "$WRITE" ]; then
112 find_hasher
113 cmake -DGENERATE_OTTDREV=.ottdrev-vc-tmp -P cmake/scripts/FindVersion.cmake
114 output_hash_list | $HASH_CMD >> .ottdrev-vc-tmp
115 mv .ottdrev-vc-tmp .ottdrev-vc
118 function unignore_files {
119 git update-index --no-assume-unchanged README.md jgrpp-changelog.md .ottdrev-vc
122 if [ -n "$RELEASETAG" ]; then
123 git update-index --assume-unchanged README.md jgrpp-changelog.md .ottdrev-vc
124 trap unignore_files EXIT
125 if ! git diff-index --quiet HEAD; then
126 echo "Repo is dirty, aborting" >&2
127 exit 1
129 if ! git diff-index --quiet --cached HEAD; then
130 echo "Repo is dirty, aborting" >&2
131 exit 1
133 if [ "${RELEASETAG:0:6}" = "jgrpp-" -a -n "${RELEASETAG:6}" ]; then
134 if ! grep -q -e "^### v${RELEASETAG:6} (" jgrpp-changelog.md; then
135 echo "v${RELEASETAG:6} is not in changelog, aborting" >&2
136 exit 1
139 if ! git tag "$RELEASETAG"; then
140 echo "Tag already exists or is not valid, aborting" >&2
141 exit 1
143 if ! ./version_utils.sh -w; then
144 exit 1
146 unignore_files
147 trap '' EXIT
148 if [ "${RELEASETAG:0:6}" = "jgrpp-" -a -n "${RELEASETAG:6}" ]; then
149 sed -i "1 s/^\(## JGR's Patchpack version \).\+/\1${RELEASETAG:6}/" README.md
151 git add .ottdrev-vc README.md jgrpp-changelog.md
152 git commit -m "Version: Committing version data for tag: $RELEASETAG"
153 git tag -f "$RELEASETAG"
156 if [ -n "$TESTOK" ]; then
157 find_hasher 2> /dev/null