33# Copyright (c) 2005 Junio C Hamano.
44#
55
6- USAGE=' <upstream> [<head>]'
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:
13+
14+ A---B---C topic
15+ /
16+ D---E---F---G master
17+
18+ $ ' " $0 " ' --onto master~1 master topic
19+
20+ would rewrite the history to look like this:
21+
22+
23+ A' \' ' --B' \' ' --C' \' ' topic
24+ /
25+ D---E---F---G master
26+ '
27+
728. git-sh-setup
829
9- case $# in 1|2) ;; * ) usage ;; esac
30+ unset newbase
31+ while case " $# " in 0) break ;; esac
32+ do
33+ case " $1 " in
34+ --onto)
35+ test 2 -le " $# " || usage
36+ newbase=" $2 "
37+ shift
38+ ;;
39+ -* )
40+ usage
41+ ;;
42+ * )
43+ break
44+ ;;
45+ esac
46+ shift
47+ done
1048
1149# Make sure we do not have .dotest
1250if mkdir .dotest
@@ -30,37 +68,52 @@ case "$diff" in
3068 ;;
3169esac
3270
33- # The other head is given. Make sure it is valid.
34- other=$( git-rev-parse --verify " $1 ^0" ) || usage
35-
36- # Make sure the branch to rebase is valid.
37- head=$( git-rev-parse --verify " ${2-HEAD} ^0" ) || exit
71+ # The upstream head must be given. Make sure it is valid.
72+ upstream_name=" $1 "
73+ upstream=` git rev-parse --verify " ${upstream_name} ^0" ` ||
74+ die " invalid upsteram $upstream_name "
3875
3976# If the branch to rebase is given, first switch to it.
4077case " $# " in
41782)
79+ branch_name=" $2 "
4280 git-checkout " $2 " || usage
81+ ;;
82+ * )
83+ branch_name=` git symbolic-ref HEAD` || die " No current branch"
84+ branch_name=` expr " $branch_name " : ' refs/heads/\(.*\)' `
85+ ;;
4386esac
87+ branch=$( git-rev-parse --verify " ${branch_name} ^0" ) || exit
4488
45- mb=$( git-merge-base " $other " " $head " )
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
4692
47- # Check if we are already based on $other.
48- if test " $mb " = " $other "
93+ # Now we are rebasing commits $upstream..$branch on top of $onto
94+
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"
4998then
50- echo >&2 " Current branch ` git-symbolic-ref HEAD` is up to date."
51- exit 0
99+ mb=$( git-merge-base " $onto " " $branch " )
100+ if test " $mb " = " $onto "
101+ then
102+ echo >&2 " Current branch $branch_name is up to date."
103+ exit 0
104+ fi
52105fi
53106
54- # Rewind the head to "$other"
55- git-reset --hard " $other "
107+ # Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
108+ git-reset --hard " $onto "
56109
57- # If the $other is a proper descendant of the tip of the branch, then
110+ # If the $onto is a proper descendant of the tip of the branch, then
58111# we just fast forwarded.
59- if test " $mb " = " $head "
112+ if test " $mb " = " $onto "
60113then
61- echo >&2 " Fast-forwarded $head to $other ."
114+ echo >&2 " Fast-forwarded $branch to $newbase ."
62115 exit 0
63116fi
64117
65- git-format-patch -k --stdout --full-index " $other " ORIG_HEAD |
118+ git-format-patch -k --stdout --full-index " $upstream " ORIG_HEAD |
66119git am --binary -3 -k
0 commit comments