]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase--interactive.sh
t3404: test two "preserve merges with -p" cases
[thirdparty/git.git] / git-rebase--interactive.sh
CommitLineData
1b1dce4b
JS
1#!/bin/sh
2#
3# Copyright (c) 2006 Johannes E. Schindelin
4
5# SHORT DESCRIPTION
6#
7# This script makes it easy to fix up commits in the middle of a series,
8# and rearrange commits.
9#
10# The original idea comes from Eric W. Biederman, in
11# http://article.gmane.org/gmane.comp.version-control.git/22407
12
f09c9b8c
JS
13USAGE='(--continue | --abort | --skip | [--preserve-merges] [--verbose]
14 [--onto <branch>] <upstream> [<branch>])'
1b1dce4b 15
8f321a39 16OPTIONS_SPEC=
1b1dce4b
JS
17. git-sh-setup
18require_work_tree
19
20DOTEST="$GIT_DIR/.dotest-merge"
c22486c9 21TODO="$DOTEST"/git-rebase-todo
1b1dce4b 22DONE="$DOTEST"/done
6368f3f8
JS
23MSG="$DOTEST"/message
24SQUASH_MSG="$DOTEST"/message-squash
f09c9b8c
JS
25REWRITTEN="$DOTEST"/rewritten
26PRESERVE_MERGES=
1b1dce4b
JS
27STRATEGY=
28VERBOSE=
8e4a91bd
JS
29test -d "$REWRITTEN" && PRESERVE_MERGES=t
30test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
31test -f "$DOTEST"/verbose && VERBOSE=t
1b1dce4b 32
804c7174
WC
33GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
34mark the corrected paths with 'git add <paths>', and
35run 'git rebase --continue'"
36export GIT_CHERRY_PICK_HELP
37
1b1dce4b
JS
38warn () {
39 echo "$*" >&2
40}
41
dfa49f33
JS
42output () {
43 case "$VERBOSE" in
44 '')
5166810b 45 output=$("$@" 2>&1 )
dfa49f33 46 status=$?
5166810b 47 test $status != 0 && printf "%s\n" "$output"
dfa49f33 48 return $status
376ccb8c 49 ;;
dfa49f33
JS
50 *)
51 "$@"
376ccb8c 52 ;;
dfa49f33
JS
53 esac
54}
55
1b1dce4b
JS
56require_clean_work_tree () {
57 # test if working tree is dirty
58 git rev-parse --verify HEAD > /dev/null &&
6848d58c
JS
59 git update-index --ignore-submodules --refresh &&
60 git diff-files --quiet --ignore-submodules &&
61 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
1b1dce4b
JS
62 die "Working tree is dirty"
63}
64
65ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
66
67comment_for_reflog () {
68 case "$ORIG_REFLOG_ACTION" in
69 ''|rebase*)
70 GIT_REFLOG_ACTION="rebase -i ($1)"
71 export GIT_REFLOG_ACTION
376ccb8c 72 ;;
1b1dce4b
JS
73 esac
74}
75
4e673877 76last_count=
1b1dce4b
JS
77mark_action_done () {
78 sed -e 1q < "$TODO" >> "$DONE"
79 sed -e 1d < "$TODO" >> "$TODO".new
80 mv -f "$TODO".new "$TODO"
aadbe44f
JK
81 count=$(grep -c '^[^#]' < "$DONE")
82 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
4e673877
JH
83 if test "$last_count" != "$count"
84 then
85 last_count=$count
86 printf "Rebasing (%d/%d)\r" $count $total
87 test -z "$VERBOSE" || echo
88 fi
1b1dce4b
JS
89}
90
91make_patch () {
be6ff208
JS
92 parent_sha1=$(git rev-parse --verify "$1"^) ||
93 die "Cannot get patch for $1^"
f3d5e463 94 git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
be6ff208
JS
95 test -f "$DOTEST"/message ||
96 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
97 test -f "$DOTEST"/author-script ||
98 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
1b1dce4b
JS
99}
100
101die_with_patch () {
102 make_patch "$1"
ecfe72ff 103 git rerere
1b1dce4b
JS
104 die "$2"
105}
106
c54b7817
JS
107die_abort () {
108 rm -rf "$DOTEST"
109 die "$1"
110}
111
376ccb8c 112has_action () {
aadbe44f 113 grep '^[^#]' "$1" >/dev/null
376ccb8c
JS
114}
115
1b1dce4b 116pick_one () {
1d25c8cf
JS
117 no_ff=
118 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
dfa49f33 119 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
f09c9b8c
JS
120 test -d "$REWRITTEN" &&
121 pick_one_preserving_merges "$@" && return
376ccb8c
JS
122 parent_sha1=$(git rev-parse --verify $sha1^) ||
123 die "Could not get the parent of $sha1"
1b1dce4b 124 current_sha1=$(git rev-parse --verify HEAD)
2858028e 125 if test "$no_ff$current_sha1" = "$parent_sha1"; then
dfa49f33
JS
126 output git reset --hard $sha1
127 test "a$1" = a-n && output git reset --soft $current_sha1
1b1dce4b 128 sha1=$(git rev-parse --short $sha1)
dfa49f33 129 output warn Fast forward to $sha1
1b1dce4b 130 else
2a9c53e0 131 output git cherry-pick "$@"
1b1dce4b
JS
132 fi
133}
134
f09c9b8c
JS
135pick_one_preserving_merges () {
136 case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
137 sha1=$(git rev-parse $sha1)
138
3b38ec16 139 if test -f "$DOTEST"/current-commit
f09c9b8c
JS
140 then
141 current_commit=$(cat "$DOTEST"/current-commit) &&
142 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
143 rm "$DOTEST"/current-commit ||
144 die "Cannot write current commit's replacement sha1"
145 fi
146
147 # rewrite parents; if none were rewritten, we can fast-forward.
148 fast_forward=t
149 preserve=t
150 new_parents=
376ccb8c 151 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
f09c9b8c 152 do
3b38ec16 153 if test -f "$REWRITTEN"/$p
f09c9b8c
JS
154 then
155 preserve=f
156 new_p=$(cat "$REWRITTEN"/$p)
157 test $p != $new_p && fast_forward=f
158 case "$new_parents" in
159 *$new_p*)
160 ;; # do nothing; that parent is already there
161 *)
162 new_parents="$new_parents $new_p"
376ccb8c 163 ;;
f09c9b8c
JS
164 esac
165 fi
166 done
167 case $fast_forward in
168 t)
dfa49f33 169 output warn "Fast forward to $sha1"
376ccb8c 170 test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
f09c9b8c
JS
171 ;;
172 f)
173 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
174
376ccb8c 175 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
f09c9b8c 176 # detach HEAD to current parent
dfa49f33 177 output git checkout $first_parent 2> /dev/null ||
f09c9b8c
JS
178 die "Cannot move HEAD to $first_parent"
179
180 echo $sha1 > "$DOTEST"/current-commit
181 case "$new_parents" in
376ccb8c 182 ' '*' '*)
f09c9b8c
JS
183 # redo merge
184 author_script=$(get_author_ident_from_commit $sha1)
185 eval "$author_script"
376ccb8c 186 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
f91333d6
BS
187 # No point in merging the first parent, that's HEAD
188 new_parents=${new_parents# $first_parent}
ae830ed7
JS
189 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
190 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
191 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
192 output git merge $STRATEGY -m "$msg" \
193 $new_parents
f09c9b8c 194 then
ecfe72ff 195 git rerere
376ccb8c 196 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
18640d99 197 die Error redoing merge $sha1
f09c9b8c
JS
198 fi
199 ;;
200 *)
2a9c53e0 201 output git cherry-pick "$@" ||
f09c9b8c 202 die_with_patch $sha1 "Could not pick $sha1"
376ccb8c 203 ;;
f09c9b8c 204 esac
376ccb8c 205 ;;
f09c9b8c
JS
206 esac
207}
208
6368f3f8
JS
209nth_string () {
210 case "$1" in
211 *1[0-9]|*[04-9]) echo "$1"th;;
212 *1) echo "$1"st;;
213 *2) echo "$1"nd;;
214 *3) echo "$1"rd;;
215 esac
216}
217
218make_squash_message () {
3b38ec16 219 if test -f "$SQUASH_MSG"; then
c7965afd 220 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
b4ce54fc 221 < "$SQUASH_MSG" | sed -ne '$p')+1))
6368f3f8 222 echo "# This is a combination of $COUNT commits."
8ad1065e
JH
223 sed -e 1d -e '2,/^./{
224 /^$/d
225 }' <"$SQUASH_MSG"
6368f3f8
JS
226 else
227 COUNT=2
228 echo "# This is a combination of two commits."
229 echo "# The first commit's message is:"
230 echo
231 git cat-file commit HEAD | sed -e '1,/^$/d'
6368f3f8 232 fi
8ad1065e 233 echo
6368f3f8
JS
234 echo "# This is the $(nth_string $COUNT) commit message:"
235 echo
236 git cat-file commit $1 | sed -e '1,/^$/d'
237}
238
239peek_next_command () {
240 sed -n "1s/ .*$//p" < "$TODO"
241}
242
1b1dce4b 243do_next () {
376ccb8c
JS
244 rm -f "$DOTEST"/message "$DOTEST"/author-script \
245 "$DOTEST"/amend || exit
1b1dce4b
JS
246 read command sha1 rest < "$TODO"
247 case "$command" in
376ccb8c 248 '#'*|'')
1b1dce4b 249 mark_action_done
1b1dce4b 250 ;;
f8babc4d 251 pick|p)
1b1dce4b
JS
252 comment_for_reflog pick
253
254 mark_action_done
255 pick_one $sha1 ||
256 die_with_patch $sha1 "Could not apply $sha1... $rest"
257 ;;
f8babc4d 258 edit|e)
1b1dce4b
JS
259 comment_for_reflog edit
260
261 mark_action_done
262 pick_one $sha1 ||
263 die_with_patch $sha1 "Could not apply $sha1... $rest"
264 make_patch $sha1
be6ff208 265 : > "$DOTEST"/amend
1b1dce4b
JS
266 warn
267 warn "You can amend the commit now, with"
268 warn
269 warn " git commit --amend"
270 warn
0460fb44
JS
271 warn "Once you are satisfied with your changes, run"
272 warn
273 warn " git rebase --continue"
274 warn
1b1dce4b
JS
275 exit 0
276 ;;
f8babc4d 277 squash|s)
1b1dce4b
JS
278 comment_for_reflog squash
279
376ccb8c 280 has_action "$DONE" ||
1b1dce4b
JS
281 die "Cannot 'squash' without a previous commit"
282
283 mark_action_done
6368f3f8
JS
284 make_squash_message $sha1 > "$MSG"
285 case "$(peek_next_command)" in
f8babc4d 286 squash|s)
6368f3f8 287 EDIT_COMMIT=
91e1ee77 288 USE_OUTPUT=output
6368f3f8 289 cp "$MSG" "$SQUASH_MSG"
376ccb8c 290 ;;
6368f3f8
JS
291 *)
292 EDIT_COMMIT=-e
91e1ee77 293 USE_OUTPUT=
376ccb8c
JS
294 rm -f "$SQUASH_MSG" || exit
295 ;;
6368f3f8
JS
296 esac
297
793ad041 298 failed=f
81ab1cb4 299 author_script=$(get_author_ident_from_commit HEAD)
dfa49f33 300 output git reset --soft HEAD^
fb47cfbd 301 pick_one -n $sha1 || failed=t
18640d99 302 echo "$author_script" > "$DOTEST"/author-script
dbedf972
SP
303 if test $failed = f
304 then
1b1dce4b
JS
305 # This is like --amend, but with a different message
306 eval "$author_script"
ae830ed7
JS
307 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
308 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
309 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
dbedf972
SP
310 $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
311 fi
312 if test $failed = t
313 then
1b1dce4b
JS
314 cp "$MSG" "$GIT_DIR"/MERGE_MSG
315 warn
316 warn "Could not apply $sha1... $rest"
1b1dce4b 317 die_with_patch $sha1 ""
dbedf972 318 fi
1b1dce4b
JS
319 ;;
320 *)
321 warn "Unknown command: $command $sha1 $rest"
322 die_with_patch $sha1 "Please fix this in the file $TODO."
376ccb8c 323 ;;
1b1dce4b
JS
324 esac
325 test -s "$TODO" && return
326
68a163c9
JS
327 comment_for_reflog finish &&
328 HEADNAME=$(cat "$DOTEST"/head-name) &&
329 OLDHEAD=$(cat "$DOTEST"/head) &&
330 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
3b38ec16 331 if test -d "$REWRITTEN"
f09c9b8c
JS
332 then
333 test -f "$DOTEST"/current-commit &&
334 current_commit=$(cat "$DOTEST"/current-commit) &&
335 git rev-parse HEAD > "$REWRITTEN"/$current_commit
34454e85
JS
336 if test -f "$REWRITTEN"/$OLDHEAD
337 then
338 NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
339 else
340 NEWHEAD=$OLDHEAD
341 fi
f09c9b8c
JS
342 else
343 NEWHEAD=$(git rev-parse HEAD)
344 fi &&
73697a0b
JS
345 case $HEADNAME in
346 refs/*)
347 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
348 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
349 git symbolic-ref HEAD $HEADNAME
350 ;;
351 esac && {
3df0a859 352 test ! -f "$DOTEST"/verbose ||
f3d5e463 353 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
3df0a859 354 } &&
1b1dce4b 355 rm -rf "$DOTEST" &&
73697a0b 356 git gc --auto &&
1b1dce4b
JS
357 warn "Successfully rebased and updated $HEADNAME."
358
359 exit
360}
361
362do_rest () {
363 while :
364 do
365 do_next
366 done
1b1dce4b
JS
367}
368
822f7c73 369while test $# != 0
1b1dce4b
JS
370do
371 case "$1" in
372 --continue)
373 comment_for_reflog continue
374
375 test -d "$DOTEST" || die "No interactive rebase running"
376
ab119032
JH
377 # Sanity check
378 git rev-parse --verify HEAD >/dev/null ||
379 die "Cannot read HEAD"
6848d58c
JS
380 git update-index --ignore-submodules --refresh &&
381 git diff-files --quiet --ignore-submodules ||
ab119032
JH
382 die "Working tree is dirty"
383
384 # do we have anything to commit?
6848d58c 385 if git diff-index --cached --quiet --ignore-submodules HEAD --
03270628 386 then
ab119032
JH
387 : Nothing to commit -- skip this
388 else
389 . "$DOTEST"/author-script ||
390 die "Cannot find the author identity"
391 if test -f "$DOTEST"/amend
392 then
393 git reset --soft HEAD^ ||
394 die "Cannot rewind the HEAD"
395 fi
396 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
397 git commit --no-verify -F "$DOTEST"/message -e ||
dbedf972 398 die "Could not commit staged changes."
03270628 399 fi
18640d99 400
1b1dce4b
JS
401 require_clean_work_tree
402 do_rest
403 ;;
404 --abort)
405 comment_for_reflog abort
406
ecfe72ff 407 git rerere clear
1b1dce4b
JS
408 test -d "$DOTEST" || die "No interactive rebase running"
409
410 HEADNAME=$(cat "$DOTEST"/head-name)
411 HEAD=$(cat "$DOTEST"/head)
73697a0b
JS
412 case $HEADNAME in
413 refs/*)
414 git symbolic-ref HEAD $HEADNAME
415 ;;
416 esac &&
dfa49f33 417 output git reset --hard $HEAD &&
1b1dce4b
JS
418 rm -rf "$DOTEST"
419 exit
420 ;;
421 --skip)
422 comment_for_reflog skip
423
ecfe72ff 424 git rerere clear
1b1dce4b
JS
425 test -d "$DOTEST" || die "No interactive rebase running"
426
dfa49f33 427 output git reset --hard && do_rest
1b1dce4b
JS
428 ;;
429 -s|--strategy)
1b1dce4b
JS
430 case "$#,$1" in
431 *,*=*)
b5e960b1 432 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
1b1dce4b
JS
433 1,*)
434 usage ;;
435 *)
436 STRATEGY="-s $2"
437 shift ;;
438 esac
439 ;;
65ae89bc 440 -m|--merge)
1b1dce4b
JS
441 # we use merge anyway
442 ;;
443 -C*)
444 die "Interactive rebase uses merge, so $1 does not make sense"
445 ;;
c54b7817 446 -v|--verbose)
1b1dce4b
JS
447 VERBOSE=t
448 ;;
f09c9b8c
JS
449 -p|--preserve-merges)
450 PRESERVE_MERGES=t
451 ;;
1b1dce4b
JS
452 -i|--interactive)
453 # yeah, we know
454 ;;
455 ''|-h)
456 usage
457 ;;
458 *)
459 test -d "$DOTEST" &&
460 die "Interactive rebase already started"
461
462 git var GIT_COMMITTER_IDENT >/dev/null ||
463 die "You need to set your committer info first"
464
465 comment_for_reflog start
466
467 ONTO=
468 case "$1" in
469 --onto)
470 ONTO=$(git rev-parse --verify "$2") ||
471 die "Does not point to a valid commit: $2"
472 shift; shift
473 ;;
474 esac
475
476 require_clean_work_tree
477
69e66f55
JS
478 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
479 test -z "$ONTO" && ONTO=$UPSTREAM
480
3b38ec16 481 if test ! -z "$2"
1b1dce4b 482 then
dfa49f33 483 output git show-ref --verify --quiet "refs/heads/$2" ||
1b1dce4b 484 die "Invalid branchname: $2"
dfa49f33 485 output git checkout "$2" ||
1b1dce4b
JS
486 die "Could not checkout $2"
487 fi
488
489 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
5166810b
MK
490 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
491
1b1dce4b 492 : > "$DOTEST"/interactive || die "Could not mark as interactive"
73697a0b
JS
493 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
494 echo "detached HEAD" > "$DOTEST"/head-name
1b1dce4b
JS
495
496 echo $HEAD > "$DOTEST"/head
497 echo $UPSTREAM > "$DOTEST"/upstream
498 echo $ONTO > "$DOTEST"/onto
8e4a91bd 499 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1b1dce4b 500 test t = "$VERBOSE" && : > "$DOTEST"/verbose
3b38ec16 501 if test t = "$PRESERVE_MERGES"
f09c9b8c
JS
502 then
503 # $REWRITTEN contains files for each commit that is
504 # reachable by at least one merge base of $HEAD and
505 # $UPSTREAM. They are not necessarily rewritten, but
506 # their children might be.
507 # This ensures that commits on merged, but otherwise
508 # unrelated side branches are left alone. (Think "X"
509 # in the man page's example.)
510 mkdir "$REWRITTEN" &&
511 for c in $(git merge-base --all $HEAD $UPSTREAM)
512 do
513 echo $ONTO > "$REWRITTEN"/$c ||
514 die "Could not init rewritten commits"
515 done
516 MERGES_OPTION=
517 else
518 MERGES_OPTION=--no-merges
519 fi
1b1dce4b 520
c54b7817
JS
521 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
522 SHORTHEAD=$(git rev-parse --short $HEAD)
523 SHORTONTO=$(git rev-parse --short $ONTO)
6047a234
JS
524 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
525 --abbrev=7 --reverse --left-right --cherry-pick \
526 $UPSTREAM...$HEAD | \
527 sed -n "s/^>/pick /p" > "$TODO"
528 cat >> "$TODO" << EOF
529
530# Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
1b1dce4b
JS
531#
532# Commands:
88b1f0b8
MV
533# p, pick = use commit
534# e, edit = use commit, but stop for amending
535# s, squash = use commit, but meld into previous commit
82576ddb
JS
536#
537# If you remove a line here THAT COMMIT WILL BE LOST.
6047a234 538# However, if you remove everything, the rebase will be aborted.
82576ddb 539#
1b1dce4b 540EOF
1b1dce4b 541
376ccb8c 542 has_action "$TODO" ||
c54b7817 543 die_abort "Nothing to do"
1b1dce4b
JS
544
545 cp "$TODO" "$TODO".backup
ef0c2abf 546 git_editor "$TODO" ||
1b1dce4b
JS
547 die "Could not execute editor"
548
376ccb8c 549 has_action "$TODO" ||
c54b7817
JS
550 die_abort "Nothing to do"
551
dfa49f33 552 output git checkout $ONTO && do_rest
376ccb8c 553 ;;
1b1dce4b
JS
554 esac
555 shift
556done