connect.c: check the commit buffer boundary while parsing.
[git/jnareb-git.git] / git-rebase.sh
blobfd0e7c498d8cdfe210fd0b34f6b0e7acaf8b381a
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano.
6 USAGE='[--onto <newbase>] <upstream> [<branch>]'
7 LONG_USAGE='git-rebase replaces <branch> with a new branch of the
8 same name. When the --onto option is provided the new branch starts
9 out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
10 It then attempts to create a new commit for each commit from the original
11 <branch> that does not exist in the <upstream> branch.
13 It is possible that a merge failure will prevent this process from being
14 completely automatic. You will have to resolve any such merge failure
15 and run git rebase --continue. Another option is to bypass the commit
16 that caused the merge failure with git rebase --skip. To restore the
17 original <branch> and remove the .dotest working files, use the command
18 git rebase --abort instead.
20 Note that if <branch> is not specified on the command line, the
21 currently checked out branch is used. You must be in the top
22 directory of your project to start (or continue) a rebase.
24 Example: git-rebase master~1 topic
26 A---B---C topic A'\''--B'\''--C'\'' topic
27 / --> /
28 D---E---F---G master D---E---F---G master
30 . git-sh-setup
32 RESOLVEMSG="
33 When you have resolved this problem run \"git rebase --continue\".
34 If you would prefer to skip this patch, instead run \"git rebase --skip\".
35 To restore the original branch and stop rebasing run \"git rebase --abort\".
37 unset newbase
38 strategy=recursive
39 do_merge=
40 dotest=$GIT_DIR/.dotest-merge
41 prec=4
43 continue_merge () {
44 test -n "$prev_head" || die "prev_head must be defined"
45 test -d "$dotest" || die "$dotest directory does not exist"
47 unmerged=$(git-ls-files -u)
48 if test -n "$unmerged"
49 then
50 echo "You still have unmerged paths in your index"
51 echo "did you forget update-index?"
52 die "$RESOLVEMSG"
55 if test -n "`git-diff-index HEAD`"
56 then
57 printf "Committed: %0${prec}d" $msgnum
58 git-commit -C "`cat $dotest/current`"
59 else
60 printf "Already applied: %0${prec}d" $msgnum
62 echo ' '`git-rev-list --pretty=oneline -1 HEAD | \
63 sed 's/^[a-f0-9]\+ //'`
65 prev_head=`git-rev-parse HEAD^0`
66 # save the resulting commit so we can read-tree on it later
67 echo "$prev_head" > "$dotest/prev_head"
69 # onto the next patch:
70 msgnum=$(($msgnum + 1))
71 echo "$msgnum" >"$dotest/msgnum"
74 call_merge () {
75 cmt="$(cat $dotest/cmt.$1)"
76 echo "$cmt" > "$dotest/current"
77 git-merge-$strategy "$cmt^" -- HEAD "$cmt"
78 rv=$?
79 case "$rv" in
81 return
84 test -d "$GIT_DIR/rr-cache" && git-rerere
85 die "$RESOLVEMSG"
88 echo "Strategy: $rv $strategy failed, try another" 1>&2
89 die "$RESOLVEMSG"
92 die "Unknown exit code ($rv) from command:" \
93 "git-merge-$strategy $cmt^ -- HEAD $cmt"
95 esac
98 finish_rb_merge () {
99 rm -r "$dotest"
100 echo "All done."
103 while case "$#" in 0) break ;; esac
105 case "$1" in
106 --continue)
107 diff=$(git-diff-files)
108 case "$diff" in
109 ?*) echo "You must edit all merge conflicts and then"
110 echo "mark them as resolved using git update-index"
111 exit 1
113 esac
114 if test -d "$dotest"
115 then
116 prev_head="`cat $dotest/prev_head`"
117 end="`cat $dotest/end`"
118 msgnum="`cat $dotest/msgnum`"
119 onto="`cat $dotest/onto`"
120 continue_merge
121 while test "$msgnum" -le "$end"
123 call_merge "$msgnum"
124 continue_merge
125 done
126 finish_rb_merge
127 exit
129 git am --resolved --3way --resolvemsg="$RESOLVEMSG"
130 exit
132 --skip)
133 if test -d "$dotest"
134 then
135 prev_head="`cat $dotest/prev_head`"
136 end="`cat $dotest/end`"
137 msgnum="`cat $dotest/msgnum`"
138 msgnum=$(($msgnum + 1))
139 onto="`cat $dotest/onto`"
140 while test "$msgnum" -le "$end"
142 call_merge "$msgnum"
143 continue_merge
144 done
145 finish_rb_merge
146 exit
148 git am -3 --skip --resolvemsg="$RESOLVEMSG"
149 exit
151 --abort)
152 if test -d "$dotest"
153 then
154 rm -r "$dotest"
155 elif test -d .dotest
156 then
157 rm -r .dotest
158 else
159 die "No rebase in progress?"
161 git reset --hard ORIG_HEAD
162 exit
164 --onto)
165 test 2 -le "$#" || usage
166 newbase="$2"
167 shift
169 -M|-m|--m|--me|--mer|--merg|--merge)
170 do_merge=t
172 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
173 --strateg=*|--strategy=*|\
174 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
175 case "$#,$1" in
176 *,*=*)
177 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
178 1,*)
179 usage ;;
181 strategy="$2"
182 shift ;;
183 esac
184 do_merge=t
187 usage
190 break
192 esac
193 shift
194 done
196 # Make sure we do not have .dotest
197 if test -z "$do_merge"
198 then
199 if mkdir .dotest
200 then
201 rmdir .dotest
202 else
203 echo >&2 '
204 It seems that I cannot create a .dotest directory, and I wonder if you
205 are in the middle of patch application or another rebase. If that is not
206 the case, please rm -fr .dotest and run me again. I am stopping in case
207 you still have something valuable there.'
208 exit 1
210 else
211 if test -d "$dotest"
212 then
213 die "previous dotest directory $dotest still exists." \
214 'try git-rebase < --continue | --abort >'
218 # The tree must be really really clean.
219 git-update-index --refresh || exit
220 diff=$(git-diff-index --cached --name-status -r HEAD)
221 case "$diff" in
222 ?*) echo "$diff"
223 exit 1
225 esac
227 # The upstream head must be given. Make sure it is valid.
228 upstream_name="$1"
229 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
230 die "invalid upstream $upstream_name"
232 # If a hook exists, give it a chance to interrupt
233 if test -x "$GIT_DIR/hooks/pre-rebase"
234 then
235 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
236 echo >&2 "The pre-rebase hook refused to rebase."
237 exit 1
241 # If the branch to rebase is given, first switch to it.
242 case "$#" in
244 branch_name="$2"
245 git-checkout "$2" || usage
248 branch_name=`git symbolic-ref HEAD` || die "No current branch"
249 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
251 esac
252 branch=$(git-rev-parse --verify "${branch_name}^0") || exit
254 # Make sure the branch to rebase onto is valid.
255 onto_name=${newbase-"$upstream_name"}
256 onto=$(git-rev-parse --verify "${onto_name}^0") || exit
258 # Now we are rebasing commits $upstream..$branch on top of $onto
260 # Check if we are already based on $onto, but this should be
261 # done only when upstream and onto are the same.
262 if test "$upstream" = "$onto"
263 then
264 mb=$(git-merge-base "$onto" "$branch")
265 if test "$mb" = "$onto"
266 then
267 echo >&2 "Current branch $branch_name is up to date."
268 exit 0
272 # Rewind the head to "$onto"; this saves our current head in ORIG_HEAD.
273 git-reset --hard "$onto"
275 # If the $onto is a proper descendant of the tip of the branch, then
276 # we just fast forwarded.
277 if test "$mb" = "$onto"
278 then
279 echo >&2 "Fast-forwarded $branch to $newbase."
280 exit 0
283 if test -z "$do_merge"
284 then
285 git-format-patch -k --stdout --full-index "$upstream"..ORIG_HEAD |
286 git am --binary -3 -k --resolvemsg="$RESOLVEMSG"
287 exit $?
290 if test "@@NO_PYTHON@@" && test "$strategy" = "recursive"
291 then
292 die 'The recursive merge strategy currently relies on Python,
293 which this installation of git was not configured with. Please consider
294 a different merge strategy (e.g. octopus, resolve, stupid, ours)
295 or install Python and git with Python support.'
299 # start doing a rebase with git-merge
300 # this is rename-aware if the recursive (default) strategy is used
302 mkdir -p "$dotest"
303 echo "$onto" > "$dotest/onto"
304 prev_head=`git-rev-parse HEAD^0`
305 echo "$prev_head" > "$dotest/prev_head"
307 msgnum=0
308 for cmt in `git-rev-list --no-merges "$upstream"..ORIG_HEAD \
309 | perl -e 'print reverse <>'`
311 msgnum=$(($msgnum + 1))
312 echo "$cmt" > "$dotest/cmt.$msgnum"
313 done
315 echo 1 >"$dotest/msgnum"
316 echo $msgnum >"$dotest/end"
318 end=$msgnum
319 msgnum=1
321 while test "$msgnum" -le "$end"
323 call_merge "$msgnum"
324 continue_merge
325 done
327 finish_rb_merge