2011-03-06 16:18:18 -08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# autorevision.sh - a shell script to get git / hg revisions etc. into binary builds.
|
|
|
|
# To use pass a path to the desired output file: some/path/to/autorevision.h.
|
|
|
|
# Note: the script will run at the root level of the repository that it is in.
|
|
|
|
|
|
|
|
# Config
|
|
|
|
TARGETFILE="${1}"
|
|
|
|
|
|
|
|
# For git repos
|
|
|
|
function gitRepo {
|
|
|
|
cd "$(git rev-parse --show-toplevel)"
|
|
|
|
|
|
|
|
# Is the working copy clean?
|
|
|
|
git diff --quiet HEAD &> /dev/null
|
|
|
|
WC_MODIFIED="${?}"
|
|
|
|
|
|
|
|
# Enumeration of changesets
|
|
|
|
VCS_NUM="$(git rev-list --count HEAD)"
|
2011-03-16 07:47:53 -07:00
|
|
|
if [ -z "${VCS_NUM}" ]; then
|
2011-03-16 11:08:48 -07:00
|
|
|
echo "warning: Counting the number of revisions may be slower due to an outdated git version less than 1.7.2.3. If something breaks, please update it."
|
2011-03-16 07:47:53 -07:00
|
|
|
VCS_NUM="$(git rev-list HEAD | wc -l)"
|
|
|
|
fi
|
2011-03-06 16:18:18 -08:00
|
|
|
|
|
|
|
# The full revision hash
|
|
|
|
VCS_FULL_HASH="$(git rev-parse HEAD)"
|
|
|
|
|
|
|
|
# The short hash
|
|
|
|
VCS_SHORT_HASH="$(echo ${VCS_FULL_HASH} | cut -b 1-7)"
|
|
|
|
|
|
|
|
# Current branch
|
|
|
|
VCS_URI="$(git symbolic-ref HEAD)"
|
|
|
|
|
|
|
|
# Current tag (or uri if there is no tag)
|
|
|
|
VCS_TAG="$(git describe --exact-match --tags 2>/dev/null)"
|
|
|
|
if [ -z "${VCS_TAG}" ]; then
|
|
|
|
VCS_TAG="${VCS_URI}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Date of the curent commit
|
|
|
|
VCS_DATE="$(git log -1 --pretty=format:%ci)"
|
|
|
|
}
|
|
|
|
|
|
|
|
# For hg repos
|
|
|
|
function hgRepo {
|
|
|
|
cd "$(hg root)"
|
|
|
|
|
|
|
|
# Is the working copy clean?
|
|
|
|
hg sum | grep -q 'commit: (clean)'
|
|
|
|
WC_MODIFIED="${?}"
|
|
|
|
|
|
|
|
# Enumeration of changesets
|
|
|
|
VCS_NUM="$(hg id -n)"
|
|
|
|
|
|
|
|
# The full revision hash
|
|
|
|
VCS_FULL_HASH="$(hg log -r ${VCS_NUM} -l 1 --template '{node}\n')"
|
|
|
|
|
|
|
|
# The short hash
|
|
|
|
VCS_SHORT_HASH="$(hg id -i)"
|
|
|
|
|
|
|
|
# Current bookmark (bookmarks are roughly equivalent to git's branches) or branch if no bookmark
|
|
|
|
VCS_URI="$(hg id -B | cut -d ' ' -f 1)"
|
|
|
|
# Fall back to the branch if there are no bookmarks
|
|
|
|
if [ -z "${VCS_URI}" ]; then
|
|
|
|
VCS_URI="$(hg id -b)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Current tag (or uri if there is no tag)
|
|
|
|
if [ "$(hg log -r ${VCS_NUM} -l 1 --template '{latesttagdistance}\n')" = "0" ]; then
|
|
|
|
VCS_TAG="`hg id -t | sed -e 's:qtip::' -e 's:tip::' -e 's:qbase::' -e 's:qparent::' -e "s:$(hg --color never qtop 2>/dev/null)::" | cut -d ' ' -f 1`"
|
|
|
|
else
|
|
|
|
VCS_TAG="${VCS_URI}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Date of the curent commit
|
|
|
|
VCS_DATE="$(hg log -r ${VCS_NUM} -l 1 --template '{date|isodatesec}\n')"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-26 23:47:02 -07:00
|
|
|
if [[ ! -z "$(git rev-parse HEAD 2>/dev/null)" ]]; then
|
2011-03-06 16:18:18 -08:00
|
|
|
gitRepo
|
2011-04-26 23:47:02 -07:00
|
|
|
elif [[ ! -z "$(hg id 2>/dev/null)" ]]; then
|
|
|
|
hgRepo
|
2011-03-06 16:18:18 -08:00
|
|
|
else
|
|
|
|
echo "error: No repo detected."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
cat > "${TARGETFILE}" << EOF
|
|
|
|
/* ${VCS_FULL_HASH} */
|
|
|
|
#ifndef AUTOREVISION_H
|
|
|
|
#define AUTOREVISION_H
|
|
|
|
|
|
|
|
#define VCS_NUM ${VCS_NUM}
|
|
|
|
#define VCS_DATE ${VCS_DATE}
|
|
|
|
#define VCS_URI ${VCS_URI}
|
|
|
|
#define VCS_TAG ${VCS_TAG}
|
|
|
|
|
|
|
|
#define VCS_FULL_HASH ${VCS_FULL_HASH}
|
|
|
|
#define VCS_SHORT_HASH ${VCS_SHORT_HASH}
|
|
|
|
|
|
|
|
#define VCS_WC_MODIFIED ${WC_MODIFIED}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-03-06 17:13:40 -08:00
|
|
|
EOF
|