lfs/git-version.sh
Xi Ruoyao 6ca780c12d git-version: simplify the versioning scheme
Now the version for trunk head is simply rx.y-z.  x.y is the last
release, and z is the number of commits after x.y.

For other commits, the version is rx.y-z-g{sha}.  An abbreviated sha
hash is added to distinguish commits with same x, y, and z on different
branches.

If there are uncommited changes, a "+" is appended to the version,
indicating the version is "unclean".

To make it works correctly, a tag "rx.y" have to be created on the last
commit on trunk of x.y release.
2021-05-01 21:00:30 +08:00

48 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
if ! git status > /dev/null; then
# Either it's not a git repository, or git is unavaliable.
# Just workaround.
echo "<!ENTITY version \"unknown\">" > version.ent
echo "<!ENTITY versiond \"unknown-systemd\">" >> version.ent
echo "<!ENTITY releasedate \"unknown\">" >> version.ent
echo "<!ENTITY copyrightdate \"1999-2021\">" >> version.ent
exit 0
fi
export LC_ALL=en_US.utf8
export TZ=US/Pacific
commit_date=$(git show -s --format=format:"%cd" --date=local)
year=$(date --date "$commit_date" "+%Y")
month=$(date --date "$commit_date" "+%B")
month_digit=$(date --date "$commit_date" "+%m")
day=$(date --date "$commit_date" "+%d" | sed 's/^0//')
case $day in
"1" | "21" | "31" ) suffix="st";;
"2" | "22" ) suffix="nd";;
"3" | "23" ) suffix="rd";;
* ) suffix="th";;
esac
full_date="$month $day$suffix, $year"
sha="$(git describe --abbrev=1)"
if git describe --all --match trunk > /dev/null 2> /dev/null; then
sha=$(echo "$sha" | sed 's/-g[^-]*$//')
fi
version="$sha"
versiond="$sha-systemd"
if [ "$(git diff HEAD | wc -l)" != "0" ]; then
version="$version+"
versiond="$versiond+"
fi
echo "<!ENTITY version \"$version\">" > version.ent
echo "<!ENTITY versiond \"$versiond\">" >> version.ent
echo "<!ENTITY releasedate \"$full_date\">" >> version.ent
echo "<!ENTITY copyrightdate \"1999-$year\">" >> version.ent