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