]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase--interactive.sh
t/README: A new section about test coverage
[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
7970aaf0
SB
13OPTIONS_KEEPDASHDASH=
14OPTIONS_SPEC="\
15git-rebase [-i] [options] [--] <upstream> [<branch>]
16git-rebase [-i] (--continue | --abort | --skip)
17--
18 Available options are
19v,verbose display a diffstat of what changed upstream
20onto= rebase onto given branch instead of upstream
21p,preserve-merges try to recreate merges instead of ignoring them
22s,strategy= use the given merge strategy
b4995494 23no-ff cherry-pick all commits, even if unchanged
7970aaf0
SB
24m,merge always used (no-op)
25i,interactive always used (no-op)
26 Actions:
27continue continue rebasing process
28abort abort rebasing process and restore original branch
29skip skip current patch and continue rebasing process
c4427656 30no-verify override pre-rebase hook from stopping the operation
d911d146 31root rebase all reachable commmits up to the root(s)
f59baa50 32autosquash move commits that begin with squash!/fixup! under -i
7970aaf0 33"
1b1dce4b
JS
34
35. git-sh-setup
36require_work_tree
37
28ed6e7b 38DOTEST="$GIT_DIR/rebase-merge"
80883bb3
MH
39
40# The file containing rebase commands, comments, and empty lines.
41# This file is created by "git rebase -i" then edited by the user. As
42# the lines are processed, they are removed from the front of this
43# file and written to the tail of $DONE.
c22486c9 44TODO="$DOTEST"/git-rebase-todo
80883bb3
MH
45
46# The rebase command lines that have already been processed. A line
47# is moved here when it is first handled, before any associated user
48# actions.
1b1dce4b 49DONE="$DOTEST"/done
80883bb3
MH
50
51# The commit message that is planned to be used for any changes that
52# need to be committed following a user interaction.
6368f3f8 53MSG="$DOTEST"/message
80883bb3
MH
54
55# The file into which is accumulated the suggested commit message for
56# squash/fixup commands. When the first of a series of squash/fixups
57# is seen, the file is created and the commit message from the
58# previous commit and from the first squash/fixup commit are written
59# to it. The commit message for each subsequent squash/fixup commit
60# is appended to the file as it is processed.
61#
62# The first line of the file is of the form
63# # This is a combination of $COUNT commits.
64# where $COUNT is the number of commits whose messages have been
65# written to the file so far (including the initial "pick" commit).
66# Each time that a commit message is processed, this line is read and
67# updated. It is deleted just before the combined commit is made.
6368f3f8 68SQUASH_MSG="$DOTEST"/message-squash
80883bb3 69
a25eb139
MH
70# If the current series of squash/fixups has not yet included a squash
71# command, then this file exists and holds the commit message of the
72# original "pick" commit. (If the series ends without a "squash"
73# command, then this can be used as the commit message of the combined
74# commit without opening the editor.)
75FIXUP_MSG="$DOTEST"/message-fixup
76
80883bb3
MH
77# $REWRITTEN is the name of a directory containing files for each
78# commit that is reachable by at least one merge base of $HEAD and
79# $UPSTREAM. They are not necessarily rewritten, but their children
80# might be. This ensures that commits on merged, but otherwise
81# unrelated side branches are left alone. (Think "X" in the man page's
82# example.)
f09c9b8c 83REWRITTEN="$DOTEST"/rewritten
80883bb3 84
faae853c 85DROPPED="$DOTEST"/dropped
0aac0de4
MH
86
87# A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
88# GIT_AUTHOR_DATE that will be used for the commit that is currently
89# being rebased.
90AUTHOR_SCRIPT="$DOTEST"/author-script
91
a4049ae7
MH
92# When an "edit" rebase command is being processed, the SHA1 of the
93# commit to be edited is recorded in this file. When "git rebase
94# --continue" is executed, if there are any staged changes then they
95# will be amended to the HEAD commit, but only provided the HEAD
96# commit is still the commit to be edited. When any other rebase
97# command is processed, this file is deleted.
98AMEND="$DOTEST"/amend
99
b079feed
TR
100# For the post-rewrite hook, we make a list of rewritten commits and
101# their new sha1s. The rewritten-pending list keeps the sha1s of
102# commits that have been processed, but not committed yet,
103# e.g. because they are waiting for a 'squash' command.
104REWRITTEN_LIST="$DOTEST"/rewritten-list
105REWRITTEN_PENDING="$DOTEST"/rewritten-pending
106
f09c9b8c 107PRESERVE_MERGES=
1b1dce4b 108STRATEGY=
7970aaf0 109ONTO=
1b1dce4b 110VERBOSE=
c4427656 111OK_TO_SKIP_PRE_REBASE=
d911d146 112REBASE_ROOT=
f59baa50 113AUTOSQUASH=
b4995494 114NEVER_FF=
1b1dce4b 115
804c7174
WC
116GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
117mark the corrected paths with 'git add <paths>', and
118run 'git rebase --continue'"
119export GIT_CHERRY_PICK_HELP
120
1b1dce4b 121warn () {
938791cd 122 printf '%s\n' "$*" >&2
1b1dce4b
JS
123}
124
dfa49f33
JS
125output () {
126 case "$VERBOSE" in
127 '')
5166810b 128 output=$("$@" 2>&1 )
dfa49f33 129 status=$?
5166810b 130 test $status != 0 && printf "%s\n" "$output"
dfa49f33 131 return $status
376ccb8c 132 ;;
dfa49f33
JS
133 *)
134 "$@"
376ccb8c 135 ;;
dfa49f33
JS
136 esac
137}
138
ee0a4afb
MH
139# Output the commit message for the specified commit.
140commit_message () {
141 git cat-file commit "$1" | sed "1,/^$/d"
142}
143
d70b4a8f 144run_pre_rebase_hook () {
c4427656
NS
145 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
146 test -x "$GIT_DIR/hooks/pre-rebase"
d70b4a8f
NS
147 then
148 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
149 echo >&2 "The pre-rebase hook refused to rebase."
150 exit 1
151 }
152 fi
153}
154
1b1dce4b
JS
155require_clean_work_tree () {
156 # test if working tree is dirty
157 git rev-parse --verify HEAD > /dev/null &&
6848d58c
JS
158 git update-index --ignore-submodules --refresh &&
159 git diff-files --quiet --ignore-submodules &&
160 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
1b1dce4b
JS
161 die "Working tree is dirty"
162}
163
164ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
165
166comment_for_reflog () {
167 case "$ORIG_REFLOG_ACTION" in
168 ''|rebase*)
169 GIT_REFLOG_ACTION="rebase -i ($1)"
170 export GIT_REFLOG_ACTION
376ccb8c 171 ;;
1b1dce4b
JS
172 esac
173}
174
4e673877 175last_count=
1b1dce4b
JS
176mark_action_done () {
177 sed -e 1q < "$TODO" >> "$DONE"
178 sed -e 1d < "$TODO" >> "$TODO".new
179 mv -f "$TODO".new "$TODO"
e1622bfc
JH
180 count=$(sane_grep -c '^[^#]' < "$DONE")
181 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
4e673877
JH
182 if test "$last_count" != "$count"
183 then
184 last_count=$count
185 printf "Rebasing (%d/%d)\r" $count $total
186 test -z "$VERBOSE" || echo
187 fi
1b1dce4b
JS
188}
189
190make_patch () {
4fb1a19d
JS
191 sha1_and_parents="$(git rev-list --parents -1 "$1")"
192 case "$sha1_and_parents" in
193 ?*' '?*' '?*)
194 git diff --cc $sha1_and_parents
195 ;;
196 ?*' '?*)
197 git diff-tree -p "$1^!"
198 ;;
199 *)
200 echo "Root commit"
201 ;;
202 esac > "$DOTEST"/patch
bdb011ad 203 test -f "$MSG" ||
ee0a4afb 204 commit_message "$1" > "$MSG"
0aac0de4
MH
205 test -f "$AUTHOR_SCRIPT" ||
206 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
1b1dce4b
JS
207}
208
209die_with_patch () {
b079feed 210 echo "$1" > "$DOTEST"/stopped-sha
1b1dce4b 211 make_patch "$1"
ecfe72ff 212 git rerere
1b1dce4b
JS
213 die "$2"
214}
215
c54b7817
JS
216die_abort () {
217 rm -rf "$DOTEST"
218 die "$1"
219}
220
376ccb8c 221has_action () {
e1622bfc 222 sane_grep '^[^#]' "$1" >/dev/null
376ccb8c
JS
223}
224
7756ecff
MH
225# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
226# GIT_AUTHOR_DATE exported from the current environment.
227do_with_author () {
76c9c0db
JH
228 (
229 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
230 "$@"
231 )
7756ecff
MH
232}
233
1b1dce4b 234pick_one () {
8e75abfd
CC
235 ff=--ff
236 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
9234b003 237 case "$NEVER_FF" in '') ;; ?*) ff= ;; esac
dfa49f33 238 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
f09c9b8c
JS
239 test -d "$REWRITTEN" &&
240 pick_one_preserving_merges "$@" && return
699f13ca 241 if test -n "$REBASE_ROOT"
d911d146
TR
242 then
243 output git cherry-pick "$@"
244 return
245 fi
8e75abfd 246 output git cherry-pick $ff "$@"
1b1dce4b
JS
247}
248
f09c9b8c 249pick_one_preserving_merges () {
71d9451e
TR
250 fast_forward=t
251 case "$1" in
252 -n)
253 fast_forward=f
254 sha1=$2
255 ;;
256 *)
257 sha1=$1
258 ;;
259 esac
f09c9b8c
JS
260 sha1=$(git rev-parse $sha1)
261
3b38ec16 262 if test -f "$DOTEST"/current-commit
f09c9b8c 263 then
4c1360f4 264 if test "$fast_forward" = t
bb645071 265 then
41f556b9 266 while read current_commit
bb645071
SH
267 do
268 git rev-parse HEAD > "$REWRITTEN"/$current_commit
41f556b9 269 done <"$DOTEST"/current-commit
bb645071
SH
270 rm "$DOTEST"/current-commit ||
271 die "Cannot write current commit's replacement sha1"
272 fi
f09c9b8c
JS
273 fi
274
bb645071 275 echo $sha1 >> "$DOTEST"/current-commit
a96dc01e 276
f09c9b8c 277 # rewrite parents; if none were rewritten, we can fast-forward.
f09c9b8c 278 new_parents=
d911d146
TR
279 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
280 if test "$pend" = " "
281 then
282 pend=" root"
283 fi
faae853c 284 while [ "$pend" != "" ]
f09c9b8c 285 do
faae853c
SH
286 p=$(expr "$pend" : ' \([^ ]*\)')
287 pend="${pend# $p}"
288
3b38ec16 289 if test -f "$REWRITTEN"/$p
f09c9b8c 290 then
f09c9b8c 291 new_p=$(cat "$REWRITTEN"/$p)
80fe82e4
SH
292
293 # If the todo reordered commits, and our parent is marked for
294 # rewriting, but hasn't been gotten to yet, assume the user meant to
295 # drop it on top of the current HEAD
296 if test -z "$new_p"
297 then
298 new_p=$(git rev-parse HEAD)
299 fi
300
f09c9b8c
JS
301 test $p != $new_p && fast_forward=f
302 case "$new_parents" in
303 *$new_p*)
304 ;; # do nothing; that parent is already there
305 *)
306 new_parents="$new_parents $new_p"
376ccb8c 307 ;;
f09c9b8c 308 esac
1c5fa0a1 309 else
faae853c
SH
310 if test -f "$DROPPED"/$p
311 then
312 fast_forward=f
d911d146
TR
313 replacement="$(cat "$DROPPED"/$p)"
314 test -z "$replacement" && replacement=root
315 pend=" $replacement$pend"
faae853c
SH
316 else
317 new_parents="$new_parents $p"
318 fi
f09c9b8c
JS
319 fi
320 done
321 case $fast_forward in
322 t)
a75d7b54 323 output warn "Fast-forward to $sha1"
71d9451e 324 output git reset --hard $sha1 ||
a75d7b54 325 die "Cannot fast-forward to $sha1"
f09c9b8c
JS
326 ;;
327 f)
376ccb8c 328 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
a4f25e36
SH
329
330 if [ "$1" != "-n" ]
331 then
332 # detach HEAD to current parent
333 output git checkout $first_parent 2> /dev/null ||
334 die "Cannot move HEAD to $first_parent"
335 fi
f09c9b8c 336
f09c9b8c 337 case "$new_parents" in
376ccb8c 338 ' '*' '*)
a4f25e36
SH
339 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
340
f09c9b8c
JS
341 # redo merge
342 author_script=$(get_author_ident_from_commit $sha1)
343 eval "$author_script"
ee0a4afb 344 msg="$(commit_message $sha1)"
f91333d6
BS
345 # No point in merging the first parent, that's HEAD
346 new_parents=${new_parents# $first_parent}
7756ecff
MH
347 if ! do_with_author output \
348 git merge $STRATEGY -m "$msg" $new_parents
f09c9b8c 349 then
376ccb8c 350 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
f5b49ea6 351 die_with_patch $sha1 "Error redoing merge $sha1"
f09c9b8c 352 fi
b079feed 353 echo "$sha1 $(git rev-parse HEAD^0)" >> "$REWRITTEN_LIST"
f09c9b8c
JS
354 ;;
355 *)
2a9c53e0 356 output git cherry-pick "$@" ||
f09c9b8c 357 die_with_patch $sha1 "Could not pick $sha1"
376ccb8c 358 ;;
f09c9b8c 359 esac
376ccb8c 360 ;;
f09c9b8c
JS
361 esac
362}
363
6368f3f8
JS
364nth_string () {
365 case "$1" in
366 *1[0-9]|*[04-9]) echo "$1"th;;
367 *1) echo "$1"st;;
368 *2) echo "$1"nd;;
369 *3) echo "$1"rd;;
370 esac
371}
372
a25eb139 373update_squash_messages () {
3b38ec16 374 if test -f "$SQUASH_MSG"; then
bde1a686 375 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
f99e269c
MH
376 COUNT=$(($(sed -n \
377 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
bde1a686
MH
378 -e "q" < "$SQUASH_MSG".bak)+1))
379 {
380 echo "# This is a combination of $COUNT commits."
381 sed -e 1d -e '2,/^./{
382 /^$/d
383 }' <"$SQUASH_MSG".bak
3fa7c3da 384 } >"$SQUASH_MSG"
6368f3f8 385 else
a25eb139 386 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
6368f3f8 387 COUNT=2
bde1a686
MH
388 {
389 echo "# This is a combination of 2 commits."
390 echo "# The first commit's message is:"
391 echo
a25eb139 392 cat "$FIXUP_MSG"
3fa7c3da 393 } >"$SQUASH_MSG"
6368f3f8 394 fi
0205e72f
MH
395 case $1 in
396 squash)
a25eb139 397 rm -f "$FIXUP_MSG"
0205e72f
MH
398 echo
399 echo "# This is the $(nth_string $COUNT) commit message:"
400 echo
ee0a4afb 401 commit_message $2
0205e72f
MH
402 ;;
403 fixup)
404 echo
405 echo "# The $(nth_string $COUNT) commit message will be skipped:"
406 echo
ee0a4afb 407 commit_message $2 | sed -e 's/^/# /'
0205e72f 408 ;;
3fa7c3da 409 esac >>"$SQUASH_MSG"
6368f3f8
JS
410}
411
412peek_next_command () {
9524cf29 413 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
6368f3f8
JS
414}
415
6bdcd0d2
MH
416# A squash/fixup has failed. Prepare the long version of the squash
417# commit message, then die_with_patch. This code path requires the
418# user to edit the combined commit message for all commits that have
419# been squashed/fixedup so far. So also erase the old squash
420# messages, effectively causing the combined commit to be used as the
421# new basis for any further squash/fixups. Args: sha1 rest
422die_failed_squash() {
423 mv "$SQUASH_MSG" "$MSG" || exit
424 rm -f "$FIXUP_MSG"
425 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
426 warn
427 warn "Could not apply $1... $2"
428 die_with_patch $1 ""
429}
430
b079feed
TR
431flush_rewritten_pending() {
432 test -s "$REWRITTEN_PENDING" || return
433 newsha1="$(git rev-parse HEAD^0)"
434 sed "s/$/ $newsha1/" < "$REWRITTEN_PENDING" >> "$REWRITTEN_LIST"
435 rm -f "$REWRITTEN_PENDING"
436}
437
438record_in_rewritten() {
439 oldsha1="$(git rev-parse $1)"
440 echo "$oldsha1" >> "$REWRITTEN_PENDING"
441
442 case "$(peek_next_command)" in
41f556b9 443 squash|s|fixup|f)
b079feed 444 ;;
41f556b9 445 *)
b079feed
TR
446 flush_rewritten_pending
447 ;;
448 esac
449}
450
1b1dce4b 451do_next () {
a4049ae7 452 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
57f2b6b2 453 read -r command sha1 rest < "$TODO"
1b1dce4b 454 case "$command" in
ff74126c 455 '#'*|''|noop)
1b1dce4b 456 mark_action_done
1b1dce4b 457 ;;
f8babc4d 458 pick|p)
1b1dce4b
JS
459 comment_for_reflog pick
460
461 mark_action_done
462 pick_one $sha1 ||
463 die_with_patch $sha1 "Could not apply $sha1... $rest"
b079feed 464 record_in_rewritten $sha1
1b1dce4b 465 ;;
6741aa6c
BG
466 reword|r)
467 comment_for_reflog reword
468
469 mark_action_done
470 pick_one $sha1 ||
471 die_with_patch $sha1 "Could not apply $sha1... $rest"
6f6bee3b 472 git commit --amend --no-post-rewrite
b079feed 473 record_in_rewritten $sha1
6741aa6c 474 ;;
f8babc4d 475 edit|e)
1b1dce4b
JS
476 comment_for_reflog edit
477
478 mark_action_done
479 pick_one $sha1 ||
480 die_with_patch $sha1 "Could not apply $sha1... $rest"
0acb62f2 481 echo "$sha1" > "$DOTEST"/stopped-sha
1b1dce4b 482 make_patch $sha1
a4049ae7 483 git rev-parse --verify HEAD > "$AMEND"
a8ccc204 484 warn "Stopped at $sha1... $rest"
1b1dce4b
JS
485 warn "You can amend the commit now, with"
486 warn
487 warn " git commit --amend"
488 warn
0460fb44
JS
489 warn "Once you are satisfied with your changes, run"
490 warn
491 warn " git rebase --continue"
492 warn
1b1dce4b
JS
493 exit 0
494 ;;
0205e72f
MH
495 squash|s|fixup|f)
496 case "$command" in
497 squash|s)
498 squash_style=squash
499 ;;
500 fixup|f)
501 squash_style=fixup
502 ;;
503 esac
504 comment_for_reflog $squash_style
1b1dce4b 505
12dd1112 506 test -f "$DONE" && has_action "$DONE" ||
0205e72f 507 die "Cannot '$squash_style' without a previous commit"
1b1dce4b
JS
508
509 mark_action_done
a25eb139 510 update_squash_messages $squash_style $sha1
7c418836 511 author_script=$(get_author_ident_from_commit HEAD)
5c5d059a
MH
512 echo "$author_script" > "$AUTHOR_SCRIPT"
513 eval "$author_script"
7c418836 514 output git reset --soft HEAD^
6bdcd0d2 515 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
6368f3f8 516 case "$(peek_next_command)" in
0205e72f 517 squash|s|fixup|f)
a25eb139
MH
518 # This is an intermediate commit; its message will only be
519 # used in case of trouble. So use the long version:
6bdcd0d2
MH
520 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
521 die_failed_squash $sha1 "$rest"
376ccb8c 522 ;;
6368f3f8 523 *)
a25eb139 524 # This is the final command of this squash/fixup group
6bdcd0d2 525 if test -f "$FIXUP_MSG"
a25eb139 526 then
6bdcd0d2
MH
527 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
528 die_failed_squash $sha1 "$rest"
529 else
530 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
531 rm -f "$GIT_DIR"/MERGE_MSG
532 do_with_author git commit --no-verify -e ||
533 die_failed_squash $sha1 "$rest"
a25eb139 534 fi
6bdcd0d2 535 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
376ccb8c 536 ;;
6368f3f8 537 esac
b079feed 538 record_in_rewritten $sha1
1b1dce4b
JS
539 ;;
540 *)
541 warn "Unknown command: $command $sha1 $rest"
f1be316a
JK
542 if git rev-parse --verify -q "$sha1" >/dev/null
543 then
544 die_with_patch $sha1 "Please fix this in the file $TODO."
545 else
546 die "Please fix this in the file $TODO."
547 fi
376ccb8c 548 ;;
1b1dce4b
JS
549 esac
550 test -s "$TODO" && return
551
68a163c9
JS
552 comment_for_reflog finish &&
553 HEADNAME=$(cat "$DOTEST"/head-name) &&
554 OLDHEAD=$(cat "$DOTEST"/head) &&
555 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
72583e6c 556 NEWHEAD=$(git rev-parse HEAD) &&
73697a0b
JS
557 case $HEADNAME in
558 refs/*)
a0c0447b 559 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
73697a0b
JS
560 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
561 git symbolic-ref HEAD $HEADNAME
562 ;;
563 esac && {
3df0a859 564 test ! -f "$DOTEST"/verbose ||
f3d5e463 565 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
3df0a859 566 } &&
eb2151bb 567 {
77bc6949 568 test -s "$REWRITTEN_LIST" &&
eb2151bb
TR
569 git notes copy --for-rewrite=rebase < "$REWRITTEN_LIST" ||
570 true # we don't care if this copying failed
571 } &&
b079feed
TR
572 if test -x "$GIT_DIR"/hooks/post-rewrite &&
573 test -s "$REWRITTEN_LIST"; then
574 "$GIT_DIR"/hooks/post-rewrite rebase < "$REWRITTEN_LIST"
575 true # we don't care if this hook failed
576 fi &&
1b1dce4b 577 rm -rf "$DOTEST" &&
73697a0b 578 git gc --auto &&
1b1dce4b
JS
579 warn "Successfully rebased and updated $HEADNAME."
580
581 exit
582}
583
584do_rest () {
585 while :
586 do
587 do_next
588 done
1b1dce4b
JS
589}
590
0e757e30
JS
591# skip picking commits whose parents are unchanged
592skip_unnecessary_picks () {
593 fd=3
57f2b6b2 594 while read -r command sha1 rest
0e757e30
JS
595 do
596 # fd=3 means we skip the command
597 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
598 3,pick,"$ONTO"*|3,p,"$ONTO"*)
599 # pick a commit whose parent is current $ONTO -> skip
600 ONTO=$sha1
601 ;;
602 3,#*|3,,*)
603 # copy comments
604 ;;
605 *)
606 fd=1
607 ;;
608 esac
938791cd 609 printf '%s\n' "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
0e757e30 610 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
b079feed
TR
611 mv -f "$TODO".new "$TODO" &&
612 case "$(peek_next_command)" in
613 squash|s|fixup|f)
614 record_in_rewritten "$ONTO"
615 ;;
616 esac ||
0e757e30
JS
617 die "Could not skip unnecessary pick commands"
618}
619
7970aaf0
SB
620# check if no other options are set
621is_standalone () {
622 test $# -eq 2 -a "$2" = '--' &&
623 test -z "$ONTO" &&
624 test -z "$PRESERVE_MERGES" &&
625 test -z "$STRATEGY" &&
626 test -z "$VERBOSE"
627}
628
629get_saved_options () {
630 test -d "$REWRITTEN" && PRESERVE_MERGES=t
631 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
632 test -f "$DOTEST"/verbose && VERBOSE=t
a6c7a276 633 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
7970aaf0
SB
634}
635
f59baa50
NS
636# Rearrange the todo list that has both "pick sha1 msg" and
637# "pick sha1 fixup!/squash! msg" appears in it so that the latter
638# comes immediately after the former, and change "pick" to
639# "fixup"/"squash".
640rearrange_squash () {
641 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
642 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
643 "$1" >"$1.sq"
644 test -s "$1.sq" || return
645
646 used=
57f2b6b2 647 while read -r pick sha1 message
f59baa50
NS
648 do
649 case " $used" in
650 *" $sha1 "*) continue ;;
651 esac
938791cd 652 printf '%s\n' "$pick $sha1 $message"
57f2b6b2 653 while read -r squash action msg
f59baa50
NS
654 do
655 case "$message" in
656 "$msg"*)
938791cd 657 printf '%s\n' "$action $squash $action! $msg"
f59baa50
NS
658 used="$used$squash "
659 ;;
660 esac
661 done <"$1.sq"
662 done >"$1.rearranged" <"$1"
663 cat "$1.rearranged" >"$1"
664 rm -f "$1.sq" "$1.rearranged"
665}
666
230a4566
NS
667LF='
668'
669parse_onto () {
670 case "$1" in
671 *...*)
672 if left=${1%...*} right=${1#*...} &&
673 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
674 then
675 case "$onto" in
676 ?*"$LF"?* | '')
677 exit 1 ;;
678 esac
679 echo "$onto"
680 exit 0
681 fi
682 esac
683 git rev-parse --verify "$1^0"
684}
685
822f7c73 686while test $# != 0
1b1dce4b
JS
687do
688 case "$1" in
c4427656
NS
689 --no-verify)
690 OK_TO_SKIP_PRE_REBASE=yes
691 ;;
692 --verify)
693 ;;
1b1dce4b 694 --continue)
7970aaf0
SB
695 is_standalone "$@" || usage
696 get_saved_options
1b1dce4b
JS
697 comment_for_reflog continue
698
699 test -d "$DOTEST" || die "No interactive rebase running"
700
ab119032
JH
701 # Sanity check
702 git rev-parse --verify HEAD >/dev/null ||
703 die "Cannot read HEAD"
6848d58c
JS
704 git update-index --ignore-submodules --refresh &&
705 git diff-files --quiet --ignore-submodules ||
ab119032
JH
706 die "Working tree is dirty"
707
708 # do we have anything to commit?
6848d58c 709 if git diff-index --cached --quiet --ignore-submodules HEAD --
03270628 710 then
ab119032
JH
711 : Nothing to commit -- skip this
712 else
0aac0de4 713 . "$AUTHOR_SCRIPT" ||
ab119032 714 die "Cannot find the author identity"
8beb1f33 715 amend=
a4049ae7 716 if test -f "$AMEND"
ab119032 717 then
8beb1f33 718 amend=$(git rev-parse --verify HEAD)
a4049ae7 719 test "$amend" = $(cat "$AMEND") ||
c14c3c82
DP
720 die "\
721You have uncommitted changes in your working tree. Please, commit them
722first and then run 'git rebase --continue' again."
ab119032
JH
723 git reset --soft HEAD^ ||
724 die "Cannot rewind the HEAD"
725 fi
7756ecff 726 do_with_author git commit --no-verify -F "$MSG" -e || {
8beb1f33
DP
727 test -n "$amend" && git reset --soft $amend
728 die "Could not commit staged changes."
729 }
03270628 730 fi
18640d99 731
0acb62f2
TR
732 record_in_rewritten "$(cat "$DOTEST"/stopped-sha)"
733
1b1dce4b
JS
734 require_clean_work_tree
735 do_rest
736 ;;
737 --abort)
7970aaf0
SB
738 is_standalone "$@" || usage
739 get_saved_options
1b1dce4b
JS
740 comment_for_reflog abort
741
ecfe72ff 742 git rerere clear
1b1dce4b
JS
743 test -d "$DOTEST" || die "No interactive rebase running"
744
745 HEADNAME=$(cat "$DOTEST"/head-name)
746 HEAD=$(cat "$DOTEST"/head)
73697a0b
JS
747 case $HEADNAME in
748 refs/*)
749 git symbolic-ref HEAD $HEADNAME
750 ;;
751 esac &&
dfa49f33 752 output git reset --hard $HEAD &&
1b1dce4b
JS
753 rm -rf "$DOTEST"
754 exit
755 ;;
756 --skip)
7970aaf0
SB
757 is_standalone "$@" || usage
758 get_saved_options
1b1dce4b
JS
759 comment_for_reflog skip
760
ecfe72ff 761 git rerere clear
1b1dce4b
JS
762 test -d "$DOTEST" || die "No interactive rebase running"
763
dfa49f33 764 output git reset --hard && do_rest
1b1dce4b 765 ;;
7970aaf0 766 -s)
1b1dce4b
JS
767 case "$#,$1" in
768 *,*=*)
b5e960b1 769 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
1b1dce4b
JS
770 1,*)
771 usage ;;
772 *)
773 STRATEGY="-s $2"
774 shift ;;
775 esac
776 ;;
7970aaf0 777 -m)
1b1dce4b
JS
778 # we use merge anyway
779 ;;
7970aaf0 780 -v)
1b1dce4b
JS
781 VERBOSE=t
782 ;;
7970aaf0 783 -p)
f09c9b8c
JS
784 PRESERVE_MERGES=t
785 ;;
7970aaf0 786 -i)
1b1dce4b
JS
787 # yeah, we know
788 ;;
b4995494
MB
789 --no-ff)
790 NEVER_FF=t
791 ;;
d911d146
TR
792 --root)
793 REBASE_ROOT=t
794 ;;
f59baa50
NS
795 --autosquash)
796 AUTOSQUASH=t
797 ;;
7970aaf0
SB
798 --onto)
799 shift
230a4566 800 ONTO=$(parse_onto "$1") ||
7970aaf0 801 die "Does not point to a valid commit: $1"
1b1dce4b 802 ;;
7970aaf0
SB
803 --)
804 shift
277d7e91
JS
805 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
806 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
1b1dce4b
JS
807 test -d "$DOTEST" &&
808 die "Interactive rebase already started"
809
810 git var GIT_COMMITTER_IDENT >/dev/null ||
811 die "You need to set your committer info first"
812
d911d146
TR
813 if test -z "$REBASE_ROOT"
814 then
815 UPSTREAM_ARG="$1"
816 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
817 test -z "$ONTO" && ONTO=$UPSTREAM
818 shift
819 else
a6c7a276 820 UPSTREAM=
d911d146
TR
821 UPSTREAM_ARG=--root
822 test -z "$ONTO" &&
823 die "You must specify --onto when using --root"
824 fi
825 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
d8fab023 826
1b1dce4b
JS
827 comment_for_reflog start
828
1b1dce4b
JS
829 require_clean_work_tree
830
d911d146 831 if test ! -z "$1"
1b1dce4b 832 then
d911d146
TR
833 output git checkout "$1" ||
834 die "Could not checkout $1"
1b1dce4b
JS
835 fi
836
837 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
5166810b
MK
838 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
839
1b1dce4b 840 : > "$DOTEST"/interactive || die "Could not mark as interactive"
73697a0b
JS
841 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
842 echo "detached HEAD" > "$DOTEST"/head-name
1b1dce4b
JS
843
844 echo $HEAD > "$DOTEST"/head
a6c7a276
JH
845 case "$REBASE_ROOT" in
846 '')
847 rm -f "$DOTEST"/rebase-root ;;
848 *)
849 : >"$DOTEST"/rebase-root ;;
850 esac
1b1dce4b 851 echo $ONTO > "$DOTEST"/onto
8e4a91bd 852 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1b1dce4b 853 test t = "$VERBOSE" && : > "$DOTEST"/verbose
3b38ec16 854 if test t = "$PRESERVE_MERGES"
f09c9b8c 855 then
d911d146
TR
856 if test -z "$REBASE_ROOT"
857 then
858 mkdir "$REWRITTEN" &&
859 for c in $(git merge-base --all $HEAD $UPSTREAM)
860 do
861 echo $ONTO > "$REWRITTEN"/$c ||
862 die "Could not init rewritten commits"
863 done
864 else
865 mkdir "$REWRITTEN" &&
866 echo $ONTO > "$REWRITTEN"/root ||
f09c9b8c 867 die "Could not init rewritten commits"
d911d146 868 fi
acc8559a
SH
869 # No cherry-pick because our first pass is to determine
870 # parents to rewrite and skipping dropped commits would
871 # prematurely end our probe
f09c9b8c 872 MERGES_OPTION=
d80d6bc1 873 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
f09c9b8c 874 else
acc8559a 875 MERGES_OPTION="--no-merges --cherry-pick"
f09c9b8c 876 fi
1b1dce4b 877
c54b7817
JS
878 SHORTHEAD=$(git rev-parse --short $HEAD)
879 SHORTONTO=$(git rev-parse --short $ONTO)
d911d146
TR
880 if test -z "$REBASE_ROOT"
881 # this is now equivalent to ! -z "$UPSTREAM"
882 then
883 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
884 REVISIONS=$UPSTREAM...$HEAD
885 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
886 else
887 REVISIONS=$ONTO...$HEAD
888 SHORTREVISIONS=$SHORTHEAD
889 fi
6047a234 890 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
acc8559a 891 --abbrev=7 --reverse --left-right --topo-order \
d911d146 892 $REVISIONS | \
41f556b9 893 sed -n "s/^>//p" |
57f2b6b2 894 while read -r shortsha1 rest
acc8559a
SH
895 do
896 if test t != "$PRESERVE_MERGES"
897 then
938791cd 898 printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
acc8559a
SH
899 else
900 sha1=$(git rev-parse $shortsha1)
d911d146
TR
901 if test -z "$REBASE_ROOT"
902 then
903 preserve=t
904 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
905 do
1830d9cb 906 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
d911d146
TR
907 then
908 preserve=f
909 fi
910 done
911 else
912 preserve=f
913 fi
acc8559a
SH
914 if test f = "$preserve"
915 then
916 touch "$REWRITTEN"/$sha1
938791cd 917 printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
acc8559a
SH
918 fi
919 fi
920 done
921
922 # Watch for commits that been dropped by --cherry-pick
923 if test t = "$PRESERVE_MERGES"
924 then
925 mkdir "$DROPPED"
926 # Save all non-cherry-picked changes
d911d146 927 git rev-list $REVISIONS --left-right --cherry-pick | \
acc8559a
SH
928 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
929 # Now all commits and note which ones are missing in
930 # not-cherry-picks and hence being dropped
d911d146 931 git rev-list $REVISIONS |
e249044c 932 while read rev
acc8559a 933 do
e1622bfc 934 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
acc8559a
SH
935 then
936 # Use -f2 because if rev-list is telling us this commit is
937 # not worthwhile, we don't want to track its multiple heads,
938 # just the history of its first-parent for others that will
939 # be rebasing on top of it
d911d146 940 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
e249044c 941 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
e1622bfc 942 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
acc8559a
SH
943 rm "$REWRITTEN"/$rev
944 fi
945 done
946 fi
d911d146 947
ff74126c 948 test -s "$TODO" || echo noop >> "$TODO"
f59baa50 949 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
6047a234
JS
950 cat >> "$TODO" << EOF
951
d911d146 952# Rebase $SHORTREVISIONS onto $SHORTONTO
1b1dce4b
JS
953#
954# Commands:
88b1f0b8 955# p, pick = use commit
6741aa6c 956# r, reword = use commit, but edit the commit message
88b1f0b8
MV
957# e, edit = use commit, but stop for amending
958# s, squash = use commit, but meld into previous commit
0205e72f 959# f, fixup = like "squash", but discard this commit's log message
82576ddb
JS
960#
961# If you remove a line here THAT COMMIT WILL BE LOST.
6047a234 962# However, if you remove everything, the rebase will be aborted.
82576ddb 963#
1b1dce4b 964EOF
1b1dce4b 965
376ccb8c 966 has_action "$TODO" ||
c54b7817 967 die_abort "Nothing to do"
1b1dce4b
JS
968
969 cp "$TODO" "$TODO".backup
ef0c2abf 970 git_editor "$TODO" ||
e49ca974 971 die_abort "Could not execute editor"
1b1dce4b 972
376ccb8c 973 has_action "$TODO" ||
c54b7817
JS
974 die_abort "Nothing to do"
975
b4995494 976 test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
0e757e30 977
b096374f 978 output git checkout $ONTO || die_abort "could not detach HEAD"
22e40795 979 git update-ref ORIG_HEAD $HEAD
b096374f 980 do_rest
376ccb8c 981 ;;
1b1dce4b
JS
982 esac
983 shift
984done