3 # Copyright (c) 2005 Junio C Hamano.
6 USAGE='[--onto <newbase>] <upstream> [<branch>]'
7 LONG_USAGE='If <branch> is specified, switch to that branch first. Then,
8 extract commits in the current branch that are not in <upstream>,
9 and reconstruct the current on top of <upstream>, discarding the original
10 development history. If --onto <newbase> is specified, the history is
11 reconstructed on top of <newbase>, instead of <upstream>. For example,
12 while on "topic" branch:
18 $ '"$0"' --onto master~1 master topic
20 would rewrite the history to look like this:
23 A'\''--B'\''--C'\'' topic
31 while case "$#" in 0) break ;; esac
35 test 2 -le "$#" || usage
49 # Make sure we do not have .dotest
55 It seems that I cannot create a .dotest directory, and I wonder if you
56 are in the middle of patch application or another rebase. If that is not
57 the case, please rm -fr .dotest and run me again. I am stopping in case
58 you still have something valuable there.'
62 # The tree must be really really clean.
63 git-update-index --refresh || exit
64 diff=$(git-diff-index --cached --name-status -r HEAD)
71 # The upstream head must be given. Make sure it is valid.
73 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
74 die "invalid upsteram $upstream_name"
76 # If the branch to rebase is given, first switch to it.
80 git-checkout "$2" || usage
83 branch_name=`git symbolic-ref HEAD` || die "No current branch"
84 branch_name=`expr "$branch_name" : 'refs/heads/\(.*\)'`
87 branch=$(git-rev-parse --verify "${branch_name}^0") || exit
89 # Make sure the branch to rebase onto is valid.
90 onto_name=${newbase-"$upstream_name"}
91 onto=$(git-rev-parse --verify "${onto_name}^0") || exit
93 # Now we are rebasing commits $upstream..$branch on top of $onto
95 # Check if we are already based on $onto, but this should be
96 # done only when upstream and onto are the same.
97 if test "$upstream" = "onto"
99 mb=$(git-merge-base "$onto" "$branch")
100 if test "$mb" = "$onto"
102 echo >&2 "Current branch $branch_name is up to date."
107 # Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
108 git-reset --hard "$onto"
110 # If the $onto is a proper descendant of the tip of the branch, then
111 # we just fast forwarded.
112 if test "$mb" = "$onto"
114 echo >&2 "Fast-forwarded $branch to $newbase."
118 git-format-patch -k --stdout --full-index "$upstream" ORIG_HEAD |
119 git am --binary -3 -k