-
Notifications
You must be signed in to change notification settings - Fork 11
/
git-mixdown
executable file
·161 lines (141 loc) · 4.24 KB
/
git-mixdown
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
#!/bin/bash
#
# git-mixdown - "mixdown" multiple branches into a combined working branch
#
# Useful for testing a combination of bugfixes / features at once via
# a throw-away temporary working branch. See accompanying utility git-mix.
#
# Copyright (C) 2013 Adam Spiers <git@adamspiers.org>
#
# The software in this repository is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
combined_branch=working
usage () {
# Call as: usage [EXITCODE] [USAGE MESSAGE]
exit_code=1
if [[ "$1" == [0-9] ]]; then
exit_code="$1"
shift
fi
if [ -n "$1" ]; then
echo >&2 "$*"
echo
fi
me=`basename $0`
cat <<EOF >&2
Usage: $me [options] [BASE-BRANCH BRANCH2 [BRANCH3 ...]]
Options:
-h, --help Show this help and exit
-b, --branch BRANCH Target branch for mixdown [default: $combined_branch]
-c, --checkout Leave target branch checked out
[default: restore previous checkout]
-s, --strategy STRATEGY Set merge strategy
Points the target branch at BASE-BRANCH and then mixes all the other
branches in. Leaves the working copy with the target branch checked out.
If no arguments are given, branches are read from the mixdown
section of git config instead, using only the keys corresponding
to the target branch.
EOF
exit "$exit_code"
}
parse_opts () {
checkout=
strategy=octopus
merge_opts=( )
while [ $# != 0 ]; do
case "$1" in
-h|--help)
usage 0
;;
-b|--branch)
combined_branch="$2"
shift 2
;;
-c|--checkout)
checkout=y
shift
;;
-s|--strategy)
if [ -z "$2" ]; then
usage "--strategy needs a value"
fi
strategy="$2"
merge_opts+=( -s "$2" )
shift 2
;;
-*)
usage "Unrecognised option: $1"
;;
*)
break
;;
esac
done
if [ $# = 1 ]; then
usage
fi
if [ $# = 0 ]; then
branches=( $( git mix "$combined_branch" ) )
if [ "${#branches[@]}" = 0 ]; then
err="No branches found for '$combined_branch' mix!"
err="$err\nMixes currently defined:\n`git mix`"
fatal "$err"
fi
base="${branches[0]}"
unset branches[0]
else
base="$1"
shift
branches=( "$@" )
fi
}
fatal () {
echo -e "$*" >&2
exit 1
}
safe_run () {
if ! "$@"; then
fatal "$* failed! Aborting."
fi
}
restore_head () {
if [ -n "$orig_head" ]; then
echo "Checking out original HEAD ($orig_head) ... "
git checkout "$orig_head"
else
echo "WARNING: Couldn't determine original HEAD; current HEAD is probably different".
fi
}
main () {
parse_opts "$@"
orig_head=`git rev-parse --abbrev-ref HEAD`
if ! git rev-parse --quiet --verify "${base}^{commit}" >/dev/null; then
fatal "$base is not a valid commit ref! Aborting."
fi
echo "Will start at $base and merge: ${branches[@]}"
safe_run git checkout -B "$combined_branch" "$base"
if [ "$strategy" = octopus ]; then
safe_run git merge --no-edit "${merge_opts[@]}" "${branches[@]}"
else
for branch in "${branches[@]}"; do
if ! git merge --no-edit "${merge_opts[@]}" "$branch"; then
echo "Fix conflicts, then exit shell to continue ..."
$SHELL
fi
done
fi
if [ -z "$checkout" ]; then
restore_head
fi
}
main "$@"