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. 4# 5# Copyright (c) 2006 Johannes E. Schindelin 6# 7# The original idea comes from Eric W. Biederman, in 8# http://article.gmane.org/gmane.comp.version-control.git/22407 9# 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. 14todo="$state_dir"/git-rebase-todo 15 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. 19done="$state_dir"/done 20 21# The commit message that is planned to be used for any changes that 22# need to be committed following a user interaction. 23msg="$state_dir"/message 24 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. 31# 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. 38squash_msg="$state_dir"/message-squash 39 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.) 45fixup_msg="$state_dir"/message-fixup 46 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.) 53rewritten="$state_dir"/rewritten 54 55dropped="$state_dir"/dropped 56 57end="$state_dir"/end 58msgnum="$state_dir"/msgnum 59 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. 63author_script="$state_dir"/author-script 64 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. 71amend="$state_dir"/amend 72 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. 77rewritten_list="$state_dir"/rewritten-list 78rewritten_pending="$state_dir"/rewritten-pending 79 80strategy_args= 81iftest -n"$do_merge" 82then 83 strategy_args=${strategy:+--strategy=$strategy} 84eval' 85 for strategy_opt in '"$strategy_opts"' 86 do 87 strategy_args="$strategy_args-X$(git rev-parse --sq-quote "${strategy_opt#--}")" 88 done 89 ' 90fi 91 92GIT_CHERRY_PICK_HELP="$resolvemsg" 93export GIT_CHERRY_PICK_HELP 94 95comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1) 96:${comment_char:=#} 97 98warn () { 99printf'%s\n'"$*">&2 100} 101 102# Output the commit message for the specified commit. 103commit_message () { 104 git cat-file commit "$1"|sed"1,/^$/d" 105} 106 107orig_reflog_action="$GIT_REFLOG_ACTION" 108 109comment_for_reflog () { 110case"$orig_reflog_action"in 111''|rebase*) 112 GIT_REFLOG_ACTION="rebase -i ($1)" 113export GIT_REFLOG_ACTION 114;; 115esac 116} 117 118last_count= 119mark_action_done () { 120sed-e1q <"$todo">>"$done" 121sed-e1d <"$todo">>"$todo".new 122mv-f"$todo".new "$todo" 123 new_count=$(git stripspace --strip-comments <"$done" | wc -l) 124echo$new_count>"$msgnum" 125 total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l))) 126echo$total>"$end" 127iftest"$last_count"!="$new_count" 128then 129 last_count=$new_count 130printf"Rebasing (%d/%d)\r"$new_count $total 131test -z"$verbose"||echo 132fi 133} 134 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. 138reschedule_last_action () { 139tail-n1"$done"|cat-"$todo">"$todo".new 140sed-e \$d<"$done">"$done".new 141mv-f"$todo".new "$todo" 142mv-f"$done".new "$done" 143} 144 145append_todo_help () { 146 git stripspace --comment-lines>>"$todo"<<\EOF 147 148Commands: 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 155 d, drop = remove commit 156 157These lines can be re-ordered; they are executed from top to bottom. 158 159If you remove a line here THAT COMMIT WILL BE LOST. 160EOF 161} 162 163make_patch () { 164 sha1_and_parents="$(git rev-list --parents -1 "$1")" 165 case "$sha1_and_parents" in 166 ?*''?*''?*) 167 git diff --cc$sha1_and_parents 168 ;; 169 ?*''?*) 170 git diff-tree -p "$1^!" 171 ;; 172 *) 173 echo "Root commit" 174 ;; 175 esac > "$state_dir"/patch 176 test -f "$msg" || 177 commit_message "$1" > "$msg" 178 test -f "$author_script" || 179 get_author_ident_from_commit "$1" > "$author_script" 180} 181 182die_with_patch () { 183 echo "$1" > "$state_dir"/stopped-sha 184 make_patch "$1" 185 git rerere 186 die "$2" 187} 188 189exit_with_patch () { 190 echo "$1" > "$state_dir"/stopped-sha 191 make_patch$1 192 git rev-parse --verify HEAD > "$amend" 193 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} 194 warn "You can amend the commit now, with" 195 warn 196 warn " git commit --amend$gpg_sign_opt_quoted" 197 warn 198 warn "Once you are satisfied with your changes, run" 199 warn 200 warn " git rebase --continue" 201 warn 202 exit$2 203} 204 205die_abort () { 206 rm -rf "$state_dir" 207 die "$1" 208} 209 210has_action () { 211 test -n "$(git stripspace --strip-comments <"$1")" 212} 213 214is_empty_commit() { 215 tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null || 216 die "$1: not a commit that can be picked") 217 ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null || 218 ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904) 219 test "$tree" = "$ptree" 220} 221 222is_merge_commit() 223{ 224 git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1 225} 226 227# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and 228# GIT_AUTHOR_DATE exported from the current environment. 229do_with_author () { 230 ( 231 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE 232 "$@" 233 ) 234} 235 236git_sequence_editor () { 237 if test -z "$GIT_SEQUENCE_EDITOR" 238 then 239 GIT_SEQUENCE_EDITOR="$(git config sequence.editor)" 240 if [ -z "$GIT_SEQUENCE_EDITOR" ] 241 then 242 GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $? 243 fi 244 fi 245 246 eval "$GIT_SEQUENCE_EDITOR" '"$@"' 247} 248 249pick_one () { 250 ff=--ff 251 252 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1;; esac 253 case "$force_rebase" in '') ;; ?*) ff= ;; esac 254 output git rev-parse --verify$sha1|| die "Invalid commit name:$sha1" 255 256 if is_empty_commit "$sha1" 257 then 258 empty_args="--allow-empty" 259 fi 260 261 test -d "$rewritten" && 262 pick_one_preserving_merges "$@" && return 263 output eval git cherry-pick \ 264${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}\ 265 "$strategy_args"$empty_args$ff"$@" 266 267 # If cherry-pick dies it leaves the to-be-picked commit unrecorded. Reschedule 268 # previous task so this commit is not lost. 269 ret=$? 270 case "$ret" in [01]) ;; *) reschedule_last_action ;; esac 271 return$ret 272} 273 274pick_one_preserving_merges () { 275 fast_forward=t 276 case "$1" in 277 -n) 278 fast_forward=f 279 sha1=$2 280 ;; 281 *) 282 sha1=$1 283 ;; 284 esac 285 sha1=$(git rev-parse $sha1) 286 287 if test -f "$state_dir"/current-commit 288 then 289 if test "$fast_forward" = t 290 then 291 while read current_commit 292 do 293 git rev-parse HEAD > "$rewritten"/$current_commit 294 done <"$state_dir"/current-commit 295 rm "$state_dir"/current-commit || 296 die "Cannot write current commit's replacement sha1" 297 fi 298 fi 299 300 echo$sha1>> "$state_dir"/current-commit 301 302 # rewrite parents; if none were rewritten, we can fast-forward. 303 new_parents= 304 pend="$(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)" 305 if test "$pend" = "" 306 then 307 pend=" root" 308 fi 309 while [ "$pend" != "" ] 310 do 311 p=$(expr "$pend" : ' \([^ ]*\)') 312 pend="${pend# $p}" 313 314 if test -f "$rewritten"/$p 315 then 316 new_p=$(cat "$rewritten"/$p) 317 318 # If the todo reordered commits, and our parent is marked for 319 # rewriting, but hasn't been gotten to yet, assume the user meant to 320 # drop it on top of the current HEAD 321 if test -z "$new_p" 322 then 323 new_p=$(git rev-parse HEAD) 324 fi 325 326 test$p!=$new_p&& fast_forward=f 327 case "$new_parents" in 328 *$new_p*) 329 ;; # do nothing; that parent is already there 330 *) 331 new_parents="$new_parents $new_p" 332 ;; 333 esac 334 else 335 if test -f "$dropped"/$p 336 then 337 fast_forward=f 338 replacement="$(cat "$dropped"/$p)" 339 test -z "$replacement" && replacement=root 340 pend="$replacement$pend" 341 else 342 new_parents="$new_parents $p" 343 fi 344 fi 345 done 346 case$fast_forwardin 347 t) 348 output warn "Fast-forward to $sha1" 349 output git reset --hard$sha1|| 350 die "Cannot fast-forward to $sha1" 351 ;; 352 f) 353 first_parent=$(expr "$new_parents" : ' \([^ ]*\)') 354 355 if [ "$1" != "-n" ] 356 then 357 # detach HEAD to current parent 358 output git checkout$first_parent2> /dev/null || 359 die "Cannot move HEAD to $first_parent" 360 fi 361 362 case "$new_parents" in 363 ' '*' '*) 364 test "a$1" = a-n && die "Refusing to squash a merge:$sha1" 365 366 # redo merge 367 author_script_content=$(get_author_ident_from_commit $sha1) 368 eval "$author_script_content" 369 msg_content="$(commit_message $sha1)" 370 # No point in merging the first parent, that's HEAD 371 new_parents=${new_parents# $first_parent} 372 merge_args="--no-log --no-ff" 373 if ! do_with_author output eval \ 374 'git merge${gpg_sign_opt:+"$gpg_sign_opt"}\ 375$merge_args$strategy_args-m "$msg_content"$new_parents' 376 then 377 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG 378 die_with_patch$sha1"Error redoing merge $sha1" 379 fi 380 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list" 381 ;; 382 *) 383 output eval git cherry-pick \ 384${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}\ 385 "$strategy_args" "$@" || 386 die_with_patch$sha1"Could not pick $sha1" 387 ;; 388 esac 389 ;; 390 esac 391} 392 393nth_string () { 394 case "$1" in 395 *1[0-9]|*[04-9]) echo "$1"th;; 396 *1) echo "$1"st;; 397 *2) echo "$1"nd;; 398 *3) echo "$1"rd;; 399 esac 400} 401 402update_squash_messages () { 403 if test -f "$squash_msg"; then 404 mv "$squash_msg" "$squash_msg".bak || exit 405 count=$(($(sed -n \ 406 -e "1s/^. This is a combination of \(.*\) commits\./\1/p" \ 407 -e "q" < "$squash_msg".bak)+1)) 408 { 409 printf '%s\n' "$comment_char This is a combination of $count commits." 410 sed -e 1d -e '2,/^./{ 411 /^$/d 412 }' <"$squash_msg".bak 413 } >"$squash_msg" 414 else 415 commit_message HEAD > "$fixup_msg" || die "Cannot write$fixup_msg" 416 count=2 417 { 418 printf '%s\n' "$comment_char This is a combination of 2 commits." 419 printf '%s\n' "$comment_char The first commit's message is:" 420 echo 421 cat "$fixup_msg" 422 } >"$squash_msg" 423 fi 424 case$1in 425 squash) 426 rm -f "$fixup_msg" 427 echo 428 printf '%s\n' "$comment_charThis is the$(nth_string $count)commit message:" 429 echo 430 commit_message$2 431 ;; 432 fixup) 433 echo 434 printf '%s\n' "$comment_charThe$(nth_string $count)commit message will be skipped:" 435 echo 436 # Change the space after the comment character to TAB: 437 commit_message$2| git stripspace --comment-lines | sed -e 's/ / /' 438 ;; 439 esac >>"$squash_msg" 440} 441 442peek_next_command () { 443 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q 444} 445 446# A squash/fixup has failed. Prepare the long version of the squash 447# commit message, then die_with_patch. This code path requires the 448# user to edit the combined commit message for all commits that have 449# been squashed/fixedup so far. So also erase the old squash 450# messages, effectively causing the combined commit to be used as the 451# new basis for any further squash/fixups. Args: sha1 rest 452die_failed_squash() { 453 mv "$squash_msg" "$msg" || exit 454 rm -f "$fixup_msg" 455 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit 456 warn 457 warn "Could not apply$1...$2" 458 die_with_patch$1"" 459} 460 461flush_rewritten_pending() { 462 test -s "$rewritten_pending" || return 463 newsha1="$(git rev-parse HEAD^0)" 464 sed "s/$/$newsha1/" < "$rewritten_pending" >> "$rewritten_list" 465 rm -f "$rewritten_pending" 466} 467 468record_in_rewritten() { 469 oldsha1="$(git rev-parse $1)" 470 echo "$oldsha1" >> "$rewritten_pending" 471 472 case "$(peek_next_command)" in 473 squash|s|fixup|f) 474 ;; 475 *) 476 flush_rewritten_pending 477 ;; 478 esac 479} 480 481do_pick () { 482 if test "$(git rev-parse HEAD)" = "$squash_onto" 483 then 484 # Set the correct commit message and author info on the 485 # sentinel root before cherry-picking the original changes 486 # without committing (-n). Finally, update the sentinel again 487 # to include these changes. If the cherry-pick results in a 488 # conflict, this means our behaviour is similar to a standard 489 # failed cherry-pick during rebase, with a dirty index to 490 # resolve before manually running git commit --amend then git 491 # rebase --continue. 492 git commit --allow-empty --allow-empty-message --amend \ 493 --no-post-rewrite -n -q -C$1&& 494 pick_one -n$1&& 495 git commit --allow-empty --allow-empty-message \ 496 --amend --no-post-rewrite -n -q -C$1\ 497${gpg_sign_opt:+"$gpg_sign_opt"}|| 498 die_with_patch$1"Could not apply$1...$2" 499 else 500 pick_one$1|| 501 die_with_patch$1"Could not apply$1...$2" 502 fi 503} 504 505do_next () { 506 rm -f "$msg" "$author_script" "$amend" || exit 507 read -r command sha1 rest < "$todo" 508 case "$command" in 509 "$comment_char"*|''|noop|drop|d) 510 mark_action_done 511 ;; 512 pick|p) 513 comment_for_reflog pick 514 515 mark_action_done 516 do_pick$sha1"$rest" 517 record_in_rewritten$sha1 518 ;; 519 reword|r) 520 comment_for_reflog reword 521 522 mark_action_done 523 do_pick$sha1"$rest" 524 git commit --amend --no-post-rewrite${gpg_sign_opt:+"$gpg_sign_opt"}|| { 525 warn "Could not amend commit after successfully picking$sha1...$rest" 526 warn "This is most likely due to an empty commit message, or the pre-commit hook" 527 warn "failed. If the pre-commit hook failed, you may need to resolve the issue before" 528 warn "you are able to reword the commit." 529 exit_with_patch$sha11 530 } 531 record_in_rewritten$sha1 532 ;; 533 edit|e) 534 comment_for_reflog edit 535 536 mark_action_done 537 do_pick$sha1"$rest" 538 warn "Stopped at$sha1...$rest" 539 exit_with_patch$sha10 540 ;; 541 squash|s|fixup|f) 542 case "$command" in 543 squash|s) 544 squash_style=squash 545 ;; 546 fixup|f) 547 squash_style=fixup 548 ;; 549 esac 550 comment_for_reflog$squash_style 551 552 test -f "$done" && has_action "$done" || 553 die "Cannot '$squash_style' without a previous commit" 554 555 mark_action_done 556 update_squash_messages$squash_style$sha1 557 author_script_content=$(get_author_ident_from_commit HEAD) 558 echo "$author_script_content" > "$author_script" 559 eval "$author_script_content" 560 if ! pick_one -n$sha1 561 then 562 git rev-parse --verify HEAD >"$amend" 563 die_failed_squash$sha1"$rest" 564 fi 565 case "$(peek_next_command)" in 566 squash|s|fixup|f) 567 # This is an intermediate commit; its message will only be 568 # used in case of trouble. So use the long version: 569 do_with_author output git commit --amend --no-verify -F "$squash_msg" \ 570${gpg_sign_opt:+"$gpg_sign_opt"}|| 571 die_failed_squash$sha1"$rest" 572 ;; 573 *) 574 # This is the final command of this squash/fixup group 575 if test -f "$fixup_msg" 576 then 577 do_with_author git commit --amend --no-verify -F "$fixup_msg" \ 578${gpg_sign_opt:+"$gpg_sign_opt"}|| 579 die_failed_squash$sha1"$rest" 580 else 581 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit 582 rm -f "$GIT_DIR"/MERGE_MSG 583 do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \ 584${gpg_sign_opt:+"$gpg_sign_opt"}|| 585 die_failed_squash$sha1"$rest" 586 fi 587 rm -f "$squash_msg" "$fixup_msg" 588 ;; 589 esac 590 record_in_rewritten$sha1 591 ;; 592 x|"exec") 593 read -r command rest < "$todo" 594 mark_action_done 595 printf 'Executing: %s\n' "$rest" 596 # "exec" command doesn't take a sha1 in the todo-list. 597# => can't just use $sha1 here. 598 git rev-parse --verify HEAD >"$state_dir"/stopped-sha 599${SHELL:-@SHELL_PATH@}-c"$rest"# Actual execution 600 status=$? 601# Run in subshell because require_clean_work_tree can die. 602 dirty=f 603(require_clean_work_tree "rebase"2>/dev/null) || dirty=t 604iftest"$status"-ne0 605then 606 warn "Execution failed:$rest" 607test"$dirty"= f || 608 warn "and made changes to the index and/or the working tree" 609 610 warn "You can fix the problem, and then run" 611 warn 612 warn " git rebase --continue" 613 warn 614iftest$status-eq127# command not found 615then 616 status=1 617fi 618exit"$status" 619eliftest"$dirty"= t 620then 621 warn "Execution succeeded:$rest" 622 warn "but left changes to the index and/or the working tree" 623 warn "Commit or stash your changes, and then run" 624 warn 625 warn " git rebase --continue" 626 warn 627exit1 628fi 629;; 630*) 631 warn "Unknown command:$command$sha1$rest" 632 fixtodo="Please fix this using 'git rebase --edit-todo'." 633if git rev-parse --verify -q"$sha1">/dev/null 634then 635 die_with_patch $sha1"$fixtodo" 636else 637 die "$fixtodo" 638fi 639;; 640esac 641test -s"$todo"&&return 642 643 comment_for_reflog finish && 644 newhead=$(git rev-parse HEAD)&& 645case$head_namein 646 refs/*) 647 message="$GIT_REFLOG_ACTION:$head_nameonto$onto"&& 648 git update-ref -m"$message"$head_name $newhead $orig_head&& 649 git symbolic-ref \ 650-m"$GIT_REFLOG_ACTION: returning to$head_name" \ 651 HEAD $head_name 652;; 653esac&& { 654test!-f"$state_dir"/verbose || 655 git diff-tree --stat$orig_head..HEAD 656} && 657{ 658test -s"$rewritten_list"&& 659 git notes copy --for-rewrite=rebase <"$rewritten_list"|| 660 true # we don't care if this copying failed 661} && 662 hook="$(git rev-parse --git-path hooks/post-rewrite)" 663iftest -x"$hook"&&test -s"$rewritten_list";then 664"$hook" rebase <"$rewritten_list" 665 true # we don't care if this hook failed 666fi&& 667 warn "Successfully rebased and updated$head_name." 668 669return1# not failure; just to break the do_rest loop 670} 671 672# can only return 0, when the infinite loop breaks 673do_rest () { 674while: 675do 676 do_next ||break 677done 678} 679 680# skip picking commits whose parents are unchanged 681skip_unnecessary_picks () { 682 fd=3 683whileread -r command rest 684do 685# fd=3 means we skip the command 686case"$fd,$command"in 6873,pick|3,p) 688# pick a commit whose parent is current $onto -> skip 689 sha1=${rest%% *} 690case"$(git rev-parse --verify --quiet "$sha1"^)"in 691"$onto"*) 692 onto=$sha1 693;; 694*) 695 fd=1 696;; 697esac 698;; 6993,"$comment_char"*|3,) 700# copy comments 701;; 702*) 703 fd=1 704;; 705esac 706printf'%s\n'"$command${rest:+ }$rest">&$fd 707done<"$todo">"$todo.new"3>>"$done"&& 708mv-f"$todo".new "$todo"&& 709case"$(peek_next_command)"in 710 squash|s|fixup|f) 711 record_in_rewritten "$onto" 712;; 713esac|| 714 die "Could not skip unnecessary pick commands" 715} 716 717transform_todo_ids () { 718whileread -r command rest 719do 720case"$command"in 721"$comment_char"* |exec) 722# Be careful for oddball commands like 'exec' 723# that do not have a SHA-1 at the beginning of $rest. 724;; 725*) 726 sha1=$(git rev-parse --verify --quiet "$@" ${rest%% *})&& 727 rest="$sha1${rest#* }" 728;; 729esac 730printf'%s\n'"$command${rest:+ }$rest" 731done<"$todo">"$todo.new"&& 732mv-f"$todo.new""$todo" 733} 734 735expand_todo_ids() { 736 transform_todo_ids 737} 738 739collapse_todo_ids() { 740 transform_todo_ids --short 741} 742 743# Rearrange the todo list that has both "pick sha1 msg" and 744# "pick sha1 fixup!/squash! msg" appears in it so that the latter 745# comes immediately after the former, and change "pick" to 746# "fixup"/"squash". 747rearrange_squash () { 748# extract fixup!/squash! lines and resolve any referenced sha1's 749whileread -r pick sha1 message 750do 751case"$message"in 752"squash! "*|"fixup! "*) 753 action="${message%%!*}" 754 rest=$message 755 prefix= 756# skip all squash! or fixup! (but save for later) 757while: 758do 759case"$rest"in 760"squash! "*|"fixup! "*) 761 prefix="$prefix${rest%%!*}," 762 rest="${rest#*! }" 763;; 764*) 765break 766;; 767esac 768done 769printf'%s %s %s %s\n'"$sha1""$action""$prefix""$rest" 770# if it's a single word, try to resolve to a full sha1 and 771# emit a second copy. This allows us to match on both message 772# and on sha1 prefix 773iftest"${rest#* }"="$rest";then 774 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)" 775iftest -n"$fullsha";then 776# prefix the action to uniquely identify this line as 777# intended for full sha1 match 778echo"$sha1+$action$prefix$fullsha" 779fi 780fi 781esac 782done>"$1.sq"<"$1" 783test -s"$1.sq"||return 784 785 used= 786whileread -r pick sha1 message 787do 788case"$used"in 789*"$sha1"*)continue;; 790esac 791printf'%s\n'"$pick$sha1$message" 792 used="$used$sha1" 793whileread -r squash action msg_prefix msg_content 794do 795case"$used"in 796*"$squash"*)continue;; 797esac 798 emit=0 799case"$action"in 800+*) 801 action="${action#+}" 802# full sha1 prefix test 803case"$msg_content"in"$sha1"*) emit=1;;esac;; 804*) 805# message prefix test 806case"$message"in"$msg_content"*) emit=1;;esac;; 807esac 808iftest$emit=1;then 809 real_prefix=$(echo "$msg_prefix" | sed "s/,/! /g") 810printf'%s\n'"$action$squash${real_prefix}$msg_content" 811 used="$used$squash" 812fi 813done<"$1.sq" 814done>"$1.rearranged"<"$1" 815cat"$1.rearranged">"$1" 816rm-f"$1.sq""$1.rearranged" 817} 818 819# Add commands after a pick or after a squash/fixup serie 820# in the todo list. 821add_exec_commands () { 822{ 823 first=t 824whileread -r insn rest 825do 826case$insnin 827 pick) 828test -n"$first"|| 829printf"%s""$cmd" 830;; 831esac 832printf"%s %s\n""$insn""$rest" 833 first= 834done 835printf"%s""$cmd" 836} <"$1">"$1.new"&& 837mv"$1.new""$1" 838} 839 840# The whole contents of this file is run by dot-sourcing it from 841# inside a shell function. It used to be that "return"s we see 842# below were not inside any function, and expected to return 843# to the function that dot-sourced us. 844# 845# However, FreeBSD /bin/sh misbehaves on such a construct and 846# continues to run the statements that follow such a "return". 847# As a work-around, we introduce an extra layer of a function 848# here, and immediately call it after defining it. 849git_rebase__interactive () { 850 851case"$action"in 852continue) 853# do we have anything to commit? 854if git diff-index --cached --quiet HEAD -- 855then 856: Nothing to commit -- skip this 857else 858if!test -f"$author_script" 859then 860 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} 861 die "You have staged changes in your working tree. If these changes are meant to be 862squashed into the previous commit, run: 863 864 git commit --amend$gpg_sign_opt_quoted 865 866If they are meant to go into a new commit, run: 867 868 git commit$gpg_sign_opt_quoted 869 870In both case, once you're done, continue with: 871 872 git rebase --continue 873" 874fi 875 . "$author_script"|| 876 die "Error trying to find the author identity to amend commit" 877iftest -f"$amend" 878then 879 current_head=$(git rev-parse --verify HEAD) 880test"$current_head"=$(cat "$amend")|| 881 die "\ 882You have uncommitted changes in your working tree. Please, commit them 883first and then run 'git rebase --continue' again." 884 do_with_author git commit --amend --no-verify -F"$msg"-e \ 885${gpg_sign_opt:+"$gpg_sign_opt"}|| 886 die "Could not commit staged changes." 887else 888 do_with_author git commit --no-verify -F"$msg"-e \ 889${gpg_sign_opt:+"$gpg_sign_opt"}|| 890 die "Could not commit staged changes." 891fi 892fi 893 894 record_in_rewritten "$(cat "$state_dir"/stopped-sha)" 895 896 require_clean_work_tree "rebase" 897 do_rest 898return0 899;; 900skip) 901 git rerere clear 902 903 do_rest 904return0 905;; 906edit-todo) 907 git stripspace --strip-comments<"$todo">"$todo".new 908mv-f"$todo".new "$todo" 909 collapse_todo_ids 910 append_todo_help 911 git stripspace --comment-lines>>"$todo"<<\EOF 912 913You are editing the todo file of an ongoing interactive rebase. 914To continue rebase after editing, run: 915 git rebase --continue 916 917EOF 918 919 git_sequence_editor "$todo"|| 920 die "Could not execute editor" 921 expand_todo_ids 922 923exit 924;; 925esac 926 927git var GIT_COMMITTER_IDENT >/dev/null || 928 die "You need to set your committer info first" 929 930comment_for_reflog start 931 932iftest!-z"$switch_to" 933then 934 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout$switch_to" 935 output git checkout "$switch_to"--|| 936 die "Could not checkout$switch_to" 937 938 comment_for_reflog start 939fi 940 941orig_head=$(git rev-parse --verify HEAD)|| die "No HEAD?" 942mkdir-p"$state_dir"|| die "Could not create temporary$state_dir" 943 944: >"$state_dir"/interactive || die "Could not mark as interactive" 945write_basic_state 946iftest t ="$preserve_merges" 947then 948iftest -z"$rebase_root" 949then 950mkdir"$rewritten"&& 951for c in$(git merge-base --all $orig_head $upstream) 952do 953echo$onto>"$rewritten"/$c|| 954 die "Could not init rewritten commits" 955done 956else 957mkdir"$rewritten"&& 958echo$onto>"$rewritten"/root || 959 die "Could not init rewritten commits" 960fi 961# No cherry-pick because our first pass is to determine 962# parents to rewrite and skipping dropped commits would 963# prematurely end our probe 964 merges_option= 965else 966 merges_option="--no-merges --cherry-pick" 967fi 968 969shorthead=$(git rev-parse --short $orig_head) 970shortonto=$(git rev-parse --short $onto) 971iftest -z"$rebase_root" 972# this is now equivalent to ! -z "$upstream" 973then 974 shortupstream=$(git rev-parse --short $upstream) 975 revisions=$upstream...$orig_head 976 shortrevisions=$shortupstream..$shorthead 977else 978 revisions=$onto...$orig_head 979 shortrevisions=$shorthead 980fi 981git rev-list$merges_option--pretty=oneline --reverse --left-right --topo-order \ 982$revisions ${restrict_revision+^$restrict_revision}| \ 983sed-n"s/^>//p"| 984whileread -r sha1 rest 985do 986 987iftest -z"$keep_empty"&& is_empty_commit $sha1&& ! is_merge_commit $sha1 988then 989 comment_out="$comment_char" 990else 991 comment_out= 992fi 993 994iftest t !="$preserve_merges" 995then 996printf'%s\n'"${comment_out}pick$sha1$rest">>"$todo" 997else 998iftest -z"$rebase_root" 999then1000 preserve=t1001for p in$(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)1002do1003iftest -f"$rewritten"/$p1004then1005 preserve=f1006fi1007done1008else1009 preserve=f1010fi1011iftest f ="$preserve"1012then1013touch"$rewritten"/$sha11014printf'%s\n'"${comment_out}pick$sha1$rest">>"$todo"1015fi1016fi1017done10181019# Watch for commits that been dropped by --cherry-pick1020iftest t ="$preserve_merges"1021then1022mkdir"$dropped"1023# Save all non-cherry-picked changes1024 git rev-list$revisions--left-right --cherry-pick| \1025sed-n"s/^>//p">"$state_dir"/not-cherry-picks1026# Now all commits and note which ones are missing in1027# not-cherry-picks and hence being dropped1028 git rev-list$revisions|1029whilereadrev1030do1031iftest -f"$rewritten"/$rev&&test"$(sane_grep "$rev" "$state_dir"/not-cherry-picks)"=""1032then1033# Use -f2 because if rev-list is telling us this commit is1034# not worthwhile, we don't want to track its multiple heads,1035# just the history of its first-parent for others that will1036# be rebasing on top of it1037 git rev-list --parents -1$rev| cut -d' '-s -f2>"$dropped"/$rev1038 sha1=$(git rev-list -1 $rev)1039 sane_grep -v"^[a-z][a-z]*$sha1"<"$todo">"${todo}2";mv"${todo}2""$todo"1040rm"$rewritten"/$rev1041fi1042done1043fi10441045test -s"$todo"||echo noop >>"$todo"1046test -n"$autosquash"&& rearrange_squash "$todo"1047test -n"$cmd"&& add_exec_commands "$todo"10481049todocount=$(git stripspace --strip-comments <"$todo" | wc -l)1050todocount=${todocount##* }10511052cat>>"$todo"<<EOF10531054$comment_charRebase$shortrevisionsonto$shortonto($todocountcommand(s))1055EOF1056append_todo_help1057git stripspace --comment-lines>>"$todo"<<\EOF10581059However,if you remove everything, the rebase will be aborted.10601061EOF10621063iftest -z"$keep_empty"1064then1065printf'%s\n'"$comment_charNote that empty commits are commented out">>"$todo"1066fi106710681069has_action "$todo"||1070return210711072cp"$todo""$todo".backup1073collapse_todo_ids1074git_sequence_editor "$todo"||1075 die_abort "Could not execute editor"10761077has_action "$todo"||1078return210791080expand_todo_ids10811082test -d"$rewritten"||test -n"$force_rebase"|| skip_unnecessary_picks10831084GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout$onto_name"1085output git checkout $onto|| die_abort "could not detach HEAD"1086git update-ref ORIG_HEAD $orig_head1087do_rest10881089}1090# ... and then we call the whole thing.1091git_rebase__interactive