93a9a1e2104a6f0b45ed36d71e900c7848b5fa7f
[gentoolkit.git] / src / ebump / ebump
1 #! /bin/sh
2 #
3 # Copyright (c) 2004 Karl Trygve Kalleberg <karltk@gentoo.org>
4 # Copyright (c) Gentoo Technologies, Inc.
5 # Licensed under the GNU General Public License, version 2
6 #
7 # Maintainer: Karl Trygve Kalleberg <karltk@gentoo.org>
8
9 __version__="0.1.0"
10 __author__="Karl Trygve Kalleberg"
11 __email__="<karltk@gentoo.org>"
12 __description__="Ebuild version bumping tool"
13
14
15
16 die() {
17         echo $1 >&2
18         exit -1
19 }
20
21 einfo() {
22         if [ ${opt_verbosity:-0} -eq 1 ] ; then
23                 echo $*
24         fi
25 }
26
27 print_version() {
28         echo "${__description__}, v${__version__}"
29         echo "Copyright (c) 2004 ${__author__} ${__email__}"
30         echo "Copyright (c) 2004 Gentoo Technologies, Inc."
31         echo "Licensed under the GNU General Public License, version 2"
32 }
33
34 print_usage() {
35         echo "Usage: ebump <options> foo<.ebuild>"
36         echo "Ebuild version bumping tool, v${__version__}"
37         echo "  -V|--version           show version info"
38         echo "  -v|--verbose           increase verbosity"
39         echo "  -q|--quiet             turn off output"
40         echo "  -a|--no-auxfiles       don't bump auxfiles (files/*)"
41         echo "  -c|--no-changelog      do not update ChangeLog (via echangelog)"
42         echo "  -C|--no-vcs            do not add to VCS"
43         echo "  -m|--message           append message to ChangeLog"
44         echo "  -d|--delete-old        delete previous revision from VCS (DANGEROUS!)"
45 }
46
47 #
48 # Load options from /etc/gentoolkit/ebump.conf and ${HOME}/.gentoo/ebump.conf
49 # Home directory file takes precedence.
50 #
51 load_options() {
52         # FIXME: Sourcing config files like this is really a bad idea; users may
53         # easily override any function in this program inside his config files.
54         if [ -f "/etc/gentoolkit/ebump.conf" ] ; then
55                 . /etc/gentoolkit/ebump.conf
56         fi
57         if [ -f "${HOME}/.gentoo/gentool-env" ] ; then
58                 . ${HOME}/.gentoo/gentool-env
59         fi
60         if [ -f "${HOME}/.gentoo/ebump.conf" ] ; then
61                 . ${HOME}/.gentoo/ebump.conf
62         fi
63
64         # FIXME: remove this warning in 2-3 releases.
65         if [ -n "${opt_add_cvs}" ]; then
66                 echo "Warning: opt_add_cvs is deprecated, please use opt_add_vcs from now on!" >&2
67         fi
68 }
69
70 #
71 # Find closes ebuild to ${1}, if any
72 #
73 find_ebuild() {
74         local f=${1}
75
76         if [ -f "${f}" ] ; then
77                 echo ${f}
78         fi
79
80         if [ -f "${f}.ebuild" ] ; then
81                 echo ${f}
82         fi
83 }
84
85 #
86 # splitname (version|name|revision) package-name-version-revision
87 #
88 splitname() {
89         case $1 in
90                 version)
91                         echo ${2} | sed -r "s/.*-([0-9].*)/\1/"
92                 ;;
93                 name)
94                         name=$(echo ${2} | sed -r "s/(.*)-[0-9].*/\1/")
95                         if [ ${name} == ${2} ] ; then
96                                 if [ $(echo ${2} | grep "^[0-9].*") ] ; then
97                                         # The filename starts with a version number, thus it has no
98                                         # name
99                                         name=""
100                                 else
101                                         # The filename doesn't have a recognizeable version number;
102                                         # everything is a name
103                                         name=${2}
104                                 fi
105                         fi
106                         echo ${name}
107                 ;;
108                 revision)
109                         rev=$(echo ${2} | sed -r "s/.*-r([0-9][0-9]*)/\1/")
110                         if [ ${rev} == ${2} ] ; then
111                                 rev=0
112                         fi
113                         echo ${rev}
114                 ;;
115                 vernorev)
116                         ver=$(echo ${2} | sed -r "s/.*-([0-9].*)-r[0-9]+/\1/")
117                         if [ ${ver} == ${2} ] ; then
118                                 ver=$(echo ${2} | sed -r "s/.*-([0-9].*)/\1/")
119                         fi
120                         echo ${ver}
121                 ;;
122                 *)
123                         echo
124                 ;;
125         esac
126 }
127
128 process_ebuild() {
129         local vcs=$1
130         local ebuild_arg=$2
131         shift $#
132
133         # Files to add to VCS
134         local addfiles=""
135         # Files to remove from VCS
136         local delfiles=""
137
138         if [ -z "${ebuild_arg}" ] ; then
139                 print_usage
140                 exit
141         fi
142
143         #
144         # Try to find a matching ebuild
145         #
146         local ebuild_name=$(find_ebuild ${ebuild_arg})
147         if [ -z "${ebuild_name}" ] ; then
148                 die "Could not find ${ebuild_arg}"
149         fi
150         einfo "Processing ebuild ${ebuild_name}"
151
152         #
153         # Bump revision suffix (or add one)
154         #
155         local PF=$(basename ${ebuild_name} .ebuild)
156         local PN=$(splitname name ${PF})
157         local PV=$(splitname version ${PF})
158         local rev=$(splitname revision ${PF})
159         local PV_norev=$(splitname vernorev ${PF})
160         local newPF=${PN}-${PV_norev}-r$[rev+1]
161
162 #       echo $PF / $PN / $PV / $rev / $PV_norev / $newPF
163
164         einfo "Bumped ${PF}.ebuild to ${newPF}.ebuild"
165
166         if [ "${vcs}" == "svn" ]; then
167                 svn cp ${PF}.ebuild ${newPF}.ebuild
168         else
169                 cp ${PF}.ebuild ${newPF}.ebuild
170         fi
171
172         einfo "Reset keywords to ~arch"
173
174         ekeyword '~all' "${newPF}.ebuild"
175
176         addfiles="${addfiles} ${newPF}.ebuild"
177         delfiles="${delfiles} ${PF}.ebuild"
178
179         #
180         # (Optional) Bump relevant files in files/
181         #
182         if [ "${opt_bump_auxfiles}" == "y" ] ; then
183                 # Gather list of auxiliary files in files/ that has a versioned
184                 # filename, where the version matches our current version.
185
186                 local bumplist=""
187                 for x in $(echo files/*) ; do
188                         if [ ! -z "$(echo $x | grep "${PV}$")" ] ; then
189                                 bumplist="${bumplist} ${x}"
190                         fi
191                 done
192
193                 # Bump version of all matches
194                 for x in ${bumplist} ; do
195                         local bn=$(basename ${x})
196                         local dn=$(dirname ${x})
197                         local newbn
198
199                         PN=$(splitname name ${bn})
200                         PV=$(splitname version ${bn})
201                         rev=$(splitname revision ${bn})
202                         PV_norev=$(splitname vernorev ${bn})
203
204 #                       echo $PN / ${PV_norev} / ${rev}
205
206                         # Special case for when we have no name part; filename
207                         # is just a version number
208                         if [ -z "${PN}" ] ; then
209                                 newbn=${PV_norev}-r$[rev+1]
210                         else
211                                 newbn=${PN}-${PV_norev}-r$[rev+1]
212                         fi
213
214                         if [ -d ${dn}/${bn} ] ; then
215                                 if [ -e ${dn}/${newbn} ] ; then
216                                         echo "Directory ${dn}/${newbn} exists, not copying" >&2
217                                 else
218                                         cp -a ${dn}/${bn} ${dn}/${newbn}
219                                         # uhm, is that necessary?
220 #                                       find ${dn}/${newbn} -name CVS | xargs rm -rf
221                                 fi
222                         else
223                                 cp ${dn}/${bn} ${dn}/${newbn}
224                         fi
225
226                         addfiles="${addfiles} ${dn}/${newbn}"
227                         delfiles="${delfiles} ${dn}/${bn}"
228
229                         einfo "Bumped ${dn}/${bn} to ${dn}/${newbn}"
230                 done
231         fi
232
233 #       echo "addfiles ${addfiles}"
234 #       echo "delfiles ${delfiles}"
235
236         #
237         # (Optional) Add VCS entry for all new files
238         #
239         if [ "${opt_add_vcs}" == "y" ] ; then
240                 for x in ${addfiles} ; do
241                         if [ -d ${x} ] ; then
242                                 find ${x} -exec ${vcs} add {} ';'
243                         else
244                                 ${vcs} add ${x}
245                         fi
246                 done
247                 einfo "Added ${addfiles} to VCS"
248         fi
249
250
251         #
252         # (Optional) Delete previous entry
253         #
254         # Could we use 'rm' instead of remove for all vcs?
255         if [ "${opt_delete_old}" == "y" ] ; then
256                 for x in ${delfiles} ; do
257                         if [ "${vcs}" == "cvs" ]; then
258                                 ${vcs} remove -f ${x}
259                         elif [ "${vcs}" == "git" ]; then
260                                 ${vcs} rm ${x}
261                         else
262                                 ${vcs} remove ${x}
263                         fi
264                 done
265                 einfo "Removed ${delfiles} from VCS"
266         fi
267
268         #
269         # (Optional) Add ChangeLog entry
270         #
271         if [ "${opt_add_changelog}" == "y" ] && [ "${opt_add_vcs}" == "y" ]; then
272                 # FIXME: remove this warning in 2-3 releases
273                 if [ -n "${AUTHORNAME}" ] || [ -n "${AUTHOREMAIL}" ]; then
274                         echo "Warning: AUTHORNAME and AUTHOREMAIL is deprecated!" >&2
275                         echo "Please take a look at echangelog(1)." >&2
276                         echo "To avoid this warning unset AUTHORNAME and AUTHOREMAIL." >&2
277                 fi
278
279                 echangelog "${opt_commitmessage}" || set $?
280
281                 if [ ${1:-0} -ne 0 ]; then
282                         einfo "Modifying ChangeLog failed!"
283                 else
284                         einfo "Added ChangeLog entry"
285                 fi
286         fi
287 }
288
289 get_vcs() {
290         if [ -d "CVS" ]; then
291                 echo "cvs"
292                 return 0
293         elif [ -d ".svn" ]; then
294                 echo "svn"
295                 return 0
296         else
297                 if [ -x "$(which git)" ]; then
298                         if [ -n "$(git rev-parse --git-dir 2>/dev/null)" ]; then
299                                 echo "git"
300                                 return 0
301                         fi
302                 fi
303
304                 echo
305                 return 1
306         fi
307 }
308
309 original_params=${#}
310
311 #
312 # Global options
313 #
314 opt_verbosity=0
315 opt_add_changelog=y
316 opt_add_vcs=y
317 opt_bump_auxfiles=y
318 opt_delete_old=n
319 opt_commitmessage=""
320
321 load_options
322
323 while [ ${#} -gt 0 ] ; do
324         arg=${1}
325         shift
326
327         case ${arg} in
328                 -h|--help)
329                         print_usage
330                         exit 0
331                 ;;
332                 -m|--message)
333                         opt_commitmessage="${1}"
334                         shift
335                         continue
336                 ;;
337                 -a|--no-auxfiles)
338                         opt_bump_auxfiles=n
339                         continue
340                 ;;
341                 -c|--no-changelog)
342                         opt_add_changelog=n
343                         continue
344                 ;;
345                 -C|--no-vcs)
346                         opt_add_vcs=n
347                         continue
348                 ;;
349                 -V|--version)
350                         print_version
351                         exit
352                 ;;
353                 -v|--verbose)
354                         opt_verbosity=1
355                         continue
356                 ;;
357                 -q|--quiet)
358                         opt_verbosity=0
359                         continue
360                 ;;
361                 -d|--delete-old)
362                         opt_delete_old=y
363                         continue
364                 ;;
365                 *)
366                         ebuild_arg=${arg}
367                         continue
368                 ;;
369         esac
370 done
371
372 vcs=$(get_vcs)
373 if [ -z "${vcs}" ]; then
374         echo "Warning: no cvs, git or svn repository found!" >&2
375         echo "Changes can't be added to the VCS" >&2
376         opt_add_vcs=n
377         opt_delete_old=n
378 fi
379 process_ebuild "${vcs}" ${ebuild_arg}
380
381 # TODO:
382 # - put cli parser into separate functions