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