-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathgetversion.sh
executable file
·144 lines (128 loc) · 3.7 KB
/
getversion.sh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/sh
#
# Create svnversion.h file or get package version
#
# Home page of code is: https://www.smartmontools.org
#
# Copyright (C) 2024 Christian Franke
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# $Id$
#
set -e
myname=$0
usage()
{
cat <<EOF
Usage: $myname [-i] [-pq] [-r] [METHOD...] [> svnversion.h]
-i Print new contents of svnversion.h
-p Print package version from configure.ac
-q Prepend 'pre-' to package version if pre-release (implies -p)
-r Print svn revision (like 'svnversion')
METHOD... One or more of 'svn', 'ids' or 'git' [default: svn ids git]
EOF
exit 1
}
error()
{
echo "$myname: $*" >&2
exit 1
}
i_opt=false; p_opt=false; q_opt=false; r_opt=false
while true; do case $1 in
-i) i_opt=true ;;
-p) p_opt=true ;;
-q) p_opt=true; q_opt=true ;;
-r) r_opt=true ;;
-*) usage ;;
*) break ;;
esac; shift; done
$i_opt || $p_opt || $r_opt || usage
if [ $# -ne 0 ]; then
methods=$*
for m in $methods; do case $m in
svn|ids|git) ;;
*) usage ;;
esac; done
else
methods="svn ids git"
fi
srcdir=${myname%/*}
test "$srcdir" != "$myname" || error 'unknown $srcdir'
files="
ChangeLog NEWS Makefile.am configure.ac smart*.in
*.cpp *.h os_win32/*.cpp os_win32/*.h
"
(cd "$srcdir" && ls -d $files >/dev/null) || error "sources not found in $srcdir"
revision_from_svn()
{
local r t
r=$(cd "$srcdir" && svnversion 2>/dev/null) || return 1
case $r in [1-9][0-9]*) ;; *) return 1 ;; esac
t=$(cd "$srcdir" && TZ='' LC_ALL=C svn info 2>/dev/null) || return 1
t=$(echo "$t" | sed -n 's,^.* Date: *\(2[-0-9]*\) \([0-9][:0-9]*\) .*$,\1 \2,p')
test -n "$t" || return 1
svnrev=$r; revdate=${t% *}; revtime=${t#* }
origin="svn info"
}
revision_from_ids()
{
local x
x=$(cd "$srcdir" && \
sed -n 's,^.*\$[I][d]: [^ ]* \([0-9][0-9]*\) \([0-9][-0-9]*\) \([0-9][:0-9]*\)Z [^$]*\$.*$,\1 \2 \3,p' $files \
| sort -n -r | sed -n 1p)
test -n "$x" || return 1
svnrev=${x%% *}; x=${x#* }; revdate=${x% *}; revtime=${x#* }
origin="Id strings"
}
revision_from_git()
{
local h r t x
x=$(cd "$srcdir" && TZ='' LC_ALL=C git log -1 --date=iso 2>/dev/null) || return 1
h=$(echo "$x" | sed -n 's,^commit \([0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]\).*$,\1,p')
t=$(echo "$x" | sed -n 's,^Date: *\(2[-0-9]*\) \([0-9][:0-9]*\) .*$,\1 \2,p')
r=$(echo "$x" | sed -n 's,^.*git-svn-id: [.:/a-z]*/smartmontools/code/trunk@\([1-9][0-9]*\) 4ea69e.*$,\1,p')
test "${h:+y}${r:+y}${t:+y}" = "yyy" || return 1
test -z "$(cd "$srcdir" && git status -s -uno 2>&1)" || r="${r}M"
svnrev=$r; revdate=${t% *}; revtime=${t#* }; githash=$h
origin="git log"
}
if $i_opt || $r_opt; then
svnrev=; revdate=; revtime=; githash=
origin="missing information"
for m in $methods; do
revision_from_$m || continue
break
done
if $i_opt; then
cat <<EOF
/* svnversion.h. Generated by ${myname##*/} from $origin. */
EOF
test -z "$svnrev" || cat << EOF
#define SMARTMONTOOLS_SVN_REV "$svnrev"
#define SMARTMONTOOLS_SVN_DATE "$revdate"
#define SMARTMONTOOLS_SVN_TIME "$revtime"
EOF
test -z "$githash" || cat <<EOF
#define SMARTMONTOOLS_GIT_HASH "$githash"
EOF
fi
if $r_opt; then
test -n "$svnrev" || error "svn revision not found using $methods"
echo "$svnrev"
fi
fi
if $p_opt; then
p=$(sed -n 's|^AC_INIT[^,]*, *\[\([0-9.]*\)\] *,.*$|\1|p' "$srcdir/configure.ac") || exit 1
test -n "$p" || error "package version not found in $srcdir/configure.ac"
if $q_opt; then
x=$(sed -n 's|^smartmontools_release_date=\(.*\)$|\1|p' "$srcdir/configure.ac") || exit 1
case $x in
20*) ;;
\ \#*) p="pre-$p" ;;
*) error "unable to detect pre-release state in $srcdir/configure.ac"
esac
fi
echo "$p"
fi