shears.sh: introduce --recrate=<merge-commit> option
[msysgit.git] / share / msysGit / shears.sh
blob968534957e1ffec0cd8d96edbbaadea216d242e6
1 #!/bin/sh
3 # Rebase the thicket of branches -- including their merge structure -- on top
4 # of the specified upstream branch (defaults to 'junio/next'), optionally
5 # retaining "fast-forwardability" by fake-merging (using the "ours" strategy)
6 # the previous state on top of the current upstream state ("merging rebase").
8 # options:
9 # --merging
10 # start the rebased branch by a fake merge of the previous state
12 # The idea is to generate our very own rebase script, then call rebase -i with
13 # our fake editor to put the rebase script into place and then let the user edit
14 # the script.
16 # To make things prettier, we rewrite the rebase script after letting the user
17 # edit it, to replace "new" rebase commands with calls to a temporary alias ".r"
18 # that is added to help with starting the merging rebase, merging things, and
19 # cleaning up afterwards.
21 die () {
22 echo "$*" >&2
23 exit 1
26 git_dir="$(git rev-parse --git-dir)" ||
27 die "Not in a Git directory"
29 help () {
30 cat >&2 << EOF
31 Usage: $0 [options] <upstream>
33 Options:
34 -m|--merging[=<msg>] allow fast-forwarding the current to the rebased branch
35 --onto=<commit> rebase onto the given commit
36 --recreate=<merge> recreate the branch merged in the specified commit
37 EOF
38 exit 1
41 # Extra commands for use in the rebase script
42 extra_commands="edit bud finish mark rewind merge start_merging_rebase cleanup"
44 edit () {
45 GIT_EDITOR="$1" &&
46 GIT_SEQUENCE_EDITOR="$GIT_EDITOR" &&
47 export GIT_EDITOR GIT_SEQUENCE_EDITOR &&
48 shift &&
49 case "$*" in
50 */git-rebase-todo)
51 sed -e '/^noop/d' < "$1" >> "$git_dir"/SHEARS-SCRIPT &&
52 mv "$git_dir"/SHEARS-SCRIPT "$1"
53 "$GIT_EDITOR" "$@" &&
54 mv "$1" "$git_dir"/SHEARS-SCRIPT &&
55 exprs="$(for command in $extra_commands
57 printf " -e 's/^$command\$/exec git .r &/'"
58 printf " -e 's/^$command /exec git .r &/'"
59 done)" &&
60 eval sed $exprs < "$git_dir"/SHEARS-SCRIPT > "$1"
63 exec "$GIT_EDITOR" "$@"
64 esac
67 mark () {
68 git update-ref -m "Marking '$1' as rewritten" refs/rewritten/"$1" HEAD
71 rewind () {
72 git reset --hard refs/rewritten/"$1"
75 bud () {
76 shorthead="$(git rev-parse --short --verify HEAD)" &&
77 git for-each-ref refs/rewritten/ |
78 grep "^$shorthead" ||
79 die "Refusing to leave unmarked revision $shorthead behind"
80 git reset --hard refs/rewritten/onto
83 finish () {
84 mark "$@" &&
85 bud
88 merge () {
89 # parse command-line arguments
90 parents=
91 while test $# -gt 0 && test "a$1" != a-C
93 parents="$parents $1" &&
94 shift
95 done &&
96 if test "a$1" = "a-C"
97 then
98 shift &&
99 orig="$1" &&
100 shift &&
101 # determine whether the merge needs to be redone
102 p="$(git rev-parse HEAD)$parents" &&
103 o="$(git rev-list -1 --parents $orig |
104 sed "s/[^ ]*//")" &&
105 while p=${p# }; o=${o# }; test -n "$p$o"
107 p1=${p%% *}; o1=${o%% *};
108 test $o1 = "$(git rev-parse "$p1")" || break
109 p=${p#$p1}; o=${o#$o1}
110 done &&
111 # either redo merge or fast-forward
112 if test -z "$p$o"
113 then
114 git reset --hard $orig
115 return
116 fi &&
117 msg="$(git cat-file commit $orig |
118 sed "1,/^$/d")"
119 else
120 msg=
122 for parent in $parents
124 test -z "$msg" ||
125 msg="$msg and "
126 msg="$msg'$parent'"
127 p="$p $(git rev-parse --verify refs/rewritten/$parent \
128 2> /dev/null ||
129 echo $parent)"
130 done &&
131 msg="Merge $msg into HEAD"
132 fi &&
133 git merge -n --no-ff -m "$msg" $p
136 start_merging_rebase () {
137 git merge -s ours -m "$(cat "$git_dir"/SHEARS-MERGING-MESSAGE)" "$1"
140 cleanup () {
141 rm -f "$git_dir"/SHEARS-SCRIPT &&
142 for rewritten
144 git update-ref -d refs/rewritten/$rewritten
145 done &&
146 for rewritten in $(git for-each-ref refs/rewritten/ |
147 sed 's/^[^ ]* commit.refs\/rewritten\///')
149 test onto = "$rewritten" ||
150 merge $rewritten
151 git update-ref -d refs/rewritten/$rewritten
152 done &&
153 git config --unset alias..r
156 merging=
157 base_message=
158 onto=
159 recreate=
160 while test $# -gt 0
162 case "$1" in
163 -m|--merging)
164 merging=t
165 base_message=
167 --merging=*)
168 merging=t
169 base_message="${1#--merging=}"
171 --onto)
172 shift
173 onto="$1"
175 --onto=*)
176 onto="${1#--onto=}"
178 --recreate)
179 shift
180 recreate="$recreate $1"
182 --recreate=*)
183 recreate="$recreate ${1#--recreate=}"
185 -h|--help)
186 help
189 echo "Unknown option: $1" >&2
190 exit 1
193 break
195 esac
196 shift
197 done
199 case " $extra_commands " in
200 *" $1 "*)
201 command="$1"
202 shift
203 "$command" "$@"
204 exit
206 esac
208 string2regex () {
209 echo "$*" |
210 sed 's/[][\\\/*?]/\\&/g'
213 merge2branch_name () {
214 git show -s --format=%s "$1" |
215 sed -n -e "s/^Merge [^']*'\([^']*\).*/\1/p" \
216 -e "s/^Merge pull request #[0-9]* from //p" |
217 tr ' ' '-'
220 ensure_labeled () {
221 for n in "$@"
223 case " $needslabel " in
224 *" $n "*)
227 needslabel="$needslabel $n"
229 esac
230 done
233 generate_script () {
234 origtodo="$(git rev-list --no-merges --cherry-pick --pretty=oneline \
235 --abbrev-commit --abbrev=7 --reverse --left-right --topo-order \
236 $upstream..$head | \
237 sed -n "s/^>/pick /p")"
238 shorthead=$(git rev-parse --short $head)
239 shortonto=$(git rev-parse --short $onto)
241 # --topo-order has the bad habit of breaking first-parent chains over
242 # merges, so we generate the topoligical order ourselves here
244 list="$(git log --format='%h %p' --topo-order --reverse \
245 $upstream..$head)"
247 todo=
248 if test -n "$merging"
249 then
250 from=$(git rev-parse --short "$upstream") &&
251 to=$(git rev-parse --short "$onto") &&
252 cat > "$git_dir"/SHEARS-MERGING-MESSAGE << EOF &&
253 Start the merging-rebase to $onto
255 This commit starts the rebase of $from to $to
256 $base_message
258 todo="start_merging_rebase \"$shorthead\""
260 todo="$(printf '%s\n%s\n' "$todo" \
261 "mark onto")"
263 toberebased=" $(echo "$list" | cut -f 1 -d ' ' | tr '\n' ' ')"
264 handled=
265 needslabel=
267 # each tip is an end point of a commit->first parent chain
268 branch_tips="$(echo "$list" |
269 cut -f 3- -d ' ' |
270 tr ' ' '\n' |
271 grep -v '^$')"
273 ensure_labeled $branch_tips
275 branch_tips="$(printf '%s\n%s' "$branch_tips" "$shorthead")"
277 for tip in $branch_tips
279 # if this is not a commit to be rebased, skip
280 case "$toberebased" in *" $tip "*) ;; *) continue;; esac
282 # if it is handled already, skip
283 case "$handled " in *" $tip "*) continue;; esac
285 # start sub-todo for this tip
286 subtodo=
287 commit=$tip
288 while true
290 # if already handled, this is our branch point
291 case "$handled " in
292 *" $commit "*)
293 ensure_labeled $commit
294 subtodo="$(printf '\nrewind %s # %s\n%s' \
295 "$(name_commit $commit)" \
296 "$(git show -s --format=%s $commit)" \
297 "$subtodo")"
298 break
300 esac
302 line="$(echo "$list" | grep "^$commit ")"
303 # if there is no line, branch from the 'onto' commit
304 if test -z "$line"
305 then
306 subtodo="$(printf '\nbud\n%s' \
307 "$subtodo")"
308 break
310 parents=${line#* }
311 case "$parents" in
312 *' '*)
313 # merge
314 parents2="`for parent in ${parents#* }
316 case "$toberebased" in
317 *" $parent "*)
318 printf refs/rewritten/
320 esac
321 echo "$parent "
322 done`"
323 subtodo="$(printf '%s # %s\n%s' \
324 "merge $parents2-C $commit" \
325 "$(git show -s --format=%s $commit)" \
326 "$subtodo")"
329 # non-merge commit
330 line="$(echo "$origtodo" |
331 grep "^pick $commit")"
332 if test -z "$line"
333 then
334 line="# skip $commit"
336 subtodo="$(printf '%s\n%s' "$line" "$subtodo")"
338 esac
339 handled="$handled $commit"
340 commit=${parents%% *}
341 done
343 # try to figure out the branch name
344 merged_by="$(echo "$list" |
345 sed -n "s/^\([^ ]*\) [^ ]* $tip$/\1/p" |
346 head -n 1)"
347 if test -n "$merged_by"
348 then
349 branch_name="$(merge2branch_name "$merged_by")"
350 test -z "$branch_name" ||
351 subtodo="$(echo "$subtodo" |
352 sed -e "1a\\
353 # Branch: $branch_name")"
356 todo="$(printf '%s\n\n%s' "$todo" "$subtodo")"
357 done
359 for commit in $needslabel
361 linenumber="$(echo "$todo" |
362 grep -n -e "^\(pick\|# skip\) $commit" \
363 -e "^merge [0-9a-f/ ]* -C $commit")"
364 linenumber=${linenumber%%:*}
365 test -n "$linenumber" ||
366 die "Internal error: could not find $commit in $todo"
367 todo="$(echo "$todo" |
368 sed "${linenumber}a\\
369 mark $commit\\
371 done
373 lastline=9999
374 while true
376 fixup="$(echo "$todo" |
377 sed "$lastline,\$d" |
378 grep -n -e '^pick [^ ]* \(fixup\|squash\)!' |
379 tail -n 1)"
380 test -n "$fixup" || break
382 linenumber=${fixup%%:*}
383 oneline="${fixup#* }"
384 shortsha1="${oneline%% *}"
385 oneline="${oneline#* }"
386 command=${oneline%%!*}
387 oneline="${oneline#*! }"
388 oneline_regex="^pick [^ ]* $(string2regex "$oneline")\$"
389 targetline="$(echo "$todo" |
390 sed "$linenumber,\$d" |
391 grep -n "$oneline_regex" |
392 tail -n 1)"
393 targetline=${targetline%%:*}
394 if test -n "$targetline"
395 then
396 todo="$(echo "$todo" |
397 sed -e "${linenumber}d" \
398 -e "${targetline}a\\
399 $command $shortsha1 $oneline")"
400 lastline=$(($linenumber+1))
401 else
402 echo "UNHANDLED: $oneline" >&2
403 lastline=$(($linenumber))
405 done
407 while test -n "$recreate"
409 recreate="${recreate# }"
410 merge="${recreate%% *}"
411 recreate="${recreate#$merge}"
413 mark="$(git rev-parse --short --verify "$merge^2")" ||
414 die "Could not find merge commit: $merge^2"
416 branch_name="$(merge2branch_name "$merge")"
417 partfile="$git_dir/SHEARS-PART"
418 printf '%s' "$(test -z "$branch_name" ||
419 echo "# Branch to recreate: $branch_name")" \
420 > "$partfile"
421 for sha1 in $(git rev-list --reverse $merge^..$merge^2)
423 msg="$(git show -s --format=%s $sha1)"
424 msg_regex="^pick [^ ]* $(string2regex "$msg")\$"
425 linenumber="$(echo "$todo" |
426 grep -n "$msg_regex" |
427 sed 's/:.*//')"
428 test -n "$linenumber" ||
429 die "Not a commit to rebase: $msg"
430 test 1 = $(echo "$linenumber" | wc -l) ||
431 die "More than one match for: $msg"
432 echo "$todo" |
433 sed -n "${linenumber}p" >> "$partfile"
434 todo="$(echo "$todo" |
435 sed "${linenumber}d")"
436 done
438 linenumber="$(echo "$todo" |
439 grep -n "^bud\$" |
440 tail -n 1 |
441 sed 's/:.*//')"
443 printf 'mark %s\n\nbud\n' \
444 "$mark" >> "$partfile"
445 todo="$(echo "$todo" |
446 sed -e "${linenumber}r$partfile" \
447 -e "\$a\\
448 merge refs/rewritten/$mark -C $merge")"
449 done
451 todo="$(printf '%s\n\n%s' "$todo" "cleanup $needslabel")"
452 echo "$todo" | uniq
455 this="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")"
456 setup () {
457 test -z "$(git for-each-ref refs/rewritten/)" ||
458 die "There are still rewritten revisions"
460 alias="$(git config --get alias..r)"
461 test -z "$alias" ||
462 test "a$alias" = "a!sh \"$this\"" ||
463 die "There is already an '.r' alias!"
465 git config alias..r "!sh \"$this\"" &&
466 generate_script > "$git_dir"/SHEARS-SCRIPT &&
467 GIT_EDITOR="$(cd "$git_dir" && pwd)/SHEARS-EDITOR" &&
468 cat > "$GIT_EDITOR" << EOF &&
469 #!/bin/sh
471 exec "$this" edit "$(git var GIT_EDITOR)" "\$@"
473 chmod +x "$GIT_EDITOR" &&
474 GIT_EDITOR="\"$GIT_EDITOR\"" &&
475 GIT_SEQUENCE_EDITOR="$GIT_EDITOR" &&
476 export GIT_EDITOR GIT_SEQUENCE_EDITOR
479 test ! -d "$git_dir"/rebase-merge &&
480 test ! -d "$git_dir"/rebase-apply ||
481 die "Rebase already in progress"
483 test $# = 1 ||
484 help
486 head="$(git rev-parse HEAD)" &&
487 upstream="$1" &&
488 onto=${onto:-$upstream}||
489 die "Could not determine rebase parameters"
491 setup
493 # Rebase!
494 git rebase -i --onto "$onto" HEAD