diff --git a/util/getversion.sh b/util/getversion.sh index abf99eb186..bb1b7c5574 100755 --- a/util/getversion.sh +++ b/util/getversion.sh @@ -6,35 +6,67 @@ # # Generate version information for the EC binary -if ghash=`git rev-parse --short --verify HEAD 2>/dev/null`; then - if gdesc=`git describe --dirty --match='v*' 2>/dev/null`; then - IFS="-" fields=($gdesc) - tag="${fields[0]}" - IFS="." vernum=($tag) - numcommits=$((${vernum[2]}+${fields[1]:-0})) - ver_major="${vernum[0]}" - ver_branch="${vernum[1]}" - else - numcommits=`git rev-list HEAD | wc -l` - ver_major="v0" - ver_branch="0" - fi - # avoid putting the -dirty attribute if only the timestamp - # changed - git status > /dev/null 2>&1 +# Use this symbol as a separator to be able to reliably concatenate strings of +# text. +dc=$'\001' - dirty=`sh -c "[ '$(git diff-index --name-only HEAD)' ] && echo '-dirty'"` - vbase="${ver_major}.${ver_branch}.${numcommits}-${ghash}${dirty}" -else - # Fall back to the VCSID provided by the packaging system if available. - if ghash=${VCSID##*-}; then - vbase="1.1.9999-${ghash:0:7}" - else - # then ultimately fails to "no_version" - vbase="no_version" - fi -fi +# This function examines the state of the current directory and attempts to +# extract its version information: the latest tag, if any, how many patches +# are there since the latest tag, the top sha1, and if there are local +# modifications. +# +# Local modifications are reported by concatenating the revision string and +# the string '-dirty' using the $dc symbol as the separator. +# +# If there is no tags defined in this git repository, the base version is +# considered to be 0.0. +# +# If current directory is not a git depository, this function prints out +# "no_version" +get_tree_version() { + local ghash + local numcommits + local tag + local vbase + local ver_branch + local ver_major + + if ghash=`git rev-parse --short --verify HEAD 2>/dev/null`; then + if gdesc=`git describe --dirty --match='v*' 2>/dev/null`; then + IFS="-" fields=($gdesc) + tag="${fields[0]}" + IFS="." vernum=($tag) + numcommits=$((${vernum[2]}+${fields[1]:-0})) + ver_major="${vernum[0]}" + ver_branch="${vernum[1]}" + else + numcommits=`git rev-list HEAD | wc -l` + ver_major="v0" + ver_branch="0" + fi + # avoid putting the -dirty attribute if only the timestamp + # changed + git status > /dev/null 2>&1 + + dirty=`sh -c "[ '$(git diff-index --name-only HEAD)' ] && echo '-dirty'"` + vbase="${ver_major}.${ver_branch}.${numcommits}-${ghash}${dirty}" + else + # Fall back to the VCSID provided by the packaging system if available. + if ghash=${VCSID##*-}; then + vbase="1.1.9999-${ghash:0:7}" + else + # then ultimately fails to "no_version" + vbase="no_version" + fi + fi + echo "${vbase}${dc}${dirty}" +} + +IFS="${dc}" +values=( $(get_tree_version) ) +vbase="${values[0]}" # Retrieved version information. +dirty="${values[1]}" # Non-zero, if the repository is 'dirty' ver="${BOARD}_${vbase}" echo "/* This file is generated by util/getversion.sh */"