summaryrefslogtreecommitdiff
path: root/svnversion_regenerate.sh
blob: 7926789e72beaae470cf9ef803ae4a8ae4006dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash

#set -x

if test $# -ne 1 -a $# -ne 2
then
  echo "Usage: "`basename "$0"`" <file> [define_name]"
  exit 1
fi

OUTPUT_FILE="`pwd`/${1}"
TEMP_FILE="${OUTPUT_FILE}.tmp"

#echo svnversion...
#pwd
#echo "$OUTPUT_FILE"
#echo "$TEMP_FILE"

# The script should reside in the toplevel source directory which should contain
# all version control files.
cd `dirname ${0}`

if test $# -eq 2
then
  DEFINE=${2}
else
  DEFINE=SVN_VERSION
fi

if test -d .svn
then
  REV=`svnversion 2> /dev/null`
else
  if test -d .git
  then
    git status >/dev/null # updates dirty state
    REV=`git show | grep '^ *git-svn-id:' | sed 's/.*@\([0-9]*\) .*/\1/'`
    if test ${REV}
    then
      test -z "$(git diff-index --name-only HEAD)" || REV="${REV}M"
    else
      REV=0+`git rev-parse HEAD`
      test -z "$(git diff-index --name-only HEAD)" || REV="${REV}-dirty"
    fi
  fi
fi

if test -z ${REV}
then
  REV="unknown"
fi

echo "#define ${DEFINE} \"${REV}\"" > "${TEMP_FILE}"
if test ! -f "${OUTPUT_FILE}"
then
  echo "Generated ${OUTPUT_FILE} (${REV})"
  cp "${TEMP_FILE}" "${OUTPUT_FILE}"
  if test $? -ne 0; then exit 1; fi
else
  if ! cmp -s "${OUTPUT_FILE}" "${TEMP_FILE}"
  then echo "Regenerated ${OUTPUT_FILE} (${REV})"
    cp "${TEMP_FILE}" "${OUTPUT_FILE}"
    if test $? -ne 0; then exit 1; fi
  fi
fi

rm "${TEMP_FILE}"

exit $?