aboutsummaryrefslogtreecommitdiff
blob: 2623a280b719f366f72b056e1aaca78b8ee6015d (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#! /bin/sh
#
# Copyright (c) 2004 Karl Trygve Kalleberg <karltk@gentoo.org>
# Copyright (c) Gentoo Technologies, Inc.
# Licensed under the GNU General Public License, version 2
#
# Maintainer: Karl Trygve Kalleberg <karltk@gentoo.org>

__version__="0.1.0"
__author__="Karl Trygve Kalleberg"
__email__="<karltk@gentoo.org>"
__description__="Ebuild version bumping tool"



die() {
    echo $1 > /dev/stderr
    exit -1
}

einfo() {
    if [ ${opt_verbosity} -gt 2 ] ; then
	echo $*
    fi
}

print_version() {
    echo "${__description__}, v${__version__}"
    echo "Copyright (c) 2004 ${__author__} ${__email__}"
    echo "Copyright (c) 2004 Gentoo Technologies, Inc."
    echo "Licensed under the GNU General Public License, version 2"
}

print_usage() {
    echo "Usage: ebump <options> foo<.ebuild>"
    echo "Ebuild version bumping tool, v${__version__}"
    echo "  -V|--version           show version info"
    echo "  -v|--verbose           increase verbosity"
    echo "  -q|--quiet             turn off output"
    echo "  -C|--no-cvs            do not add to CVS"
    echo "  -m|--message           append message to ChangeLog"
    echo "  -d|--delete-old        delete previous revision from CVS (DANGEROUS!)"
}

#
# Load options from /etc/gentoolkit/ebump.conf and ${HOME}/.gentoo/ebump.conf
# Home directory file takes precedence.
#
load_options() {

    # FIXME: Sourcing config files like this is really a bad idea; users may
    # easily override any function in this program inside his config files.

    if [ -f /etc/gentoolkit/ebump.conf ] ; then
	. /etc/gentoolkit/ebump.conf
    fi
    if [ -f ${HOME}/.gentoo/gentool-env ] ; then
	. ${HOME}/.gentoo/gentool-env
    fi
    if [ -f ${HOME}/.gentoo/ebump.conf ] ; then
	. ${HOME}/.gentoo/ebump.conf
    fi
}

#
# Find closes ebuild to ${1}, if any
#
find_ebuild() {
    f=${1}

    if [ -f ${1} ] ; then
	echo ${1}
    fi

    if [ -f ${1}.ebuild ] ; then
	echo ${1}
    fi
}

#
# splitname (version|name|revision) package-name-version-revision
#
splitname() {
    case $1 in
	version)
	    echo ${2} | sed -r "s/.*-([0-9].*)/\1/"
	    ;;
	name)
	    name=$(echo ${2} | sed -r "s/(.*)-[0-9].*/\1/")
	    if [ ${name} == ${2} ] ; then
		if [ $(echo ${2} | grep "^[0-9].*") ] ; then
		    # The filename starts with a version number, thus it has no
                    # name
		    name=""
		else
		    # The filename doesn't have a recognizeable version number;
                    # everything is a name
		    name=${2}
		fi
	    fi
	    echo ${name}
	    ;;
	revision)
	    rev=$(echo ${2} | sed -r "s/.*-r([0-9][0-9]*)/\1/")
	    if [ ${rev} == ${2} ] ; then
		rev=0
	    fi
	    echo ${rev}
	    ;;
	vernorev)
	    ver=$(echo ${2} | sed -r "s/.*-([0-9].*)-r[0-9]+/\1/")
	    if [ ${ver} == ${2} ] ; then
		ver=$(echo ${2} | sed -r "s/.*-([0-9].*)/\1/")
	    fi
	    echo ${ver}
	    ;;
	*)
	    echo
    esac
}

