stamp-version.sh (2821B)
1 #!/bin/sh 2 # Stamp debian/changelog with the version derived from the most recent git tag. 3 # 4 # Run this before dpkg-buildpackage. The Debian version has to be baked into 5 # debian/changelog: dpkg reads the package version from there and from nowhere 6 # else, so a git-derived version can only be applied by rewriting that file. 7 # 8 # ./debian/stamp-version.sh && dpkg-buildpackage -b -uc -us 9 # 10 # The version itself comes from scripts/get_version.sh, the same script the 11 # other GNU Taler repositories use ("git describe --tags" with the leading "v" 12 # stripped, cached in .version for builds from an exported tree). For a tagged 13 # commit that yields e.g. "0.1.0", further down the branch "0.1.0-3-gc0ffee12". 14 # Both are valid Debian versions -- dpkg splits on the *last* hyphen, so the 15 # commit hash becomes the Debian revision -- and this matches how the taler-rust 16 # packages are versioned. 17 # 18 # This script is idempotent: re-running it when the changelog already carries 19 # the current version does nothing. 20 set -eu 21 22 top_dir=$(dirname "$0")/.. 23 cd "$top_dir" 24 25 changelog=debian/changelog 26 package=$(dpkg-parsechangelog -l "$changelog" -S Source) 27 28 version=$(./scripts/get_version.sh) 29 30 # get_version.sh reports "unknown" when there is no tag reachable from HEAD 31 # (which is the case for this repository today). A Debian upstream version 32 # must start with a digit, so fall back to 0.0.0 plus the commit count, keeping 33 # the "<version>-<commits>-g<hash>" shape so the versions still sort correctly. 34 case "$version" in 35 [0-9]*) ;; 36 *) 37 if [ -e .git ]; then 38 version="0.0.0-$(git rev-list --count HEAD)-g$(git rev-parse --short=8 HEAD)" 39 else 40 echo "$0: no git tag and no git repository; cannot derive a version" >&2 41 exit 1 42 fi 43 # .version was written by get_version.sh with the bogus value. 44 rm -f .version 45 ;; 46 esac 47 48 current=$(dpkg-parsechangelog -l "$changelog" -S Version) 49 if [ "$current" = "$version" ]; then 50 echo "debian/changelog is already at $version" 51 exit 0 52 fi 53 54 # Prefer the maintainer tooling when it is installed; it keeps the changelog 55 # formatting canonical and honours DEBEMAIL/DEBFULLNAME. 56 if command -v dch >/dev/null 2>&1; then 57 EDITOR=true dch --changelog "$changelog" --newversion "$version" \ 58 --distribution UNRELEASED --force-bad-version --force-distribution \ 59 "Build from git ($version)." 60 else 61 maintainer=$(dpkg-parsechangelog -l "$changelog" -S Maintainer) 62 { 63 printf '%s (%s) UNRELEASED; urgency=medium\n\n' "$package" "$version" 64 printf ' * Build from git (%s).\n\n' "$version" 65 printf ' -- %s %s\n\n' "$maintainer" "$(date -R)" 66 cat "$changelog" 67 } > "$changelog.new" 68 mv "$changelog.new" "$changelog" 69 fi 70 71 echo "debian/changelog stamped with $version"