1
1
#! /bin/sh
2
2
3
3
usage=" USAGE
4
- $0 [--clean] <remote> <branch-prefix>
4
+ $0 [--clean] [ <remote> [ <branch-prefix>]]
5
5
push each commit from stdin to its own branch.
6
6
7
+ DESCRIPTION
8
+ Read commit <sha1> hashes from stdin, one per line, and
9
+ for each one, push a branch to <remote>.
10
+ The branch is named following the <branch-prefix>-<sha1>
11
+ pattern and points to <sha1>.
12
+ By default <branch-prefix> is 'gpe-' and <remote> is
13
+ 'origin'.
14
+
7
15
OPTIONS
8
16
--clean delete all branches matching pattern:
9
17
<branch-prefix><hash>
@@ -25,6 +33,12 @@ EXAMPLE
25
33
# defaults
26
34
remote=" origin"
27
35
prefix=" gpe-"
36
+
37
+ die () {
38
+ echo >&2 " $@ "
39
+ exit 1
40
+ }
41
+
28
42
clean=0
29
43
dryrun=0
30
44
@@ -42,7 +56,7 @@ while [ $# -gt 0 ]; do
42
56
dryrun=1
43
57
shift ;;
44
58
--* )
45
- die " $( eval_gettext " unrecognised option: '\ $ arg'" ) " ;;
59
+ die " Unrecognised option: '$arg '\n $usage " ;;
46
60
* )
47
61
# get args
48
62
case " $# " in
@@ -54,29 +68,37 @@ while [ $# -gt 0 ]; do
54
68
1)
55
69
remote=$1
56
70
shift ;;
71
+ * )
72
+ die " Too many arguments: '$@ '\n$usage " ;;
57
73
esac ;;
58
74
esac
59
75
done
60
76
77
+ test -n " $remote " || die " Empty <remote> argument\n$usage "
78
+ test -n " $prefix " || die " Empty <branch-prefix> argument\n$usage "
79
+
61
80
# clean up mode
62
81
if [ $clean -eq 1 ]; then
63
- spec=" ($prefix [0-9a-f]{40})"
64
- for line in $( git branch -r | egrep -wo " $remote /$spec " | egrep -o " $spec " )
82
+ spec=" ($prefix [0-9a-fA-F]{40})"
83
+ temp=$( mktemp) || die " mktemp failed"
84
+ git branch -r > " $temp " || die " Could not list remote branches"
85
+ for line in $( egrep -wo " $remote /$spec " " $temp " | egrep -o " $spec " )
65
86
do
66
- echo " git push $remote :$line "
67
- if [ $dryrun -eq 0 ]; then
68
- git push " $remote " " :$line "
69
- fi
87
+ echo " git push $remote :$line "
88
+ if [ $dryrun -eq 0 ]; then
89
+ git push " $remote " " :$line " || die " Could not git push ' $remote ' ': $line ' "
90
+ fi
70
91
done
92
+ rm " $temp "
71
93
exit 0
72
94
fi
73
95
74
96
# push mode
75
- for line in $( egrep -wo [0-9a-f ]{40})
97
+ for line in $( egrep -wo [0-9a-fA-F ]{40})
76
98
do
77
99
spec=" $line :refs/heads/$prefix$line "
78
100
echo " git push $remote $spec "
79
101
if [ $dryrun -eq 0 ]; then
80
- git push " $remote " " $spec "
102
+ git push " $remote " " $spec " || die " Could not git push ' $remote ' ' $spec ' "
81
103
fi
82
104
done
0 commit comments