glfs/git-version.sh

58 lines
1.7 KiB
Bash
Raw Normal View History

2021-04-15 17:31:50 +08:00
#!/bin/sh
if [ "$1" = sysv ]; then
SYSV="INCLUDE"
SYSTEMD="IGNORE "
elif [ "$1" = systemd ]; then
SYSV="IGNORE "
SYSTEMD="INCLUDE"
else
echo You must provide either \"sysv\" or \"systemd\" as argument
exit 1
fi
echo "<!ENTITY % sysv \"$SYSV\">" > conditional.ent
echo "<!ENTITY % systemd \"$SYSTEMD\">" >> conditional.ent
2021-04-15 17:31:50 +08:00
if ! git status > /dev/null; then
# Either it's not a git repository, or git is unavaliable.
# Just workaround.
echo "<!ENTITY year \"????\">" > version.ent
echo "<!ENTITY version \"unknown\">" >> version.ent
echo "<!ENTITY releasedate \"unknown\">" >> version.ent
echo "<!ENTITY pubdate \"unknown\">" >> version.ent
exit 0
2021-04-15 17:31:50 +08:00
fi
export LC_ALL=en_US.utf8
2023-01-01 23:56:35 +08:00
export TZ=America/Chicago
2021-04-15 17:31:50 +08:00
commit_date=$(git show -s --format=format:"%cd" --date=local)
2021-04-15 17:31:50 +08:00
short_date=$(date --date "$commit_date" "+%Y-%m-%d")
year=$(date --date "$commit_date" "+%Y")
month=$(date --date "$commit_date" "+%B")
day_digit=$(date --date "$commit_date" "+%d")
day=$(echo $day_digit | sed 's/^0//')
case $day in
"1" | "21" | "31" ) suffix="st";;
"2" | "22" ) suffix="nd";;
"3" | "23" ) suffix="rd";;
* ) suffix="th";;
2021-04-15 17:31:50 +08:00
esac
full_date="$month $day$suffix, $year"
sha="$(git describe --abbrev=1 --always --exclude '*')"
version=$(echo -n "#" && echo -n "$sha")
2021-04-15 17:31:50 +08:00
if [ "$(git diff HEAD | wc -l)" != "0" ]; then
version="$version"
fi
echo "<!ENTITY year \"$year\">" > version.ent
2021-04-15 17:31:50 +08:00
echo "<!ENTITY version \"$version\">" >> version.ent
echo "<!ENTITY releasedate \"$full_date\">" >> version.ent
echo "<!ENTITY pubdate \"$short_date\">" >> version.ent