process_ebuild() {
    ebuild_arg=${1}

    # Files to add to CVS
    addfiles=""
    # Files to remove from CVS
    delfiles=""

    if [ -z ${ebuild_arg} ] ; then
	print_usage
	exit
    fi

    #
    # Try to find a matching ebuild
    #

    ebuild_name=$(find_ebuild ${ebuild_arg})
    if [ -z ${ebuild_name} ] ; then
	die "Could not find ${ebuild_arg}"
    fi
    einfo "Processing ebuild ${ebuild_name}"

    #
    # Bump revision suffix (or add one)
    #

    PF=$(basename ${ebuild_name} .ebuild)
    PN=$(splitname name ${PF})
    PV=$(splitname version ${PF})
    rev=$(splitname revision ${PF})
    PV_norev=$(splitname vernorev ${PF})
    newPF=${PN}-${PV_norev}-r$[rev+1]

#    echo $PF / $PN / $PV / $rev / $PV_norev / $newPF

    einfo "Bumped ${PF}.ebuild to ${newPF}.ebuild"

    cp ${PF}.ebuild ${newPF}.ebuild

    addfiles="${addfiles} ${newPF}.ebuild"
    delfiles="${delfiles} ${PF}.ebuild"

    #
    # (Optional) Bump relevant files in files/
    #

    if [ "${opt_bump_auxfiles}" == "y" ] ; then

        # Gather list of auxiliary files in files/ that has a versioned
	# filename, where the version matches our current version.

	bumplist=""
	for x in $(echo files/*) ; do
	    if [ ! -z $(echo $x | grep "${PV}$") ] ; then
		bumplist="${bumplist} ${x}"
	    fi
	done

        # Bump version of all matches

	for x in ${bumplist} ; do

	    bn=$(basename ${x})
	    dn=$(dirname ${x})

	    PN=$(splitname name ${bn})
	    PV=$(splitname version ${bn})
	    rev=$(splitname revision ${bn})
	    PV_norev=$(splitname vernorev ${bn})

#	    echo $PN / ${PV_norev} / ${rev}

	    # Special case for when we have no name part; filename
	    # is just a version number
	    if [ -z "${PN}" ] ; then
		newbn=${PV_norev}-r$[rev+1]
	    else
		newbn=${PN}-${PV_norev}-r$[rev+1]
	    fi

	    if [ -d ${dn}/${bn} ] ; then
		if [ -e ${dn}/${newbn} ] ; then
		    echo "Directory ${dn}/${newbn} exists, not copying" > /dev/stderr
		else
		    cp -a ${dn}/${bn} ${dn}/${newbn}
		    find ${dn}/${newbn} -name CVS | xargs rm -rf
		fi
	    else
		cp ${dn}/${bn} ${dn}/${newbn}
	    fi

	    addfiles="${addfiles} ${dn}/${newbn}"
	    delfiles="${delfiles} ${dn}/${bn}"

	    einfo "Bumped ${dn}/${bn} to ${dn}/${newbn}"
	done
    fi

#    echo "addfiles ${addfiles}"
#    echo "delfiles ${delfiles}"

	filelist="${addfiles}"
    #
    # (Optional) Add ChangeLog entry
    #

    if [ "${opt_add_changelog}" == "y" ] &&
	[ -f ChangeLog ] ; then

        # Add ChangeLog entry

	curdate=$(LC_TIME="us" date +"%02d %b %Y")
	cp ChangeLog ChangeLog.old
	(
	    # Use header (four first lines) of the old ChangeLog
	    head -n 4 ChangeLog.old

	    # Write new entry

	    echo "*${newPF} (${curdate})"
	    echo
	    echo "  ${curdate}; ${AUTHORNAME} <${AUTHOREMAIL}> ${filelist}"

	    # If we don't have a commit message, add comment
	    if [ -z "${opt_commitmessage}" ] ; then
		echo "  # INSERT ENTRY HERE"
		if [ "${opt_delete_old}" == "y" ] && [ ! -z "${delfiles}" ] ; then
			echo "  Removed ${delfiles}."
		fi
		echo
	    else
		echo "  ${opt_commitmessage}"
		echo
	    fi

	    # Write tail of old ChangeLog
	    nl=$(wc -l ChangeLog.old | sed -r "s/^([0-9]+).*/\1/")
	    tail -n $[nl - 4] ChangeLog.old
	) > ChangeLog
	rm ChangeLog.old

	einfo "Added ChangeLog entry"
    fi

    #
    # (Optional) Add CVS entry for all new files
    #

    if [ "${opt_add_cvs}" == "y" ] ; then

	# Add all new files to CVS
	for x in ${addfiles} ; do
	    if [ -d ${x} ] ; then
		find ${x} | xargs echo cvs add
	    else
		cvs add ${x}
	    fi
	done
	einfo "Added ${addfiles} to CVS"
    fi


    #
    # (Optional) Delete previous entry
    #

    if [ "${opt_delete_old}" == "y" ] ; then

	for x in ${delfiles} ; do
	    cvs remove -f ${x}
	done
	einfo "Removed ${delfiles} from CVS"
    fi

}

original_params=${#}

#
# Global options
#
opt_verbosity=1
opt_warn_on_delete=y
opt_add_changelog=y
opt_add_cvs=y
opt_bump_auxfiles=y
opt_delete_old=n
opt_commitmessage=""

load_options

skip=0
while [ ${#} -gt 0 ] ; do
    arg=${1}
    shift
    if [ ${skip} -gt 0 ] ; then
	skip=$[skip-1]
    else
	case ${arg} in
	    -h|--help)
		print_usage
		exit 0
		;;
	    -m|--message)
		opt_commitmessage="${1}"
		skip=1
		;;
	    -C|--no-cvs)
		opt_add_cvs=n
		;;
	    -V|--version)
		print_version
		exit
		;;
	    -v|--verbose)
		opt_verbosity=$[opt_verbosity + 1]
		;;
	    -q|--quiet)
		opt_verbosity=0
		;;
	    -d|--delete-old)
		opt_delete_old=y
		;;
	    *)
		ebuild_arg=${arg}
		;;
	esac
    fi
done

process_ebuild ${ebuild_arg}

# TODO:
# - put cli parser into separate functions