~budgester/irm/trunk

1 by budgester at budgester
Initial import of IRM codebase
1
#!/bin/sh
2
3
# BAz iRM -- a set of commands to work with the IRM revision control archives
4
#
5
# For help, run 'barm help'.
6
#
7
8
# Master archive name.  Do not change this!
9
MASTER="irm-pqm@hezmatt.org--2005"
10
11
function main
12
{
13
	CMD="$1"
14
	shift
15
	
16
	if ! function_exists "cmd_$CMD"; then
17
		echo "Unknown command ($CMD)  try $(basename $0) help"
18
		exit 1
19
	fi
20
21
	if [ "$1" = "-h" ]; then
22
		if function_exists "cmd_$CMD_help"; then
23
			cmd_${CMD}_help
24
			exit 0
25
		else
26
			echo "I don't have any help about '$CMD'; sorry about that"
27
			exit 1
28
		fi
29
	fi
30
31
	cmd_$CMD "$@"
32
	
33
	exit $?
34
}
35
36
function get_latest_trunk
37
{
38
	baz browse --versions "${MASTER}/irm--trunk" | tail -n 1
39
}
40
41
function get_branch_version
42
{
43
	baz parse-package-name -v "$1"
44
}
45
46
function get_trunk_version
47
{
48
	get_branch_version $(get_latest_trunk)
49
}
50
51
function get_ancestor
52
{
53
	baz ancestry | head -n 4 | tail -n 1
54
}
55
56
function check_baz_version
57
{
58
	if [ -z "$(command -v baz)" ]; then
59
		echo "The baz command could not be found.  Please place baz in your path"
60
		exit 1
61
	fi
62
	
63
	if ! baz -V | grep -q '^Bazaar version 1\.5'; then
64
		echo "The barm command requires bazaar version 1.5 (or a pre-release thereof)."
65
		echo "Please upgrade your copy of baz."
66
		exit 1
67
	fi
68
}
69
70
function write_config
71
{
72
	cat << EOF > $HOME/.barmrc
73
# Your e-mail address, including your name.  Keep the format as it is, or
74
# demons may fly out of your nose.
75
EMAIL="Jane User <user@host.com>"
76
77
# Your local archive.  It should be in the form "your@email.address--irm"
78
ARCHIVE="your@email.address--irm"
79
80
# A directory in which to put your local archive
81
ARCHIVEDIR="$HOME/.arch-ives"
82
83
# A mirror location.  Make it somewhere web-accessable.  See
84
# http://www.stackworks.net/wiki/moin.cgi/IrmInArch for more info on this
85
# parameter.
86
#MIRRORLOCATION="sftp://user@www.host.com/home/user/public_html/arch"
87
88
EOF
89
}
90
91
function function_exists
92
{
93
	if declare -F | grep -q "$1"; then
94
		return 0
95
	else
96
		return 1
97
	fi
98
}
99
100
function cmd_help
101
{
102
	cat << EOF
103
104
Valid subcommands
105
-------------------
106
107
init	-- Setup archives and metadata
108
109
branch	-- Create a new branch from the development trunk
110
get	-- Retrieve a branch from the local archive for editing
111
112
push	-- Send a patch or branch to the PQM (requires authorization)
113
pull	-- Retrieve all new changes from the PQM into the current working copy
114
lub	-- List Unmerged Branches in your archive
115
116
bump	-- Retarget a branch against a new trunk version
117
118
mirror	-- Synchronise mirror with master archive
119
120
EOF
121
}
122
123
function cmd_help_help
124
{
125
	cmd_help
126
}
127
128
function cmd_branch_help
129
{
130
	cat << EOF
131
create a branch from the development trunk
132
133
Usage: $(basename $0) branch [branch-root] <new-name>
134
135
[branch-root] should be a full branch name; current trunk will be used if not
136
specified
137
138
<new-name> is the name of the new branch (just the name)
139
EOF
140
}
141
142
function cmd_branch
143
{
144
	if [ -z "$2" ]; then
145
		EXISTING=$(get_latest_trunk)
146
		SRCARCHIVE="$MASTER"
147
		NEWBRANCH="$1"
148
	else
149
		EXISTING="$1"
150
		SRCARCHIVE="$MASTER"
151
		NEWBRANCH="$2"
152
	fi
153
	
154
	EXISTING=${EXISTING// /}
155
	
156
	if [ -z "$NEWBRANCH" ]; then
157
		echo "No new branch given"
158
		echo "Try $(basename $0) branch -h"
159
		exit 1
160
	fi
161
	VER=$(baz parse-package-name -v $EXISTING)
162
		
163
	if [ -z "$VER" ]; then
164
		echo "No version in $EXISTING"
165
		exit 1
166
	fi
167
168
	echo "Going to make a branch of $SRCARCHIVE/$EXISTING called irm--$NEWBRANCH--$VER"
169
		
170
	baz branch "$SRCARCHIVE/$EXISTING" "$ARCHIVE/irm--$NEWBRANCH--$VER"
171
	baz get "${ARCHIVE}/irm--$NEWBRANCH--$VER" "$NEWBRANCH"
172
		
173
	rm -f testbranch
174
	ln -s "$NEWBRANCH" testbranch
175
}
176
177
function cmd_get_help
178
{
179
	cat << EOF
180
Retrieve the latest revision of a specified branch.
181
182
Usage: $(basename $0) get <branch> [ver]
183
184
<branch> should be just a branch name.
185
186
[ver] is the version of the branch to retrieve.  If left empty, this will
187
	default to the latest version of the branch available.
188
EOF
189
}
190
191
function cmd_get
192
{
193
	BRANCH="$1"
194
	VER="$2"
195
		
196
	if [ -z $VER ]; then
197
		VER=$(get_trunk_version)
198
	fi
199
200
	if [ -z "$BRANCH" ]; then
201
		echo "No branch specified!"
202
		exit 1
203
	fi
204
		
205
	if [ -z $VER ]; then
206
		echo "No version given and no trunk version found"
207
		exit 1
208
	fi
209
		
210
	baz get $ARCHIVE/irm--$BRANCH--$VER $BRANCH
211
	rm -f testbranch
212
	ln -s "$BRANCH" testbranch
213
}
214
215
function cmd_push_help
216
{	
217
	cat << EOF
218
Send a set of changes to the PQM integration branch.
219
220
Usage: $(basename $0) push [source] [description]
221
222
[source] is a full branch identifier to take the changes from; the default
223
	is to push the current working tree to the PQM.
224
225
[description] is a string describing the changes to the PQM; if none is given,
226
	you will be prompted for it.  Remember to enclose your summary in
227
	quotes.
228
EOF
229
}
230
231
function cmd_push
232
{
233
	if baz valid-package-name -tv "$1" 2>/dev/null; then
234
		SRC="$1"
235
		DESC="$2"
236
	elif baz valid-package-name -tv "$2" 2>/dev/null; then
237
		SRC="$2"
238
		DESC="$1"
239
	else
240
		SRC=""
241
		DESC="$1"
242
	fi
243
244
	if [ -z "$SRC" ]; then
245
		SRC=$(baz tree-version)
246
	fi
247
		
248
	if [ -z "$DESC" ]; then
249
		read -p "Please describe these changes: " DESC
250
	fi
251
252
	UPSTREAM=$(cat $(baz tree-root)/\{arch\}/=upstream)
253
		
254
	if echo $SRC | egrep -q '(patch|version|versionfix)-[[:digit:]]+$'; then
255
		# We've been handed a single patch -- replay it!
256
		PUSHCMD="replay $SRC $UPSTREAM"
257
	else
258
		PUSHCMD="star-merge $SRC $UPSTREAM"
259
	fi
260
		
261
	echo $PUSHCMD | gpg --clearsign | mail -s "$DESC" irm-pqm@hezmatt.org
262
}
263
264
function cmd_pull_help
265
{
266
	cat << EOF
267
Merge changes from the PQM integration branch into the current working tree.
268
269
Usage: $(basename $0) pull
270
EOF
271
}
272
273
function cmd_pull
274
{
275
	if ! baz status >/dev/null; then
276
		echo "You have uncommitted changes in your tree.  Please commit before continuing"
277
		exit 1
278
	fi
279
		
280
	UPSTREAM=$(cat $(baz tree-root)/\{arch\}/=upstream)
281
		
282
	baz merge "$UPSTREAM" || true
283
	if [ -e '{arch}/+rejects-exist' ]; then
284
		# Give the user a chance to correct the conflicts.
285
		# With thanks to dpatch-edit-patch for the cool idea
286
		# of a subshell.
287
		echo "There were conflicts between the master archive and your changes."
288
		echo
289
		echo "I will now drop you into a subshell so you can correct the conflicts before"
290
		echo "committing."
291
		echo
292
		echo "If you would like to abort this process, exit the shell so it returns an"
293
		echo "exit code of 230.  This is typically done by running the command 'exit 230'."
294
		$SHELL && EXITVAL="0" || EXITVAL="$?"
295
		if [ "$EXITVAL" = "230" ]; then
296
			echo "Shell returned 230; aborting"
297
			popd
298
			rm -rf $WORKDIR
299
			exit 1
300
		fi
301
		find . -name \*.orig -or -name \*.rej | xargs rm -f
302
		baz resolved --all
303
	fi
304
305
	if baz status >/dev/null; then
306
		echo "No new changes"
307
		exit 0
308
	fi
309
310
	baz commit -s "Pulled new changes from $UPSTREAM"
311
}
312
313
function cmd_rc_help
314
{
315
	cat << EOF
316
Create a new release candidate branch.  Only useful to the release manager.
317
318
Usage: $(basename $0) rc <version>
319
EOF
320
}
321
	
322
function cmd_rc
323
{
324
	if [ "$ARCHIVE" != "mpalmer@hezmatt.org--irm-2004" ]; then
325
		echo "This command is only of use to the RM"
326
		exit 0;
327
	fi
328
	VER="$1"
329
330
	baz branch $( baz tree-id ) "$ARCHIVE/irm--release-candidates--$VER"
331
	cd ..
332
	baz get "$ARCHIVE/irm--release-candidates--$VER" "rc$VER"
333
}
334
335
function cmd_lub_help
336
{
337
	cat << EOF
338
List Unmerged Branches in your local IRM archive.
339
340
Performs a browse over your local archive, listing all branches which
341
have patches that aren't present in the current upstream PQM branch.
342
343
Primarily useful to see which branches you still have under active
344
development, and provide a hint as to which branches you still need
345
to push to the PQM.
346
347
Usage: $(basename $0) lub
348
EOF
349
}
350
351
function cmd_lub
352
{
353
	PQM=$(get_latest_trunk)
354
	PQM=${MASTER}/${PQM// /}
355
	PQMVER=$(baz parse-package-name -v $PQM)
356
	branches=$(baz browse --versions --no-tree-view --regex "${ARCHIVE}/.*${PQMVER}")
357
	for b in $branches; do
358
		baz missing --from $PQM $b | grep -q "^$ARCHIVE" && echo $b
359
	done
360
}
361
362
function cmd_bump_help
363
{
364
	cat << EOF
365
Retarget an ongoing line of development against a new PQM version branch.
366
367
Usage: $(basename $0) bump <branch> <from-version> <to-version>
368
EOF
369
}
370
371
function cmd_bump
372
{
373
	BRANCH="$1"
374
	FROM="$2"
375
	TO="$3"
376
377
	if [ "$#" != "3" ]; then
378
		echo "Incorrect argument count!"
379
		echo "Usage: irm bump <branch> <from-version> <to-version>"
380
		exit 1
381
	fi
382
		
383
	WORKDIR=$( mktemp -d )
384
		
385
	pushd "$WORKDIR"
386
	baz branch "$MASTER/irm--trunk--$TO" "$ARCHIVE/irm--$BRANCH--$TO"
387
		
388
	baz get "$ARCHIVE/irm--$BRANCH--$TO" "$BRANCH"
389
	cd "$BRANCH"
390
	mergeoutput=$(mktemp)
391
	baz merge --star-merge --two-way -r	\
392
		"$MASTER/irm--trunk--$FROM"	\
393
		"$ARCHIVE/irm--$BRANCH--$FROM"	\
394
		|| true
395
	if [ -e '{arch}/+rejects-exist' ]; then
396
		# Give the user a chance to correct the conflicts.
397
		# With thanks to dpatch-edit-patch for the cool idea
398
		# of a subshell.
399
		echo "There were conflicts in the merge between the new trunk and your changes."
400
		echo
401
		echo "I will now drop you into a subshell so you can correct the conflicts before"
402
		echo "committing."
403
		echo
404
		echo "If you would like to abort this process, exit the shell so it returns an"
405
		echo "exit code of 230.  This is typically done by running the command 'exit 230'."
406
		$SHELL && EXITVAL="0" || EXITVAL="$?"
407
		if [ "$EXITVAL" = "230" ]; then
408
			echo "Shell returned 230; aborting"
409
			popd
410
			rm -rf $WORKDIR
411
			exit 1
412
		fi
413
		find . -name \*.orig -or -name \*.rej | xargs rm -f
414
		baz resolved --all
415
	fi
416
417
	baz commit -s "Updated $BRANCH for version $TO"
418
		
419
	popd
420
		
421
	rm -rf "$WORKDIR"
422
}
423
424
function cmd_init_help
425
{
426
	cat << EOF
427
Do some basic initialisation of the IRM development environment.
428
429
Usage: $(basename $0) init
430
EOF
431
}
432
433
function cmd_init
434
{
435
	if [ -d "$HOME/.arch-params" ]; then
436
		echo "You appear to already have a working Arch setup.  Aborting."
437
		exit 0
438
	fi
439
	baz my-id "$EMAIL"
440
	baz register-archive "http://www.hezmatt.org/~mpalmer/arch/$MASTER"
441
	if [ ! -d "$ARCHIVEDIR" ]; then
442
		mkdir "$ARCHIVEDIR"
443
	fi
444
	baz make-archive "$ARCHIVE" "$ARCHIVEDIR/$ARCHIVE"
445
	baz my-default-archive "$ARCHIVE"
446
		
447
	if [ -n "$MIRRORLOCATION" ]; then
448
		baz make-archive --listing --mirror "$ARCHIVE" "$MIRRORLOCATION"
449
	fi
450
}
451
452
function cmd_move
453
{
454
	baz make-archive --listing --mirror "$ARCHIVE" "$MIRRORLOCATION"
455
}
456
function cmd_mirror_help
457
{
458
	cat << EOF
459
Update a existing mirror of your IRM development work.
460
461
Usage: $(basename $0) mirror
462
EOF
463
}
464
465
function cmd_mirror
466
{
467
	if [ -n "$MIRRORLOCATION" ]; then
468
		baz archive-mirror "$ARCHIVE"
469
	else
470
		echo "No mirror location defined."
471
	fi
472
}
473
474
if [ -f $HOME/.barmrc ]; then
475
	. $HOME/.barmrc
476
else
477
	write_config
478
	echo "No barm config found.  A template config file has been written to"
479
	echo "$HOME/.barmrc.  Please edit this file for your local configuration."
480
	exit 1
481
fi
482
483
check_baz_version
484
485
main "$@"
486