Merge branch 'pt/pull-builtin'
[git/mingw.git] / git-rebase--interactive.sh
blob2f6ce55b586d7e71cd954409c30e6574dfcf301e
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 # Put the last action marked done at the beginning of the todo list
136 # again. If there has not been an action marked done yet, leave the list of
137 # items on the todo list unchanged.
138 reschedule_last_action () {
139 tail -n 1 "$done" | cat - "$todo" >"$todo".new
140 sed -e \$d <"$done" >"$done".new
141 mv -f "$todo".new "$todo"
142 mv -f "$done".new "$done"
145 append_todo_help () {
146 git stripspace --comment-lines >>"$todo" <<\EOF
148 Commands:
149 p, pick = use commit
150 r, reword = use commit, but edit the commit message
151 e, edit = use commit, but stop for amending
152 s, squash = use commit, but meld into previous commit
153 f, fixup = like "squash", but discard this commit's log message
154 x, exec = run command (the rest of the line) using shell
156 These lines can be re-ordered; they are executed from top to bottom.
158 If you remove a line here THAT COMMIT WILL BE LOST.
162 make_patch () {
163 sha1_and_parents="$(git rev-list --parents -1 "$1")"
164 case "$sha1_and_parents" in
165 ?*' '?*' '?*)
166 git diff --cc $sha1_and_parents
168 ?*' '?*)
169 git diff-tree -p "$1^!"
172 echo "Root commit"
174 esac > "$state_dir"/patch
175 test -f "$msg" ||
176 commit_message "$1" > "$msg"
177 test -f "$author_script" ||
178 get_author_ident_from_commit "$1" > "$author_script"
181 die_with_patch () {
182 echo "$1" > "$state_dir"/stopped-sha
183 make_patch "$1"
184 git rerere
185 die "$2"
188 exit_with_patch () {
189 echo "$1" > "$state_dir"/stopped-sha
190 make_patch $1
191 git rev-parse --verify HEAD > "$amend"
192 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
193 warn "You can amend the commit now, with"
194 warn
195 warn " git commit --amend $gpg_sign_opt_quoted"
196 warn
197 warn "Once you are satisfied with your changes, run"
198 warn
199 warn " git rebase --continue"
200 warn
201 exit $2
204 die_abort () {
205 rm -rf "$state_dir"
206 die "$1"
209 has_action () {
210 test -n "$(git stripspace --strip-comments <"$1")"
213 is_empty_commit() {
214 tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null ||
215 die "$1: not a commit that can be picked")
216 ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null ||
217 ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904)
218 test "$tree" = "$ptree"
221 is_merge_commit()
223 git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
226 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
227 # GIT_AUTHOR_DATE exported from the current environment.
228 do_with_author () {
230 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
231 "$@"
235 git_sequence_editor () {
236 if test -z "$GIT_SEQUENCE_EDITOR"
237 then
238 GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
239 if [ -z "$GIT_SEQUENCE_EDITOR" ]
240 then
241 GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
245 eval "$GIT_SEQUENCE_EDITOR" '"$@"'
248 pick_one () {
249 ff=--ff
251 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
252 case "$force_rebase" in '') ;; ?*) ff= ;; esac
253 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
255 if is_empty_commit "$sha1"
256 then
257 empty_args="--allow-empty"
260 test -d "$rewritten" &&
261 pick_one_preserving_merges "$@" && return
262 output eval git cherry-pick \
263 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
264 "$strategy_args" $empty_args $ff "$@"
266 # If cherry-pick dies it leaves the to-be-picked commit unrecorded. Reschedule
267 # previous task so this commit is not lost.
268 ret=$?
269 case "$ret" in [01]) ;; *) reschedule_last_action ;; esac
270 return $ret
273 pick_one_preserving_merges () {
274 fast_forward=t
275 case "$1" in
277 fast_forward=f
278 sha1=$2
281 sha1=$1
283 esac
284 sha1=$(git rev-parse $sha1)
286 if test -f "$state_dir"/current-commit
287 then
288 if test "$fast_forward" = t
289 then
290 while read current_commit
292 git rev-parse HEAD > "$rewritten"/$current_commit
293 done <"$state_dir"/current-commit
294 rm "$state_dir"/current-commit ||
295 die "Cannot write current commit's replacement sha1"
299 echo $sha1 >> "$state_dir"/current-commit
301 # rewrite parents; if none were rewritten, we can fast-forward.
302 new_parents=
303 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
304 if test "$pend" = " "
305 then
306 pend=" root"
308 while [ "$pend" != "" ]
310 p=$(expr "$pend" : ' \([^ ]*\)')
311 pend="${pend# $p}"
313 if test -f "$rewritten"/$p
314 then
315 new_p=$(cat "$rewritten"/$p)
317 # If the todo reordered commits, and our parent is marked for
318 # rewriting, but hasn't been gotten to yet, assume the user meant to
319 # drop it on top of the current HEAD
320 if test -z "$new_p"
321 then
322 new_p=$(git rev-parse HEAD)
325 test $p != $new_p && fast_forward=f
326 case "$new_parents" in
327 *$new_p*)
328 ;; # do nothing; that parent is already there
330 new_parents="$new_parents $new_p"
332 esac
333 else
334 if test -f "$dropped"/$p
335 then
336 fast_forward=f
337 replacement="$(cat "$dropped"/$p)"
338 test -z "$replacement" && replacement=root
339 pend=" $replacement$pend"
340 else
341 new_parents="$new_parents $p"
344 done
345 case $fast_forward in
347 output warn "Fast-forward to $sha1"
348 output git reset --hard $sha1 ||
349 die "Cannot fast-forward to $sha1"
352 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
354 if [ "$1" != "-n" ]
355 then
356 # detach HEAD to current parent
357 output git checkout $first_parent 2> /dev/null ||
358 die "Cannot move HEAD to $first_parent"
361 case "$new_parents" in
362 ' '*' '*)
363 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
365 # redo merge
366 author_script_content=$(get_author_ident_from_commit $sha1)
367 eval "$author_script_content"
368 msg_content="$(commit_message $sha1)"
369 # No point in merging the first parent, that's HEAD
370 new_parents=${new_parents# $first_parent}
371 merge_args="--no-log --no-ff"
372 if ! do_with_author output eval \
373 'git merge ${gpg_sign_opt:+"$gpg_sign_opt"} \
374 $merge_args $strategy_args -m "$msg_content" $new_parents'
375 then
376 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
377 die_with_patch $sha1 "Error redoing merge $sha1"
379 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
382 output eval git cherry-pick \
383 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
384 "$strategy_args" "$@" ||
385 die_with_patch $sha1 "Could not pick $sha1"
387 esac
389 esac
392 nth_string () {
393 case "$1" in
394 *1[0-9]|*[04-9]) echo "$1"th;;
395 *1) echo "$1"st;;
396 *2) echo "$1"nd;;
397 *3) echo "$1"rd;;
398 esac
401 update_squash_messages () {
402 if test -f "$squash_msg"; then
403 mv "$squash_msg" "$squash_msg".bak || exit
404 count=$(($(sed -n \
405 -e "1s/^. This is a combination of \(.*\) commits\./\1/p" \
406 -e "q" < "$squash_msg".bak)+1))
408 printf '%s\n' "$comment_char This is a combination of $count commits."
409 sed -e 1d -e '2,/^./{
410 /^$/d
411 }' <"$squash_msg".bak
412 } >"$squash_msg"
413 else
414 commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
415 count=2
417 printf '%s\n' "$comment_char This is a combination of 2 commits."
418 printf '%s\n' "$comment_char The first commit's message is:"
419 echo
420 cat "$fixup_msg"
421 } >"$squash_msg"
423 case $1 in
424 squash)
425 rm -f "$fixup_msg"
426 echo
427 printf '%s\n' "$comment_char This is the $(nth_string $count) commit message:"
428 echo
429 commit_message $2
431 fixup)
432 echo
433 printf '%s\n' "$comment_char The $(nth_string $count) commit message will be skipped:"
434 echo
435 # Change the space after the comment character to TAB:
436 commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
438 esac >>"$squash_msg"
441 peek_next_command () {
442 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
445 # A squash/fixup has failed. Prepare the long version of the squash
446 # commit message, then die_with_patch. This code path requires the
447 # user to edit the combined commit message for all commits that have
448 # been squashed/fixedup so far. So also erase the old squash
449 # messages, effectively causing the combined commit to be used as the
450 # new basis for any further squash/fixups. Args: sha1 rest
451 die_failed_squash() {
452 mv "$squash_msg" "$msg" || exit
453 rm -f "$fixup_msg"
454 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
455 warn
456 warn "Could not apply $1... $2"
457 die_with_patch $1 ""
460 flush_rewritten_pending() {
461 test -s "$rewritten_pending" || return
462 newsha1="$(git rev-parse HEAD^0)"
463 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
464 rm -f "$rewritten_pending"
467 record_in_rewritten() {
468 oldsha1="$(git rev-parse $1)"
469 echo "$oldsha1" >> "$rewritten_pending"
471 case "$(peek_next_command)" in
472 squash|s|fixup|f)
475 flush_rewritten_pending
477 esac
480 do_pick () {
481 if test "$(git rev-parse HEAD)" = "$squash_onto"
482 then
483 # Set the correct commit message and author info on the
484 # sentinel root before cherry-picking the original changes
485 # without committing (-n). Finally, update the sentinel again
486 # to include these changes. If the cherry-pick results in a
487 # conflict, this means our behaviour is similar to a standard
488 # failed cherry-pick during rebase, with a dirty index to
489 # resolve before manually running git commit --amend then git
490 # rebase --continue.
491 git commit --allow-empty --allow-empty-message --amend \
492 --no-post-rewrite -n -q -C $1 &&
493 pick_one -n $1 &&
494 git commit --allow-empty --allow-empty-message \
495 --amend --no-post-rewrite -n -q -C $1 \
496 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
497 die_with_patch $1 "Could not apply $1... $2"
498 else
499 pick_one $1 ||
500 die_with_patch $1 "Could not apply $1... $2"
504 do_next () {
505 rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
506 read -r command sha1 rest < "$todo"
507 case "$command" in
508 "$comment_char"*|''|noop)
509 mark_action_done
511 pick|p)
512 comment_for_reflog pick
514 mark_action_done
515 do_pick $sha1 "$rest"
516 record_in_rewritten $sha1
518 reword|r)
519 comment_for_reflog reword
521 mark_action_done
522 do_pick $sha1 "$rest"
523 git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} || {
524 warn "Could not amend commit after successfully picking $sha1... $rest"
525 warn "This is most likely due to an empty commit message, or the pre-commit hook"
526 warn "failed. If the pre-commit hook failed, you may need to resolve the issue before"
527 warn "you are able to reword the commit."
528 exit_with_patch $sha1 1
530 record_in_rewritten $sha1
532 edit|e)
533 comment_for_reflog edit
535 mark_action_done
536 do_pick $sha1 "$rest"
537 warn "Stopped at $sha1... $rest"
538 exit_with_patch $sha1 0
540 squash|s|fixup|f)
541 case "$command" in
542 squash|s)
543 squash_style=squash
545 fixup|f)
546 squash_style=fixup
548 esac
549 comment_for_reflog $squash_style
551 test -f "$done" && has_action "$done" ||
552 die "Cannot '$squash_style' without a previous commit"
554 mark_action_done
555 update_squash_messages $squash_style $sha1
556 author_script_content=$(get_author_ident_from_commit HEAD)
557 echo "$author_script_content" > "$author_script"
558 eval "$author_script_content"
559 if ! pick_one -n $sha1
560 then
561 git rev-parse --verify HEAD >"$amend"
562 die_failed_squash $sha1 "$rest"
564 case "$(peek_next_command)" in
565 squash|s|fixup|f)
566 # This is an intermediate commit; its message will only be
567 # used in case of trouble. So use the long version:
568 do_with_author output git commit --amend --no-verify -F "$squash_msg" \
569 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
570 die_failed_squash $sha1 "$rest"
573 # This is the final command of this squash/fixup group
574 if test -f "$fixup_msg"
575 then
576 do_with_author git commit --amend --no-verify -F "$fixup_msg" \
577 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
578 die_failed_squash $sha1 "$rest"
579 else
580 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
581 rm -f "$GIT_DIR"/MERGE_MSG
582 do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
583 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
584 die_failed_squash $sha1 "$rest"
586 rm -f "$squash_msg" "$fixup_msg"
588 esac
589 record_in_rewritten $sha1
591 x|"exec")
592 read -r command rest < "$todo"
593 mark_action_done
594 printf 'Executing: %s\n' "$rest"
595 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
596 status=$?
597 # Run in subshell because require_clean_work_tree can die.
598 dirty=f
599 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
600 if test "$status" -ne 0
601 then
602 warn "Execution failed: $rest"
603 test "$dirty" = f ||
604 warn "and made changes to the index and/or the working tree"
606 warn "You can fix the problem, and then run"
607 warn
608 warn " git rebase --continue"
609 warn
610 if test $status -eq 127 # command not found
611 then
612 status=1
614 exit "$status"
615 elif test "$dirty" = t
616 then
617 warn "Execution succeeded: $rest"
618 warn "but left changes to the index and/or the working tree"
619 warn "Commit or stash your changes, and then run"
620 warn
621 warn " git rebase --continue"
622 warn
623 exit 1
627 warn "Unknown command: $command $sha1 $rest"
628 fixtodo="Please fix this using 'git rebase --edit-todo'."
629 if git rev-parse --verify -q "$sha1" >/dev/null
630 then
631 die_with_patch $sha1 "$fixtodo"
632 else
633 die "$fixtodo"
636 esac
637 test -s "$todo" && return
639 comment_for_reflog finish &&
640 newhead=$(git rev-parse HEAD) &&
641 case $head_name in
642 refs/*)
643 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
644 git update-ref -m "$message" $head_name $newhead $orig_head &&
645 git symbolic-ref \
646 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
647 HEAD $head_name
649 esac && {
650 test ! -f "$state_dir"/verbose ||
651 git diff-tree --stat $orig_head..HEAD
652 } &&
654 test -s "$rewritten_list" &&
655 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
656 true # we don't care if this copying failed
657 } &&
658 hook="$(git rev-parse --git-path hooks/post-rewrite)"
659 if test -x "$hook" && test -s "$rewritten_list"; then
660 "$hook" rebase < "$rewritten_list"
661 true # we don't care if this hook failed
662 fi &&
663 warn "Successfully rebased and updated $head_name."
665 return 1 # not failure; just to break the do_rest loop
668 # can only return 0, when the infinite loop breaks
669 do_rest () {
670 while :
672 do_next || break
673 done
676 # skip picking commits whose parents are unchanged
677 skip_unnecessary_picks () {
678 fd=3
679 while read -r command rest
681 # fd=3 means we skip the command
682 case "$fd,$command" in
683 3,pick|3,p)
684 # pick a commit whose parent is current $onto -> skip
685 sha1=${rest%% *}
686 case "$(git rev-parse --verify --quiet "$sha1"^)" in
687 "$onto"*)
688 onto=$sha1
691 fd=1
693 esac
695 3,"$comment_char"*|3,)
696 # copy comments
699 fd=1
701 esac
702 printf '%s\n' "$command${rest:+ }$rest" >&$fd
703 done <"$todo" >"$todo.new" 3>>"$done" &&
704 mv -f "$todo".new "$todo" &&
705 case "$(peek_next_command)" in
706 squash|s|fixup|f)
707 record_in_rewritten "$onto"
709 esac ||
710 die "Could not skip unnecessary pick commands"
713 transform_todo_ids () {
714 while read -r command rest
716 case "$command" in
717 "$comment_char"* | exec)
718 # Be careful for oddball commands like 'exec'
719 # that do not have a SHA-1 at the beginning of $rest.
722 sha1=$(git rev-parse --verify --quiet "$@" ${rest%% *}) &&
723 rest="$sha1 ${rest#* }"
725 esac
726 printf '%s\n' "$command${rest:+ }$rest"
727 done <"$todo" >"$todo.new" &&
728 mv -f "$todo.new" "$todo"
731 expand_todo_ids() {
732 transform_todo_ids
735 collapse_todo_ids() {
736 transform_todo_ids --short
739 # Rearrange the todo list that has both "pick sha1 msg" and
740 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
741 # comes immediately after the former, and change "pick" to
742 # "fixup"/"squash".
744 # Note that if the config has specified a custom instruction format
745 # each log message will be re-retrieved in order to normalize the
746 # autosquash arrangement
747 rearrange_squash () {
748 # extract fixup!/squash! lines and resolve any referenced sha1's
749 while read -r pick sha1 message
751 test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
752 case "$message" in
753 "squash! "*|"fixup! "*)
754 action="${message%%!*}"
755 rest=$message
756 prefix=
757 # skip all squash! or fixup! (but save for later)
758 while :
760 case "$rest" in
761 "squash! "*|"fixup! "*)
762 prefix="$prefix${rest%%!*},"
763 rest="${rest#*! }"
766 break
768 esac
769 done
770 printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
771 # if it's a single word, try to resolve to a full sha1 and
772 # emit a second copy. This allows us to match on both message
773 # and on sha1 prefix
774 if test "${rest#* }" = "$rest"; then
775 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
776 if test -n "$fullsha"; then
777 # prefix the action to uniquely identify this line as
778 # intended for full sha1 match
779 echo "$sha1 +$action $prefix $fullsha"
782 esac
783 done >"$1.sq" <"$1"
784 test -s "$1.sq" || return
786 used=
787 while read -r pick sha1 message
789 case " $used" in
790 *" $sha1 "*) continue ;;
791 esac
792 printf '%s\n' "$pick $sha1 $message"
793 test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
794 used="$used$sha1 "
795 while read -r squash action msg_prefix msg_content
797 case " $used" in
798 *" $squash "*) continue ;;
799 esac
800 emit=0
801 case "$action" in
803 action="${action#+}"
804 # full sha1 prefix test
805 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
807 # message prefix test
808 case "$message" in "$msg_content"*) emit=1;; esac ;;
809 esac
810 if test $emit = 1; then
811 if test -n "${format}"
812 then
813 msg_content=$(git log -n 1 --format="${format}" ${squash})
814 else
815 msg_content="$(echo "$msg_prefix" | sed "s/,/! /g")$msg_content"
817 printf '%s\n' "$action $squash $msg_content"
818 used="$used$squash "
820 done <"$1.sq"
821 done >"$1.rearranged" <"$1"
822 cat "$1.rearranged" >"$1"
823 rm -f "$1.sq" "$1.rearranged"
826 # Add commands after a pick or after a squash/fixup serie
827 # in the todo list.
828 add_exec_commands () {
830 first=t
831 while read -r insn rest
833 case $insn in
834 pick)
835 test -n "$first" ||
836 printf "%s" "$cmd"
838 esac
839 printf "%s %s\n" "$insn" "$rest"
840 first=
841 done
842 printf "%s" "$cmd"
843 } <"$1" >"$1.new" &&
844 mv "$1.new" "$1"
847 # The whole contents of this file is run by dot-sourcing it from
848 # inside a shell function. It used to be that "return"s we see
849 # below were not inside any function, and expected to return
850 # to the function that dot-sourced us.
852 # However, FreeBSD /bin/sh misbehaves on such a construct and
853 # continues to run the statements that follow such a "return".
854 # As a work-around, we introduce an extra layer of a function
855 # here, and immediately call it after defining it.
856 git_rebase__interactive () {
858 case "$action" in
859 continue)
860 # do we have anything to commit?
861 if git diff-index --cached --quiet HEAD --
862 then
863 # Nothing to commit -- skip this commit
865 test ! -f "$GIT_DIR"/CHERRY_PICK_HEAD ||
866 rm "$GIT_DIR"/CHERRY_PICK_HEAD ||
867 die "Could not remove CHERRY_PICK_HEAD"
868 else
869 if ! test -f "$author_script"
870 then
871 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
872 die "You have staged changes in your working tree. If these changes are meant to be
873 squashed into the previous commit, run:
875 git commit --amend $gpg_sign_opt_quoted
877 If they are meant to go into a new commit, run:
879 git commit $gpg_sign_opt_quoted
881 In both case, once you're done, continue with:
883 git rebase --continue
886 . "$author_script" ||
887 die "Error trying to find the author identity to amend commit"
888 if test -f "$amend"
889 then
890 current_head=$(git rev-parse --verify HEAD)
891 test "$current_head" = $(cat "$amend") ||
892 die "\
893 You have uncommitted changes in your working tree. Please, commit them
894 first and then run 'git rebase --continue' again."
895 do_with_author git commit --amend --no-verify -F "$msg" -e \
896 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
897 die "Could not commit staged changes."
898 else
899 do_with_author git commit --no-verify -F "$msg" -e \
900 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
901 die "Could not commit staged changes."
905 if test -r "$state_dir"/stopped-sha
906 then
907 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
910 require_clean_work_tree "rebase"
911 do_rest
912 return 0
914 skip)
915 git rerere clear
917 do_rest
918 return 0
920 edit-todo)
921 git stripspace --strip-comments <"$todo" >"$todo".new
922 mv -f "$todo".new "$todo"
923 collapse_todo_ids
924 append_todo_help
925 git stripspace --comment-lines >>"$todo" <<\EOF
927 You are editing the todo file of an ongoing interactive rebase.
928 To continue rebase after editing, run:
929 git rebase --continue
933 git_sequence_editor "$todo" ||
934 die "Could not execute editor"
935 expand_todo_ids
937 exit
939 esac
941 git var GIT_COMMITTER_IDENT >/dev/null ||
942 die "You need to set your committer info first"
944 comment_for_reflog start
946 if test ! -z "$switch_to"
947 then
948 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
949 output git checkout "$switch_to" -- ||
950 die "Could not checkout $switch_to"
952 comment_for_reflog start
955 orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?"
956 mkdir -p "$state_dir" || die "Could not create temporary $state_dir"
958 : > "$state_dir"/interactive || die "Could not mark as interactive"
959 write_basic_state
960 if test t = "$preserve_merges"
961 then
962 if test -z "$rebase_root"
963 then
964 mkdir "$rewritten" &&
965 for c in $(git merge-base --all $orig_head $upstream)
967 echo $onto > "$rewritten"/$c ||
968 die "Could not init rewritten commits"
969 done
970 else
971 mkdir "$rewritten" &&
972 echo $onto > "$rewritten"/root ||
973 die "Could not init rewritten commits"
975 # No cherry-pick because our first pass is to determine
976 # parents to rewrite and skipping dropped commits would
977 # prematurely end our probe
978 merges_option=
979 else
980 merges_option="--no-merges --cherry-pick"
983 shorthead=$(git rev-parse --short $orig_head)
984 shortonto=$(git rev-parse --short $onto)
985 if test -z "$rebase_root"
986 # this is now equivalent to ! -z "$upstream"
987 then
988 shortupstream=$(git rev-parse --short $upstream)
989 revisions=$upstream...$orig_head
990 shortrevisions=$shortupstream..$shorthead
991 else
992 revisions=$onto...$orig_head
993 shortrevisions=$shorthead
995 format=$(git config --get rebase.instructionFormat)
996 # the 'rev-list .. | sed' requires %m to parse; the instruction requires %H to parse
997 git rev-list $merges_option --format="%m%H ${format:-%s}" \
998 --reverse --left-right --topo-order \
999 $revisions ${restrict_revision+^$restrict_revision} | \
1000 sed -n "s/^>//p" |
1001 while read -r sha1 rest
1004 if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
1005 then
1006 comment_out="$comment_char "
1007 else
1008 comment_out=
1011 if test t != "$preserve_merges"
1012 then
1013 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
1014 else
1015 if test -z "$rebase_root"
1016 then
1017 preserve=t
1018 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
1020 if test -f "$rewritten"/$p
1021 then
1022 preserve=f
1024 done
1025 else
1026 preserve=f
1028 if test f = "$preserve"
1029 then
1030 touch "$rewritten"/$sha1
1031 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
1034 done
1036 # Watch for commits that been dropped by --cherry-pick
1037 if test t = "$preserve_merges"
1038 then
1039 mkdir "$dropped"
1040 # Save all non-cherry-picked changes
1041 git rev-list $revisions --left-right --cherry-pick | \
1042 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
1043 # Now all commits and note which ones are missing in
1044 # not-cherry-picks and hence being dropped
1045 git rev-list $revisions |
1046 while read rev
1048 if test -f "$rewritten"/$rev && test "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
1049 then
1050 # Use -f2 because if rev-list is telling us this commit is
1051 # not worthwhile, we don't want to track its multiple heads,
1052 # just the history of its first-parent for others that will
1053 # be rebasing on top of it
1054 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
1055 sha1=$(git rev-list -1 $rev)
1056 sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
1057 rm "$rewritten"/$rev
1059 done
1062 test -s "$todo" || echo noop >> "$todo"
1063 test -n "$autosquash" && rearrange_squash "$todo"
1064 test -n "$cmd" && add_exec_commands "$todo"
1066 todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
1067 todocount=${todocount##* }
1069 cat >>"$todo" <<EOF
1071 $comment_char Rebase $shortrevisions onto $shortonto ($todocount command(s))
1073 append_todo_help
1074 git stripspace --comment-lines >>"$todo" <<\EOF
1076 However, if you remove everything, the rebase will be aborted.
1080 if test -z "$keep_empty"
1081 then
1082 printf '%s\n' "$comment_char Note that empty commits are commented out" >>"$todo"
1086 has_action "$todo" ||
1087 return 2
1089 cp "$todo" "$todo".backup
1090 collapse_todo_ids
1091 git_sequence_editor "$todo" ||
1092 die_abort "Could not execute editor"
1094 has_action "$todo" ||
1095 return 2
1097 expand_todo_ids
1099 test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
1101 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
1102 output git checkout $onto || die_abort "could not detach HEAD"
1103 git update-ref ORIG_HEAD $orig_head
1104 do_rest
1107 # ... and then we call the whole thing.
1108 git_rebase__interactive