aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/build-aux/git-package-version.sh
blob: 8c10f642b79ef3ed427c3396227c3074014a16ca (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/sh
#
#   Bindle Binaries Objective-C Kit
#   Copyright (c) 2012, Bindle Binaries
#
#   @BINDLE_BINARIES_BSD_LICENSE_START@
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions are
#   met:
#
#      * Redistributions of source code must retain the above copyright
#        notice, this list of conditions and the following disclaimer.
#      * Redistributions in binary form must reproduce the above copyright
#        notice, this list of conditions and the following disclaimer in the
#        documentation and/or other materials provided with the distribution.
#      * Neither the name of Bindle Binaries nor the
#        names of its contributors may be used to endorse or promote products
#        derived from this software without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
#   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
#   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BINDLE BINARIES BE LIABLE FOR
#   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
#
#   @BINDLE_BINARIES_BSD_LICENSE_END@
#
#   build-aux/git-package-version.sh - determines Git version if available
#

# saves srcdir from command line arguments
if test "x${1}" == "x";then
   echo "Usage: ${0} srcdir [ savedir ]" 1>&2;
   exit 1;
fi;
SRCDIR=$1;

# saves savedir from command line arguments
OUTDIR="${SRCDIR}/build-aux"
if test "x${2}" != "x";then
   OUTDIR="${2}"
   if test ! -d "${OUTDIR}";then
      echo "${0}: ${OUTDIR}: directory does not exist" 1>&2;
      exit 1;
   fi
fi;

GPV="" # git project version (XXX.YYY.ZZZ.gCCC)
GBV="" # git build version (gCCC)
GAV="" # git application version (XXX.YYY.ZZZ)

# set default file names
if test "x${GIT_VERSION_FILE}" == "x";then
   GIT_VERSION_FILE="${OUTDIR}/git-package-version.txt"
fi
if test "x${GIT_VERSION_HEADER}" == "x";then
   GIT_VERSION_HEADER="${OUTDIR}/git-package-version.h"
fi
if test "x${GIT_VERSION_PREFIX_HEADER}" == "x";then
   GIT_VERSION_PREFIX_HEADER="${OUTDIR}/git-package-version-prefix.h"
fi

# sets default directories
GIT_VERSION_FILE_DIR=`dirname ${GIT_VERSION_FILE}`
GIT_VERSION_HEADER_DIR=`dirname ${GIT_VERSION_HEADER}`
GIT_VERSION_PREFIX_HEADER_DIR=`dirname ${GIT_VERSION_PREFIX_HEADER}`

if test -f ${SRCDIR}/.git/config;then
   # retrieve raw output of git describe
   RAW=`git --git-dir=${SRCDIR}/.git describe --long --abbrev=7 HEAD 2> /dev/null`;

   # calculate GBV from raw output of git describe
   GBV=`echo ${RAW} |cut -d- -f3`;

   # calculate GPV from raw output of git describe
   GPV=`echo ${RAW} |sed -e 's/-/./g' -e 's/^v//g'`;

   # calculate GAV from GPV
   GAV=`echo ${GPV} |sed -e 's/\.g[[:xdigit:]]\{0,\}$//g' -e 's/\.0$//g'`;

   # write data to file and display results
   if test "x${GPV}" != "x";then
      rm -f ${GIT_VERSION_FILE} ${GIT_VERSION_HEADER} ${GIT_VERSION_PREFIX_HEADER}

      # writes git version file
      if test -d ${GIT_VERSION_FILE_DIR};then
         echo "${GPV}" > ${GIT_VERSION_FILE};   2>&1
      fi

      # writes git version C header
      if test -d ${GIT_VERSION_HEADER_DIR};then
         echo "#define GIT_PACKAGE_VERSION     \"${GPV}\""  > ${GIT_VERSION_HEADER}; 2>&1
         echo "#define GIT_APPLICATION_VERSION \"${GAV}\"" >> ${GIT_VERSION_HEADER}; 2>&1
         echo "#define GIT_BUILD_VERSION       \"${GBV}\"" >> ${GIT_VERSION_HEADER}; 2>&1
      fi

      # writes git version Info.plist preprocessor prefix file
      if test -d ${GIT_VERSION_PREFIX_HEADER_DIR};then
         echo "#define GIT_PACKAGE_VERSION     ${GPV}"  > ${GIT_VERSION_PREFIX_HEADER}; 2>&1
         echo "#define GIT_APPLICATION_VERSION ${GAV}" >> ${GIT_VERSION_PREFIX_HEADER}; 2>&1
         echo "#define GIT_BUILD_VERSION       ${GBV}" >> ${GIT_VERSION_PREFIX_HEADER}; 2>&1
      fi

      # displays summary
      if test "x${GIT_PACKAGE_VERSION_VERBOSE}" == "xYES";then
         echo "Git Package Version:     ${GPV}";
         echo "Git Application Version: ${GAV}";
         echo "Git Build Version:       ${GBV}";
      else
         echo "${GPV}"
      fi
   fi;
fi

# end of script