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