Factor version munging capabilities out of release.sh.
[fw/openocd] / tools / release / version.sh
1 #!/bin/sh -e
2 # version.sh: openocd version process automation
3 # Copyright (C) 2009 by Zachary T Welch <zw@superlucidity.net>
4 # Release under the GNU GPL v2 (or later versions).
5
6 source "tools/release/helpers.sh"
7
8 do_version_usage() {
9         cat << USAGE
10 usage: $0 <command>
11 Version Commands:
12   tag {add|remove} <label>     Add or remove the specified tag.
13   bump {major|minor|micro|rc}  Bump the specified version number, and
14                                reset less-significant numbers to zero.
15   bump tag <label>             Add or bump a versioned tag (e.g. -rcN).
16   bump final <label>           Remove a versioned tag (e.g. -rcN).
17 USAGE
18 }
19
20 do_version_sed() {
21         local OLD_VERSION="${PACKAGE_VERSION}"
22         local NEW_VERSION="$1"
23         local MSG="$2"
24
25         sed -i -e "/AC_INIT/ s|${OLD_VERSION}|${NEW_VERSION}|" configure.in
26         package_info_load
27         echo "${MSG}: ${OLD_VERSION} -> ${NEW_VERSION}"
28 }
29 do_version_bump_sed() {
30         local NEW_VERSION="$1"
31         [ -z "${PACKAGE_VERSION_TAGS}" ] || \
32                 NEW_VERSION="${NEW_VERSION}${PACKAGE_VERSION_TAGS}"
33
34         do_version_sed "${NEW_VERSION}" \
35                 "Bump ${CMD} package version number"
36 }
37 do_version_bump_major() {
38         do_version_bump_sed "$((PACKAGE_MAJOR + 1)).0.0"
39 }
40 do_version_bump_minor() {
41         do_version_bump_sed "${PACKAGE_MAJOR}.$((PACKAGE_MINOR + 1)).0"
42 }
43 do_version_bump_micro() {
44         do_version_bump_sed "${PACKAGE_MAJOR_AND_MINOR}.$((PACKAGE_MICRO + 1))"
45 }
46 do_version_bump_tag() {
47         local TAG="$1"
48         [ "${TAG}" ] || die "TAG argument is missing"
49         local TAGS="${PACKAGE_VERSION_TAGS}"
50         if has_version_tag "${TAG}"; then
51                 local RC=$(do_version_tag_value "${TAG}")
52                 RC=$((${RC} + 1))
53                 TAGS=$(echo ${TAGS} | perl -npe "s/-${TAG}[\\d]*/-${TAG}${RC}/")
54         else
55                 TAGS="-${TAG}0${PACKAGE_VERSION_TAGS}"
56         fi
57         PACKAGE_VERSION_TAGS="${TAGS}"
58         do_version_bump_sed "${PACKAGE_VERSION_BASE}"
59 }
60 do_version_bump_final() {
61         local TAG="$1"
62         [ "${TAG}" ] || die "TAG argument is missing"
63         has_version_tag "${TAG}" || die "-${TAG} tag is missing"
64         do_version_tag_remove "${TAG}$(do_version_tag_value "${TAG}")"
65 }
66 do_version_bump() {
67         CMD="$1"
68         shift
69         case "${CMD}" in
70         major|minor|micro|final|tag)
71                 "do_version_bump_${CMD}" "$@"
72                 ;;
73         rc)
74                 do_version_bump_tag "rc"
75                 ;;
76         *)
77                 do_version_usage
78                 ;;
79         esac
80 }
81
82 has_version_tag() {
83         test "${PACKAGE_VERSION/-${1}/}" != "${PACKAGE_VERSION}"
84 }
85 do_version_tag_value() {
86         local TAG="$1"
87         echo ${PACKAGE_VERSION_TAGS} | perl -ne "/-${TAG}"'(\d+)/ && print $1'
88 }
89 do_version_tag_add() {
90         local TAG="$1"
91         has_version_tag "${TAG}" && \
92                 die "error: tag '-${TAG}' exists in '${PACKAGE_VERSION}'"
93         do_version_sed "${PACKAGE_VERSION}-${TAG}" \
94                 "Add '-${TAG}' version tag"
95 }
96 do_version_tag_remove() {
97         local TAG="$1"
98         has_version_tag "${TAG}" || \
99                 die "error: tag '-${TAG}' missing from '${PACKAGE_VERSION}'"
100         do_version_sed "${PACKAGE_VERSION/-${TAG}/}" \
101                 "Remove '-${TAG}' version tag"
102 }
103 do_version_tag() {
104         CMD="$1"
105         shift
106         case "${CMD}" in
107         add|remove)
108                 local i=
109                 for i in "$@"; do
110                         "do_version_tag_${CMD}" "${i}"
111                 done
112                 ;;
113         *)
114                 do_version_usage
115                 ;;
116         esac
117 }
118
119 do_version() {
120         CMD="$1"
121         shift
122         case "${CMD}" in
123         tag|bump)
124                 "do_version_${CMD}" "$@"
125                 ;;
126         commit)
127                 do_version_commit "$@"
128                 ;;
129         *)
130                 do_version_usage
131                 ;;
132         esac
133 }
134
135 package_info_load
136 do_version "$@"
137