rebase -i: fix post-rewrite hook with failed exec command
[git.git] / git-rebase--interactive.sh
blob9d413cbc128327ab047c5450d476cc45e178c763
1 # This shell script fragment is sourced by git-rebase to implement
2 # its interactive mode. "git rebase --interactive" makes it easy
3 # to fix up commits in the middle of a series and rearrange commits.
5 # Copyright (c) 2006 Johannes E. Schindelin
7 # The original idea comes from Eric W. Biederman, in
8 # http://article.gmane.org/gmane.comp.version-control.git/22407
10 # The file containing rebase commands, comments, and empty lines.
11 # This file is created by "git rebase -i" then edited by the user. As
12 # the lines are processed, they are removed from the front of this
13 # file and written to the tail of $done.
14 todo="$state_dir"/git-rebase-todo
16 # The rebase command lines that have already been processed. A line
17 # is moved here when it is first handled, before any associated user
18 # actions.
19 done="$state_dir"/done
21 # The commit message that is planned to be used for any changes that
22 # need to be committed following a user interaction.
23 msg="$state_dir"/message
25 # The file into which is accumulated the suggested commit message for
26 # squash/fixup commands. When the first of a series of squash/fixups
27 # is seen, the file is created and the commit message from the
28 # previous commit and from the first squash/fixup commit are written
29 # to it. The commit message for each subsequent squash/fixup commit
30 # is appended to the file as it is processed.
32 # The first line of the file is of the form
33 # # This is a combination of $count commits.
34 # where $count is the number of commits whose messages have been
35 # written to the file so far (including the initial "pick" commit).
36 # Each time that a commit message is processed, this line is read and
37 # updated. It is deleted just before the combined commit is made.
38 squash_msg="$state_dir"/message-squash
40 # If the current series of squash/fixups has not yet included a squash
41 # command, then this file exists and holds the commit message of the
42 # original "pick" commit. (If the series ends without a "squash"
43 # command, then this can be used as the commit message of the combined
44 # commit without opening the editor.)
45 fixup_msg="$state_dir"/message-fixup
47 # $rewritten is the name of a directory containing files for each
48 # commit that is reachable by at least one merge base of $head and
49 # $upstream. They are not necessarily rewritten, but their children
50 # might be. This ensures that commits on merged, but otherwise
51 # unrelated side branches are left alone. (Think "X" in the man page's
52 # example.)
53 rewritten="$state_dir"/rewritten
55 dropped="$state_dir"/dropped
57 end="$state_dir"/end
58 msgnum="$state_dir"/msgnum
60 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
61 # GIT_AUTHOR_DATE that will be used for the commit that is currently
62 # being rebased.
63 author_script="$state_dir"/author-script
65 # When an "edit" rebase command is being processed, the SHA1 of the
66 # commit to be edited is recorded in this file. When "git rebase
67 # --continue" is executed, if there are any staged changes then they
68 # will be amended to the HEAD commit, but only provided the HEAD
69 # commit is still the commit to be edited. When any other rebase
70 # command is processed, this file is deleted.
71 amend="$state_dir"/amend
73 # For the post-rewrite hook, we make a list of rewritten commits and
74 # their new sha1s. The rewritten-pending list keeps the sha1s of
75 # commits that have been processed, but not committed yet,
76 # e.g. because they are waiting for a 'squash' command.
77 rewritten_list="$state_dir"/rewritten-list
78 rewritten_pending="$state_dir"/rewritten-pending
80 strategy_args=
81 if test -n "$do_merge"
82 then
83 strategy_args=${strategy:+--strategy=$strategy}
84 eval '
85 for strategy_opt in '"$strategy_opts"'
87 strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")"
88 done
92 GIT_CHERRY_PICK_HELP="$resolvemsg"
93 export GIT_CHERRY_PICK_HELP
95 comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1)
96 : ${comment_char:=#}
98 warn () {
99 printf '%s\n' "$*" >&2
102 # Output the commit message for the specified commit.
103 commit_message () {
104 git cat-file commit "$1" | sed "1,/^$/d"
107 orig_reflog_action="$GIT_REFLOG_ACTION"
109 comment_for_reflog () {
110 case "$orig_reflog_action" in
111 ''|rebase*)
112 GIT_REFLOG_ACTION="rebase -i ($1)"
113 export GIT_REFLOG_ACTION
115 esac
118 last_count=
119 mark_action_done () {
120 sed -e 1q < "$todo" >> "$done"
121 sed -e 1d < "$todo" >> "$todo".new
122 mv -f "$todo".new "$todo"
123 new_count=$(git stripspace --strip-comments <"$done" | wc -l)
124 echo $new_count >"$msgnum"
125 total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l)))
126 echo $total >"$end"
127 if test "$last_count" != "$new_count"
128 then
129 last_count=$new_count
130 printf "Rebasing (%d/%d)\r" $new_count $total
131 test -z "$verbose" || echo
135 append_todo_help () {
136 git stripspace --comment-lines >>"$todo" <<\EOF
138 Commands:
139 p, pick = use commit
140 r, reword = use commit, but edit the commit message
141 e, edit = use commit, but stop for amending
142 s, squash = use commit, but meld into previous commit
143 f, fixup = like "squash", but discard this commit's log message
144 x, exec = run command (the rest of the line) using shell
146 These lines can be re-ordered; they are executed from top to bottom.
148 If you remove a line here THAT COMMIT WILL BE LOST.
152 make_patch () {
153 sha1_and_parents="$(git rev-list --parents -1 "$1")"
154 case "$sha1_and_parents" in
155 ?*' '?*' '?*)
156 git diff --cc $sha1_and_parents
158 ?*' '?*)
159 git diff-tree -p "$1^!"
162 echo "Root commit"
164 esac > "$state_dir"/patch
165 test -f "$msg" ||
166 commit_message "$1" > "$msg"
167 test -f "$author_script" ||
168 get_author_ident_from_commit "$1" > "$author_script"
171 die_with_patch () {
172 echo "$1" > "$state_dir"/stopped-sha
173 make_patch "$1"
174 git rerere
175 die "$2"
178 exit_with_patch () {
179 echo "$1" > "$state_dir"/stopped-sha
180 make_patch $1
181 git rev-parse --verify HEAD > "$amend"
182 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
183 warn "You can amend the commit now, with"
184 warn
185 warn " git commit --amend $gpg_sign_opt_quoted"
186 warn
187 warn "Once you are satisfied with your changes, run"
188 warn
189 warn " git rebase --continue"
190 warn
191 exit $2
194 die_abort () {
195 rm -rf "$state_dir"
196 die "$1"
199 has_action () {
200 test -n "$(git stripspace --strip-comments <"$1")"
203 is_empty_commit() {
204 tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null ||
205 die "$1: not a commit that can be picked")
206 ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null ||
207 ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904)
208 test "$tree" = "$ptree"
211 is_merge_commit()
213 git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
216 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
217 # GIT_AUTHOR_DATE exported from the current environment.
218 do_with_author () {
220 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
221 "$@"
225 git_sequence_editor () {
226 if test -z "$GIT_SEQUENCE_EDITOR"
227 then
228 GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
229 if [ -z "$GIT_SEQUENCE_EDITOR" ]
230 then
231 GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
235 eval "$GIT_SEQUENCE_EDITOR" '"$@"'
238 pick_one () {
239 ff=--ff
241 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
242 case "$force_rebase" in '') ;; ?*) ff= ;; esac
243 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
245 if is_empty_commit "$sha1"
246 then
247 empty_args="--allow-empty"
250 test -d "$rewritten" &&
251 pick_one_preserving_merges "$@" && return
252 output eval git cherry-pick \
253 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
254 "$strategy_args" $empty_args $ff "$@"
257 pick_one_preserving_merges () {
258 fast_forward=t
259 case "$1" in
261 fast_forward=f
262 sha1=$2
265 sha1=$1
267 esac
268 sha1=$(git rev-parse $sha1)
270 if test -f "$state_dir"/current-commit
271 then
272 if test "$fast_forward" = t
273 then
274 while read current_commit
276 git rev-parse HEAD > "$rewritten"/$current_commit
277 done <"$state_dir"/current-commit
278 rm "$state_dir"/current-commit ||
279 die "Cannot write current commit's replacement sha1"
283 echo $sha1 >> "$state_dir"/current-commit
285 # rewrite parents; if none were rewritten, we can fast-forward.
286 new_parents=
287 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
288 if test "$pend" = " "
289 then
290 pend=" root"
292 while [ "$pend" != "" ]
294 p=$(expr "$pend" : ' \([^ ]*\)')
295 pend="${pend# $p}"
297 if test -f "$rewritten"/$p
298 then
299 new_p=$(cat "$rewritten"/$p)
301 # If the todo reordered commits, and our parent is marked for
302 # rewriting, but hasn't been gotten to yet, assume the user meant to
303 # drop it on top of the current HEAD
304 if test -z "$new_p"
305 then
306 new_p=$(git rev-parse HEAD)
309 test $p != $new_p && fast_forward=f
310 case "$new_parents" in
311 *$new_p*)
312 ;; # do nothing; that parent is already there
314 new_parents="$new_parents $new_p"
316 esac
317 else
318 if test -f "$dropped"/$p
319 then
320 fast_forward=f
321 replacement="$(cat "$dropped"/$p)"
322 test -z "$replacement" && replacement=root
323 pend=" $replacement$pend"
324 else
325 new_parents="$new_parents $p"
328 done
329 case $fast_forward in
331 output warn "Fast-forward to $sha1"
332 output git reset --hard $sha1 ||
333 die "Cannot fast-forward to $sha1"
336 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
338 if [ "$1" != "-n" ]
339 then
340 # detach HEAD to current parent
341 output git checkout $first_parent 2> /dev/null ||
342 die "Cannot move HEAD to $first_parent"
345 case "$new_parents" in
346 ' '*' '*)
347 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
349 # redo merge
350 author_script_content=$(get_author_ident_from_commit $sha1)
351 eval "$author_script_content"
352 msg_content="$(commit_message $sha1)"
353 # No point in merging the first parent, that's HEAD
354 new_parents=${new_parents# $first_parent}
355 merge_args="--no-log --no-ff"
356 if ! do_with_author output eval \
357 'git merge ${gpg_sign_opt:+"$gpg_sign_opt"} \
358 $merge_args $strategy_args -m "$msg_content" $new_parents'
359 then
360 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
361 die_with_patch $sha1 "Error redoing merge $sha1"
363 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
366 output eval git cherry-pick \
367 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
368 "$strategy_args" "$@" ||
369 die_with_patch $sha1 "Could not pick $sha1"
371 esac
373 esac
376 nth_string () {
377 case "$1" in
378 *1[0-9]|*[04-9]) echo "$1"th;;
379 *1) echo "$1"st;;
380 *2) echo "$1"nd;;
381 *3) echo "$1"rd;;
382 esac
385 update_squash_messages () {
386 if test -f "$squash_msg"; then
387 mv "$squash_msg" "$squash_msg".bak || exit
388 count=$(($(sed -n \
389 -e "1s/^. This is a combination of \(.*\) commits\./\1/p" \
390 -e "q" < "$squash_msg".bak)+1))
392 printf '%s\n' "$comment_char This is a combination of $count commits."
393 sed -e 1d -e '2,/^./{
394 /^$/d
395 }' <"$squash_msg".bak
396 } >"$squash_msg"
397 else
398 commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
399 count=2
401 printf '%s\n' "$comment_char This is a combination of 2 commits."
402 printf '%s\n' "$comment_char The first commit's message is:"
403 echo
404 cat "$fixup_msg"
405 } >"$squash_msg"
407 case $1 in
408 squash)
409 rm -f "$fixup_msg"
410 echo
411 printf '%s\n' "$comment_char This is the $(nth_string $count) commit message:"
412 echo
413 commit_message $2
415 fixup)
416 echo
417 printf '%s\n' "$comment_char The $(nth_string $count) commit message will be skipped:"
418 echo
419 # Change the space after the comment character to TAB:
420 commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
422 esac >>"$squash_msg"
425 peek_next_command () {
426 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
429 # A squash/fixup has failed. Prepare the long version of the squash
430 # commit message, then die_with_patch. This code path requires the
431 # user to edit the combined commit message for all commits that have
432 # been squashed/fixedup so far. So also erase the old squash
433 # messages, effectively causing the combined commit to be used as the
434 # new basis for any further squash/fixups. Args: sha1 rest
435 die_failed_squash() {
436 mv "$squash_msg" "$msg" || exit
437 rm -f "$fixup_msg"
438 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
439 warn
440 warn "Could not apply $1... $2"
441 die_with_patch $1 ""
444 flush_rewritten_pending() {
445 test -s "$rewritten_pending" || return
446 newsha1="$(git rev-parse HEAD^0)"
447 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
448 rm -f "$rewritten_pending"
451 record_in_rewritten() {
452 oldsha1="$(git rev-parse $1)"
453 echo "$oldsha1" >> "$rewritten_pending"
455 case "$(peek_next_command)" in
456 squash|s|fixup|f)
459 flush_rewritten_pending
461 esac
464 do_pick () {
465 if test "$(git rev-parse HEAD)" = "$squash_onto"
466 then
467 # Set the correct commit message and author info on the
468 # sentinel root before cherry-picking the original changes
469 # without committing (-n). Finally, update the sentinel again
470 # to include these changes. If the cherry-pick results in a
471 # conflict, this means our behaviour is similar to a standard
472 # failed cherry-pick during rebase, with a dirty index to
473 # resolve before manually running git commit --amend then git
474 # rebase --continue.
475 git commit --allow-empty --allow-empty-message --amend \
476 --no-post-rewrite -n -q -C $1 &&
477 pick_one -n $1 &&
478 git commit --allow-empty --allow-empty-message \
479 --amend --no-post-rewrite -n -q -C $1 \
480 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
481 die_with_patch $1 "Could not apply $1... $2"
482 else
483 pick_one $1 ||
484 die_with_patch $1 "Could not apply $1... $2"
488 do_next () {
489 rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
490 read -r command sha1 rest < "$todo"
491 case "$command" in
492 "$comment_char"*|''|noop)
493 mark_action_done
495 pick|p)
496 comment_for_reflog pick
498 mark_action_done
499 do_pick $sha1 "$rest"
500 record_in_rewritten $sha1
502 reword|r)
503 comment_for_reflog reword
505 mark_action_done
506 do_pick $sha1 "$rest"
507 git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} || {
508 warn "Could not amend commit after successfully picking $sha1... $rest"
509 warn "This is most likely due to an empty commit message, or the pre-commit hook"
510 warn "failed. If the pre-commit hook failed, you may need to resolve the issue before"
511 warn "you are able to reword the commit."
512 exit_with_patch $sha1 1
514 record_in_rewritten $sha1
516 edit|e)
517 comment_for_reflog edit
519 mark_action_done
520 do_pick $sha1 "$rest"
521 warn "Stopped at $sha1... $rest"
522 exit_with_patch $sha1 0
524 squash|s|fixup|f)
525 case "$command" in
526 squash|s)
527 squash_style=squash
529 fixup|f)
530 squash_style=fixup
532 esac
533 comment_for_reflog $squash_style
535 test -f "$done" && has_action "$done" ||
536 die "Cannot '$squash_style' without a previous commit"
538 mark_action_done
539 update_squash_messages $squash_style $sha1
540 author_script_content=$(get_author_ident_from_commit HEAD)
541 echo "$author_script_content" > "$author_script"
542 eval "$author_script_content"
543 if ! pick_one -n $sha1
544 then
545 git rev-parse --verify HEAD >"$amend"
546 die_failed_squash $sha1 "$rest"
548 case "$(peek_next_command)" in
549 squash|s|fixup|f)
550 # This is an intermediate commit; its message will only be
551 # used in case of trouble. So use the long version:
552 do_with_author output git commit --amend --no-verify -F "$squash_msg" \
553 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
554 die_failed_squash $sha1 "$rest"
557 # This is the final command of this squash/fixup group
558 if test -f "$fixup_msg"
559 then
560 do_with_author git commit --amend --no-verify -F "$fixup_msg" \
561 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
562 die_failed_squash $sha1 "$rest"
563 else
564 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
565 rm -f "$GIT_DIR"/MERGE_MSG
566 do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
567 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
568 die_failed_squash $sha1 "$rest"
570 rm -f "$squash_msg" "$fixup_msg"
572 esac
573 record_in_rewritten $sha1
575 x|"exec")
576 read -r command rest < "$todo"
577 mark_action_done
578 printf 'Executing: %s\n' "$rest"
579 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
580 status=$?
581 # Run in subshell because require_clean_work_tree can die.
582 dirty=f
583 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
584 if test "$status" -ne 0
585 then
586 warn "Execution failed: $rest"
587 test "$dirty" = f ||
588 warn "and made changes to the index and/or the working tree"
590 warn "You can fix the problem, and then run"
591 warn
592 warn " git rebase --continue"
593 warn
594 if test $status -eq 127 # command not found
595 then
596 status=1
598 exit "$status"
599 elif test "$dirty" = t
600 then
601 warn "Execution succeeded: $rest"
602 warn "but left changes to the index and/or the working tree"
603 warn "Commit or stash your changes, and then run"
604 warn
605 warn " git rebase --continue"
606 warn
607 exit 1
611 warn "Unknown command: $command $sha1 $rest"
612 fixtodo="Please fix this using 'git rebase --edit-todo'."
613 if git rev-parse --verify -q "$sha1" >/dev/null
614 then
615 die_with_patch $sha1 "$fixtodo"
616 else
617 die "$fixtodo"
620 esac
621 test -s "$todo" && return
623 comment_for_reflog finish &&
624 newhead=$(git rev-parse HEAD) &&
625 case $head_name in
626 refs/*)
627 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
628 git update-ref -m "$message" $head_name $newhead $orig_head &&
629 git symbolic-ref \
630 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
631 HEAD $head_name
633 esac && {
634 test ! -f "$state_dir"/verbose ||
635 git diff-tree --stat $orig_head..HEAD
636 } &&
638 test -s "$rewritten_list" &&
639 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
640 true # we don't care if this copying failed
641 } &&
642 if test -x "$GIT_DIR"/hooks/post-rewrite &&
643 test -s "$rewritten_list"; then
644 "$GIT_DIR"/hooks/post-rewrite rebase < "$rewritten_list"
645 true # we don't care if this hook failed
646 fi &&
647 warn "Successfully rebased and updated $head_name."
649 return 1 # not failure; just to break the do_rest loop
652 # can only return 0, when the infinite loop breaks
653 do_rest () {
654 while :
656 do_next || break
657 done
660 # skip picking commits whose parents are unchanged
661 skip_unnecessary_picks () {
662 fd=3
663 while read -r command rest
665 # fd=3 means we skip the command
666 case "$fd,$command" in
667 3,pick|3,p)
668 # pick a commit whose parent is current $onto -> skip
669 sha1=${rest%% *}
670 case "$(git rev-parse --verify --quiet "$sha1"^)" in
671 "$onto"*)
672 onto=$sha1
675 fd=1
677 esac
679 3,"$comment_char"*|3,)
680 # copy comments
683 fd=1
685 esac
686 printf '%s\n' "$command${rest:+ }$rest" >&$fd
687 done <"$todo" >"$todo.new" 3>>"$done" &&
688 mv -f "$todo".new "$todo" &&
689 case "$(peek_next_command)" in
690 squash|s|fixup|f)
691 record_in_rewritten "$onto"
693 esac ||
694 die "Could not skip unnecessary pick commands"
697 transform_todo_ids () {
698 while read -r command rest
700 case "$command" in
701 "$comment_char"* | exec)
702 # Be careful for oddball commands like 'exec'
703 # that do not have a SHA-1 at the beginning of $rest.
706 sha1=$(git rev-parse --verify --quiet "$@" ${rest%% *}) &&
707 rest="$sha1 ${rest#* }"
709 esac
710 printf '%s\n' "$command${rest:+ }$rest"
711 done <"$todo" >"$todo.new" &&
712 mv -f "$todo.new" "$todo"
715 expand_todo_ids() {
716 transform_todo_ids
719 collapse_todo_ids() {
720 transform_todo_ids --short
723 # Rearrange the todo list that has both "pick sha1 msg" and
724 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
725 # comes immediately after the former, and change "pick" to
726 # "fixup"/"squash".
727 rearrange_squash () {
728 # extract fixup!/squash! lines and resolve any referenced sha1's
729 while read -r pick sha1 message
731 case "$message" in
732 "squash! "*|"fixup! "*)
733 action="${message%%!*}"
734 rest=$message
735 prefix=
736 # skip all squash! or fixup! (but save for later)
737 while :
739 case "$rest" in
740 "squash! "*|"fixup! "*)
741 prefix="$prefix${rest%%!*},"
742 rest="${rest#*! }"
745 break
747 esac
748 done
749 printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
750 # if it's a single word, try to resolve to a full sha1 and
751 # emit a second copy. This allows us to match on both message
752 # and on sha1 prefix
753 if test "${rest#* }" = "$rest"; then
754 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
755 if test -n "$fullsha"; then
756 # prefix the action to uniquely identify this line as
757 # intended for full sha1 match
758 echo "$sha1 +$action $prefix $fullsha"
761 esac
762 done >"$1.sq" <"$1"
763 test -s "$1.sq" || return
765 used=
766 while read -r pick sha1 message
768 case " $used" in
769 *" $sha1 "*) continue ;;
770 esac
771 printf '%s\n' "$pick $sha1 $message"
772 used="$used$sha1 "
773 while read -r squash action msg_prefix msg_content
775 case " $used" in
776 *" $squash "*) continue ;;
777 esac
778 emit=0
779 case "$action" in
781 action="${action#+}"
782 # full sha1 prefix test
783 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
785 # message prefix test
786 case "$message" in "$msg_content"*) emit=1;; esac ;;
787 esac
788 if test $emit = 1; then
789 real_prefix=$(echo "$msg_prefix" | sed "s/,/! /g")
790 printf '%s\n' "$action $squash ${real_prefix}$msg_content"
791 used="$used$squash "
793 done <"$1.sq"
794 done >"$1.rearranged" <"$1"
795 cat "$1.rearranged" >"$1"
796 rm -f "$1.sq" "$1.rearranged"
799 # Add commands after a pick or after a squash/fixup serie
800 # in the todo list.
801 add_exec_commands () {
803 first=t
804 while read -r insn rest
806 case $insn in
807 pick)
808 test -n "$first" ||
809 printf "%s" "$cmd"
811 esac
812 printf "%s %s\n" "$insn" "$rest"
813 first=
814 done
815 printf "%s" "$cmd"
816 } <"$1" >"$1.new" &&
817 mv "$1.new" "$1"
820 # The whole contents of this file is run by dot-sourcing it from
821 # inside a shell function. It used to be that "return"s we see
822 # below were not inside any function, and expected to return
823 # to the function that dot-sourced us.
825 # However, FreeBSD /bin/sh misbehaves on such a construct and
826 # continues to run the statements that follow such a "return".
827 # As a work-around, we introduce an extra layer of a function
828 # here, and immediately call it after defining it.
829 git_rebase__interactive () {
831 case "$action" in
832 continue)
833 # do we have anything to commit?
834 if git diff-index --cached --quiet HEAD --
835 then
836 : Nothing to commit -- skip this
837 else
838 if ! test -f "$author_script"
839 then
840 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
841 die "You have staged changes in your working tree. If these changes are meant to be
842 squashed into the previous commit, run:
844 git commit --amend $gpg_sign_opt_quoted
846 If they are meant to go into a new commit, run:
848 git commit $gpg_sign_opt_quoted
850 In both case, once you're done, continue with:
852 git rebase --continue
855 . "$author_script" ||
856 die "Error trying to find the author identity to amend commit"
857 if test -f "$amend"
858 then
859 current_head=$(git rev-parse --verify HEAD)
860 test "$current_head" = $(cat "$amend") ||
861 die "\
862 You have uncommitted changes in your working tree. Please, commit them
863 first and then run 'git rebase --continue' again."
864 do_with_author git commit --amend --no-verify -F "$msg" -e \
865 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
866 die "Could not commit staged changes."
867 else
868 do_with_author git commit --no-verify -F "$msg" -e \
869 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
870 die "Could not commit staged changes."
874 if test -r "$state_dir"/stopped-sha
875 then
876 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
879 require_clean_work_tree "rebase"
880 do_rest
881 return 0
883 skip)
884 git rerere clear
886 do_rest
887 return 0
889 edit-todo)
890 git stripspace --strip-comments <"$todo" >"$todo".new
891 mv -f "$todo".new "$todo"
892 collapse_todo_ids
893 append_todo_help
894 git stripspace --comment-lines >>"$todo" <<\EOF
896 You are editing the todo file of an ongoing interactive rebase.
897 To continue rebase after editing, run:
898 git rebase --continue
902 git_sequence_editor "$todo" ||
903 die "Could not execute editor"
904 expand_todo_ids
906 exit
908 esac
910 git var GIT_COMMITTER_IDENT >/dev/null ||
911 die "You need to set your committer info first"
913 comment_for_reflog start
915 if test ! -z "$switch_to"
916 then
917 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
918 output git checkout "$switch_to" -- ||
919 die "Could not checkout $switch_to"
921 comment_for_reflog start
924 orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?"
925 mkdir -p "$state_dir" || die "Could not create temporary $state_dir"
927 : > "$state_dir"/interactive || die "Could not mark as interactive"
928 write_basic_state
929 if test t = "$preserve_merges"
930 then
931 if test -z "$rebase_root"
932 then
933 mkdir "$rewritten" &&
934 for c in $(git merge-base --all $orig_head $upstream)
936 echo $onto > "$rewritten"/$c ||
937 die "Could not init rewritten commits"
938 done
939 else
940 mkdir "$rewritten" &&
941 echo $onto > "$rewritten"/root ||
942 die "Could not init rewritten commits"
944 # No cherry-pick because our first pass is to determine
945 # parents to rewrite and skipping dropped commits would
946 # prematurely end our probe
947 merges_option=
948 else
949 merges_option="--no-merges --cherry-pick"
952 shorthead=$(git rev-parse --short $orig_head)
953 shortonto=$(git rev-parse --short $onto)
954 if test -z "$rebase_root"
955 # this is now equivalent to ! -z "$upstream"
956 then
957 shortupstream=$(git rev-parse --short $upstream)
958 revisions=$upstream...$orig_head
959 shortrevisions=$shortupstream..$shorthead
960 else
961 revisions=$onto...$orig_head
962 shortrevisions=$shorthead
964 git rev-list $merges_option --pretty=oneline --abbrev-commit \
965 --abbrev=7 --reverse --left-right --topo-order \
966 $revisions | \
967 sed -n "s/^>//p" |
968 while read -r shortsha1 rest
971 if test -z "$keep_empty" && is_empty_commit $shortsha1 && ! is_merge_commit $shortsha1
972 then
973 comment_out="$comment_char "
974 else
975 comment_out=
978 if test t != "$preserve_merges"
979 then
980 printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
981 else
982 sha1=$(git rev-parse $shortsha1)
983 if test -z "$rebase_root"
984 then
985 preserve=t
986 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
988 if test -f "$rewritten"/$p
989 then
990 preserve=f
992 done
993 else
994 preserve=f
996 if test f = "$preserve"
997 then
998 touch "$rewritten"/$sha1
999 printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
1002 done
1004 # Watch for commits that been dropped by --cherry-pick
1005 if test t = "$preserve_merges"
1006 then
1007 mkdir "$dropped"
1008 # Save all non-cherry-picked changes
1009 git rev-list $revisions --left-right --cherry-pick | \
1010 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
1011 # Now all commits and note which ones are missing in
1012 # not-cherry-picks and hence being dropped
1013 git rev-list $revisions |
1014 while read rev
1016 if test -f "$rewritten"/$rev -a "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
1017 then
1018 # Use -f2 because if rev-list is telling us this commit is
1019 # not worthwhile, we don't want to track its multiple heads,
1020 # just the history of its first-parent for others that will
1021 # be rebasing on top of it
1022 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
1023 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
1024 sane_grep -v "^[a-z][a-z]* $short" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
1025 rm "$rewritten"/$rev
1027 done
1030 test -s "$todo" || echo noop >> "$todo"
1031 test -n "$autosquash" && rearrange_squash "$todo"
1032 test -n "$cmd" && add_exec_commands "$todo"
1034 cat >>"$todo" <<EOF
1036 $comment_char Rebase $shortrevisions onto $shortonto
1038 append_todo_help
1039 git stripspace --comment-lines >>"$todo" <<\EOF
1041 However, if you remove everything, the rebase will be aborted.
1045 if test -z "$keep_empty"
1046 then
1047 printf '%s\n' "$comment_char Note that empty commits are commented out" >>"$todo"
1051 has_action "$todo" ||
1052 return 2
1054 cp "$todo" "$todo".backup
1055 git_sequence_editor "$todo" ||
1056 die_abort "Could not execute editor"
1058 has_action "$todo" ||
1059 return 2
1061 expand_todo_ids
1063 test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
1065 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
1066 output git checkout $onto || die_abort "could not detach HEAD"
1067 git update-ref ORIG_HEAD $orig_head
1068 do_rest
1071 # ... and then we call the whole thing.
1072 git_rebase__interactive