1#!/bin/sh 2# 3# Copyright (c) 2005 Junio C Hamano. 4# 5 6USAGE='[--onto <newbase>] <upstream> [<branch>]' 7LONG_USAGE='git-rebase replaces <branch> with a new branch of the 8same name. When the --onto option is provided the new branch starts 9out with a HEAD equal to <newbase>, otherwise it is equal to <upstream> 10It then attempts to create a new commit for each commit from the original 11<branch> that does not exist in the <upstream> branch. 12 13It is possible that a merge failure will prevent this process from being 14completely automatic. You will have to resolve any such merge failure 15and run git rebase --continue. Another option is to bypass the commit 16that caused the merge failure with git rebase --skip. To restore the 17original <branch> and remove the .dotest working files, use the command 18git rebase --abort instead. 19 20Note that if <branch> is not specified on the command line, the 21currently checked out branch is used. You must be in the top 22directory of your project to start (or continue) a rebase. 23 24Example: git-rebase master~1 topic 25 26 A---B---C topic A'\''--B'\''--C'\'' topic 27 / --> / 28 D---E---F---G master D---E---F---G master 29' 30. git-sh-setup 31 32RESOLVEMSG=" 33When you have resolved this problem run\"git rebase --continue\". 34If you would prefer to skip this patch, instead run\"git rebase --skip\". 35To restore the original branch and stop rebasing run\"git rebase --abort\". 36" 37unset newbase 38while case"$#"in0)break;;esac 39do 40case"$1"in 41--continue) 42diff=$(git-diff-files) 43case"$diff"in 44 ?*)echo"You must edit all merge conflicts and then" 45echo"mark them as resolved using git update-index" 46exit1 47;; 48esac 49 git am --resolved --3way --resolvemsg="$RESOLVEMSG" 50exit 51;; 52--skip) 53 git am -3 --skip --resolvemsg="$RESOLVEMSG" 54exit 55;; 56--abort) 57[-d .dotest ] || die "No rebase in progress?" 58 git reset--hard ORIG_HEAD 59rm-r .dotest 60exit 61;; 62--onto) 63test2-le"$#"|| usage 64 newbase="$2" 65shift 66;; 67-*) 68 usage 69;; 70*) 71break 72;; 73esac 74shift 75done 76 77# Make sure we do not have .dotest 78ifmkdir .dotest 79then 80rmdir .dotest 81else 82echo>&2' 83It seems that I cannot create a .dotest directory, and I wonder if you 84are in the middle of patch application or another rebase. If that is not 85the case, please rm -fr .dotest and run me again. I am stopping in case 86you still have something valuable there.' 87exit1 88fi 89 90# The tree must be really really clean. 91git-update-index --refresh||exit 92diff=$(git-diff-index --cached --name-status -r HEAD) 93case"$diff"in 94?*)echo"$diff" 95exit1 96;; 97esac 98 99# The upstream head must be given. Make sure it is valid. 100upstream_name="$1" 101upstream=`git rev-parse --verify "${upstream_name}^0"`|| 102 die "invalid upstream$upstream_name" 103 104# If a hook exists, give it a chance to interrupt 105iftest -x"$GIT_DIR/hooks/pre-rebase" 106then 107"$GIT_DIR/hooks/pre-rebase"${1+"$@"}|| { 108echo>&2"The pre-rebase hook refused to rebase." 109exit1 110} 111fi 112 113# If the branch to rebase is given, first switch to it. 114case"$#"in 1152) 116 branch_name="$2" 117 git-checkout"$2"|| usage 118;; 119*) 120 branch_name=`git symbolic-ref HEAD`|| die "No current branch" 121 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'` 122;; 123esac 124branch=$(git-rev-parse --verify "${branch_name}^0")||exit 125 126# Make sure the branch to rebase onto is valid. 127onto_name=${newbase-"$upstream_name"} 128onto=$(git-rev-parse --verify "${onto_name}^0")||exit 129 130# Now we are rebasing commits $upstream..$branch on top of $onto 131 132# Check if we are already based on $onto, but this should be 133# done only when upstream and onto are the same. 134iftest"$upstream"="$onto" 135then 136 mb=$(git-merge-base "$onto" "$branch") 137iftest"$mb"="$onto" 138then 139echo>&2"Current branch$branch_nameis up to date." 140exit0 141fi 142fi 143 144# Rewind the head to "$onto"; this saves our current head in ORIG_HEAD. 145git-reset --hard"$onto" 146 147# If the $onto is a proper descendant of the tip of the branch, then 148# we just fast forwarded. 149iftest"$mb"="$onto" 150then 151echo>&2"Fast-forwarded$branchto$newbase." 152exit0 153fi 154 155git-format-patch -k --stdout --full-index"$upstream"..ORIG_HEAD | 156git am --binary -3 -k --resolvemsg="$RESOLVEMSG" 157