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