Merge master to get fixes up to 1.2.1
[git.git] / git-rebase.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano.
4 #
5
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
28 . git-sh-setup
29
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
48
49 # Make sure we do not have .dotest
50 if mkdir .dotest
51 then
52         rmdir .dotest
53 else
54         echo >&2 '
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.'
59         exit 1
60 fi
61
62 # The tree must be really really clean.
63 git-update-index --refresh || exit
64 diff=$(git-diff-index --cached --name-status -r HEAD)
65 case "$diff" in
66 ?*)     echo "$diff"
67         exit 1
68         ;;
69 esac
70
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"
75
76 # If a hook exists, give it a chance to interrupt
77 if test -x "$GIT_DIR/hooks/pre-rebase"
78 then
79         "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
80                 echo >&2 "The pre-rebase hook refused to rebase."
81                 exit 1
82         }
83 fi
84
85 # If the branch to rebase is given, first switch to it.
86 case "$#" in
87 2)
88         branch_name="$2"
89         git-checkout "$2" || usage
90         ;;
91 *)
92         branch_name=`git symbolic-ref HEAD` || die "No current branch"
93         branch_name=`expr "$branch_name" : 'refs/heads/\(.*\)'`
94         ;;
95 esac
96 branch=$(git-rev-parse --verify "${branch_name}^0") || exit
97
98 # Make sure the branch to rebase onto is valid.
99 onto_name=${newbase-"$upstream_name"}
100 onto=$(git-rev-parse --verify "${onto_name}^0") || exit
101
102 # Now we are rebasing commits $upstream..$branch on top of $onto
103
104 # Check if we are already based on $onto, but this should be
105 # done only when upstream and onto are the same.
106 if test "$upstream" = "onto"
107 then
108         mb=$(git-merge-base "$onto" "$branch")
109         if test "$mb" = "$onto"
110         then
111                 echo >&2 "Current branch $branch_name is up to date."
112                 exit 0
113         fi
114 fi
115
116 # Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
117 git-reset --hard "$onto"
118
119 # If the $onto is a proper descendant of the tip of the branch, then
120 # we just fast forwarded.
121 if test "$mb" = "$onto"
122 then
123         echo >&2 "Fast-forwarded $branch to $newbase."
124         exit 0
125 fi
126
127 git-format-patch -k --stdout --full-index "$upstream" ORIG_HEAD |
128 git am --binary -3 -k