summaryrefslogtreecommitdiff
path: root/buildutil/version-util
blob: 24e9b789bd4143cc82da2c66e85caf7b2aac0307 (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
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
123
124
125
#!/bin/sh
# -----------------------------------------------------------------------------
# Shell script to determine the last commit version of the project It
# checks for CVS and .git repositories.  The output is a string that
# can be used in a define at compile time to automatically mark
# repository versions.  For CVS repositories the string contains the
# date and time of the commit that lead to the current version of
# files.  For git repositories the output contains the git-id of the
# current tree.  An indication if localy modified files exist is
# added currently only for CVS.
# -----------------------------------------------------------------------------

[ $# -lt 1 ] && echo "USAGE: version-util [-F] <versionfile>" && exit 1

VERS_FILE=$1
FORCE_EMPTY=0

[ "$VERS_FILE" == "-F" -a $# -lt 2 ] && echo "USAGE: version-util [-F] <versionfile>" && exit 1

if [ $# -gt 1 ]; then
    VERS_FILE=$2
    FORCE_EMPTY=1
fi


SCRIPT_PATH=`dirname $0`

# echo "file: ${VERS_FILE}, force = $FORCE_EMPTY"
# exit 0

createVers()
{
cat <<EOF
/* ATTENTION: this file is automatically generated and will be overwritten!
 * Manual changes will get lost!
 */
#ifndef GEN_VERSION_SUFFIX_H
#define GEN_VERSION_SUFFIX_H
#define VERSION_SUFFIX "$1"
#endif
EOF
}

fileVersions()
{
    for file in `find . -wholename '*CVS/Entries' -print`
    do
	sed -e's/\//|/g' -e's/|/\//' $file \
	    | grep --label=$file -H '^/' \
	    | sed -e's/\/CVS\/Entries://' -e's/\.\///'
    done
}

cvsLog()
{
    echo "= == ===marker=== == ="
    cvs log -b -N 2> /dev/null
}

cvsData()
{
    fileVersions
    cvsLog
}

cvsVers()
{
    d=`cvsData \
	| awk -f ${SCRIPT_PATH}/version-util.awk \
	| sort -u \
	| tail -1 \
	| tr -d ' \-:'`
    m=`cvs status 2> /dev/null \
	| grep 'Status: Locally Modified' > /dev/null && echo "_MOD"`
    echo "_cvs_${d}${m}"
}

gitVers()
{
    b=`git branch \
	| grep '^*' \
	| sed -e's/^* //'`
    h=`git show --pretty=format:"%h_%ci" HEAD \
	| head -1 \
	| tr -d ' \-:'`
    echo "_git_${b}_${h}"
}

emptyVers()
{
    echo ""
}

checkVers()
{
    s=`$1`
    if [ ! -e ${VERS_FILE} ]; then
	echo "$VERS_FILE does not exist! creating a new one."
	createVers $s > ${VERS_FILE}
    else
	v=`grep '^#define VERSION_SUFFIX' ${VERS_FILE} \
	    | awk '{print $3}'`
	if [ "$v" != "\"$s\"" ]; then
	    echo "$VERS_FILE is being recreated!"
	    createVers $s > ${VERS_FILE}
	fi
    fi
}

if [ $FORCE_EMPTY -eq 1 ]; then
    checkVers emptyVers
    exit 0
fi

if [ -d CVS ]; then
    checkVers cvsVers
    exit 0
fi

if [ -d .git ]; then
    checkVers gitVers
    exit 0
fi

checkVers emptyVers