Teach rebase to rebase even if upstream is up to date
[git] / git-rebase.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano.
4 #
5
6 USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--onto <newbase>] [<upstream>|--root] [<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.
12
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 .git/rebase-apply working files, use the
18 command git rebase --abort instead.
19
20 Note that if <branch> is not specified on the command line, the
21 currently checked out branch is used.
22
23 Example:       git-rebase master~1 topic
24
25         A---B---C topic                   A'\''--B'\''--C'\'' topic
26        /                   -->           /
27   D---E---F---G master          D---E---F---G master
28 '
29
30 SUBDIRECTORY_OK=Yes
31 OPTIONS_SPEC=
32 . git-sh-setup
33 set_reflog_action rebase
34 require_work_tree
35 cd_to_toplevel
36
37 OK_TO_SKIP_PRE_REBASE=
38 RESOLVEMSG="
39 When you have resolved this problem run \"git rebase --continue\".
40 If you would prefer to skip this patch, instead run \"git rebase --skip\".
41 To restore the original branch and stop rebasing run \"git rebase --abort\".
42 "
43 unset newbase
44 strategy=recursive
45 do_merge=
46 dotest="$GIT_DIR"/rebase-merge
47 prec=4
48 verbose=
49 git_am_opt=
50 rebase_root=
51 force_rebase=
52
53 continue_merge () {
54         test -n "$prev_head" || die "prev_head must be defined"
55         test -d "$dotest" || die "$dotest directory does not exist"
56
57         unmerged=$(git ls-files -u)
58         if test -n "$unmerged"
59         then
60                 echo "You still have unmerged paths in your index"
61                 echo "did you forget to use git add?"
62                 die "$RESOLVEMSG"
63         fi
64
65         cmt=`cat "$dotest/current"`
66         if ! git diff-index --quiet --ignore-submodules HEAD --
67         then
68                 if ! git commit --no-verify -C "$cmt"
69                 then
70                         echo "Commit failed, please do not call \"git commit\""
71                         echo "directly, but instead do one of the following: "
72                         die "$RESOLVEMSG"
73                 fi
74                 printf "Committed: %0${prec}d " $msgnum
75         else
76                 printf "Already applied: %0${prec}d " $msgnum
77         fi
78         git rev-list --pretty=oneline -1 "$cmt" | sed -e 's/^[^ ]* //'
79
80         prev_head=`git rev-parse HEAD^0`
81         # save the resulting commit so we can read-tree on it later
82         echo "$prev_head" > "$dotest/prev_head"
83
84         # onto the next patch:
85         msgnum=$(($msgnum + 1))
86         echo "$msgnum" >"$dotest/msgnum"
87 }
88
89 call_merge () {
90         cmt="$(cat "$dotest/cmt.$1")"
91         echo "$cmt" > "$dotest/current"
92         hd=$(git rev-parse --verify HEAD)
93         cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
94         msgnum=$(cat "$dotest/msgnum")
95         end=$(cat "$dotest/end")
96         eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
97         eval GITHEAD_$hd='$(cat "$dotest/onto_name")'
98         export GITHEAD_$cmt GITHEAD_$hd
99         git-merge-$strategy "$cmt^" -- "$hd" "$cmt"
100         rv=$?
101         case "$rv" in
102         0)
103                 unset GITHEAD_$cmt GITHEAD_$hd
104                 return
105                 ;;
106         1)
107                 git rerere
108                 die "$RESOLVEMSG"
109                 ;;
110         2)
111                 echo "Strategy: $rv $strategy failed, try another" 1>&2
112                 die "$RESOLVEMSG"
113                 ;;
114         *)
115                 die "Unknown exit code ($rv) from command:" \
116                         "git-merge-$strategy $cmt^ -- HEAD $cmt"
117                 ;;
118         esac
119 }
120
121 move_to_original_branch () {
122         test -z "$head_name" &&
123                 head_name="$(cat "$dotest"/head-name)" &&
124                 onto="$(cat "$dotest"/onto)" &&
125                 orig_head="$(cat "$dotest"/orig-head)"
126         case "$head_name" in
127         refs/*)
128                 message="rebase finished: $head_name onto $onto"
129                 git update-ref -m "$message" \
130                         $head_name $(git rev-parse HEAD) $orig_head &&
131                 git symbolic-ref HEAD $head_name ||
132                 die "Could not move back to $head_name"
133                 ;;
134         esac
135 }
136
137 finish_rb_merge () {
138         move_to_original_branch
139         rm -r "$dotest"
140         echo "All done."
141 }
142
143 is_interactive () {
144         while test $# != 0
145         do
146                 case "$1" in
147                         -i|--interactive)
148                                 interactive_rebase=explicit
149                                 break
150                         ;;
151                         -p|--preserve-merges)
152                                 interactive_rebase=implied
153                         ;;
154                 esac
155                 shift
156         done
157
158         if [ "$interactive_rebase" = implied ]; then
159                 GIT_EDITOR=:
160                 export GIT_EDITOR
161         fi
162
163         test -n "$interactive_rebase" || test -f "$dotest"/interactive
164 }
165
166 run_pre_rebase_hook () {
167         if test -z "$OK_TO_SKIP_PRE_REBASE" &&
168            test -x "$GIT_DIR/hooks/pre-rebase"
169         then
170                 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
171                         echo >&2 "The pre-rebase hook refused to rebase."
172                         exit 1
173                 }
174         fi
175 }
176
177 test -f "$GIT_DIR"/rebase-apply/applying &&
178         die 'It looks like git-am is in progress. Cannot rebase.'
179
180 is_interactive "$@" && exec git-rebase--interactive "$@"
181
182 if test $# -eq 0
183 then
184         test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply || usage
185         test -d "$dotest" -o -f "$GIT_DIR"/rebase-apply/rebasing &&
186                 die 'A rebase is in progress, try --continue, --skip or --abort.'
187         die "No arguments given and $GIT_DIR/rebase-apply already exists."
188 fi
189
190 while test $# != 0
191 do
192         case "$1" in
193         --no-verify)
194                 OK_TO_SKIP_PRE_REBASE=yes
195                 ;;
196         --continue)
197                 test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
198                         die "No rebase in progress?"
199
200                 git diff-files --quiet --ignore-submodules || {
201                         echo "You must edit all merge conflicts and then"
202                         echo "mark them as resolved using git add"
203                         exit 1
204                 }
205                 if test -d "$dotest"
206                 then
207                         prev_head=$(cat "$dotest/prev_head")
208                         end=$(cat "$dotest/end")
209                         msgnum=$(cat "$dotest/msgnum")
210                         onto=$(cat "$dotest/onto")
211                         continue_merge
212                         while test "$msgnum" -le "$end"
213                         do
214                                 call_merge "$msgnum"
215                                 continue_merge
216                         done
217                         finish_rb_merge
218                         exit
219                 fi
220                 head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) &&
221                 onto=$(cat "$GIT_DIR"/rebase-apply/onto) &&
222                 orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) &&
223                 git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
224                 move_to_original_branch
225                 exit
226                 ;;
227         --skip)
228                 test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
229                         die "No rebase in progress?"
230
231                 git reset --hard HEAD || exit $?
232                 if test -d "$dotest"
233                 then
234                         git rerere clear
235                         prev_head=$(cat "$dotest/prev_head")
236                         end=$(cat "$dotest/end")
237                         msgnum=$(cat "$dotest/msgnum")
238                         msgnum=$(($msgnum + 1))
239                         onto=$(cat "$dotest/onto")
240                         while test "$msgnum" -le "$end"
241                         do
242                                 call_merge "$msgnum"
243                                 continue_merge
244                         done
245                         finish_rb_merge
246                         exit
247                 fi
248                 head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) &&
249                 onto=$(cat "$GIT_DIR"/rebase-apply/onto) &&
250                 orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) &&
251                 git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
252                 move_to_original_branch
253                 exit
254                 ;;
255         --abort)
256                 test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
257                         die "No rebase in progress?"
258
259                 git rerere clear
260                 if test -d "$dotest"
261                 then
262                         move_to_original_branch
263                 else
264                         dotest="$GIT_DIR"/rebase-apply
265                         move_to_original_branch
266                 fi
267                 git reset --hard $(cat "$dotest/orig-head")
268                 rm -r "$dotest"
269                 exit
270                 ;;
271         --onto)
272                 test 2 -le "$#" || usage
273                 newbase="$2"
274                 shift
275                 ;;
276         -M|-m|--m|--me|--mer|--merg|--merge)
277                 do_merge=t
278                 ;;
279         -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
280                 --strateg=*|--strategy=*|\
281         -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
282                 case "$#,$1" in
283                 *,*=*)
284                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
285                 1,*)
286                         usage ;;
287                 *)
288                         strategy="$2"
289                         shift ;;
290                 esac
291                 do_merge=t
292                 ;;
293         -v|--verbose)
294                 verbose=t
295                 ;;
296         --whitespace=*)
297                 git_am_opt="$git_am_opt $1"
298                 case "$1" in
299                 --whitespace=fix|--whitespace=strip)
300                         force_rebase=t
301                         ;;
302                 esac
303                 ;;
304         -C*)
305                 git_am_opt="$git_am_opt $1"
306                 ;;
307         --root)
308                 rebase_root=t
309                 ;;
310         -f|--f|--fo|--for|--forc|force|--force-r|--force-re|--force-reb|--force-reba|--force_rebas|--force-rebase)
311                 force_rebase=t
312                 ;;
313         -*)
314                 usage
315                 ;;
316         *)
317                 break
318                 ;;
319         esac
320         shift
321 done
322
323 # Make sure we do not have $GIT_DIR/rebase-apply
324 if test -z "$do_merge"
325 then
326         if mkdir "$GIT_DIR"/rebase-apply 2>/dev/null
327         then
328                 rmdir "$GIT_DIR"/rebase-apply
329         else
330                 echo >&2 '
331 It seems that I cannot create a rebase-apply directory, and
332 I wonder if you are in the middle of patch application or another
333 rebase.  If that is not the case, please
334         rm -fr '"$GIT_DIR"'/rebase-apply
335 and run me again.  I am stopping in case you still have something
336 valuable there.'
337                 exit 1
338         fi
339 else
340         if test -d "$dotest"
341         then
342                 die "previous rebase directory $dotest still exists." \
343                         'Try git rebase (--continue | --abort | --skip)'
344         fi
345 fi
346
347 # The tree must be really really clean.
348 if ! git update-index --ignore-submodules --refresh; then
349         echo >&2 "cannot rebase: you have unstaged changes"
350         exit 1
351 fi
352 diff=$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)
353 case "$diff" in
354 ?*)     echo >&2 "cannot rebase: your index contains uncommitted changes"
355         echo >&2 "$diff"
356         exit 1
357         ;;
358 esac
359
360 if test -z "$rebase_root"
361 then
362         # The upstream head must be given.  Make sure it is valid.
363         upstream_name="$1"
364         shift
365         upstream=`git rev-parse --verify "${upstream_name}^0"` ||
366         die "invalid upstream $upstream_name"
367         unset root_flag
368         upstream_arg="$upstream_name"
369 else
370         test -z "$newbase" && die "--root must be used with --onto"
371         unset upstream_name
372         unset upstream
373         root_flag="--root"
374         upstream_arg="$root_flag"
375 fi
376
377 # Make sure the branch to rebase onto is valid.
378 onto_name=${newbase-"$upstream_name"}
379 onto=$(git rev-parse --verify "${onto_name}^0") || exit
380
381 # If a hook exists, give it a chance to interrupt
382 run_pre_rebase_hook "$upstream_arg" "$@"
383
384 # If the branch to rebase is given, that is the branch we will rebase
385 # $branch_name -- branch being rebased, or HEAD (already detached)
386 # $orig_head -- commit object name of tip of the branch before rebasing
387 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
388 switch_to=
389 case "$#" in
390 1)
391         # Is it "rebase other $branchname" or "rebase other $commit"?
392         branch_name="$1"
393         switch_to="$1"
394
395         if git show-ref --verify --quiet -- "refs/heads/$1" &&
396            branch=$(git rev-parse -q --verify "refs/heads/$1")
397         then
398                 head_name="refs/heads/$1"
399         elif branch=$(git rev-parse -q --verify "$1")
400         then
401                 head_name="detached HEAD"
402         else
403                 usage
404         fi
405         ;;
406 *)
407         # Do not need to switch branches, we are already on it.
408         if branch_name=`git symbolic-ref -q HEAD`
409         then
410                 head_name=$branch_name
411                 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
412         else
413                 head_name="detached HEAD"
414                 branch_name=HEAD ;# detached
415         fi
416         branch=$(git rev-parse --verify "${branch_name}^0") || exit
417         ;;
418 esac
419 orig_head=$branch
420
421 # Now we are rebasing commits $upstream..$branch (or with --root,
422 # everything leading up to $branch) on top of $onto
423
424 # Check if we are already based on $onto with linear history,
425 # but this should be done only when upstream and onto are the same.
426 mb=$(git merge-base "$onto" "$branch")
427 if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
428         # linear history?
429         ! (git rev-list --parents "$onto".."$branch" | grep " .* ") > /dev/null
430 then
431         if test -z "$force_rebase"
432         then
433                 # Lazily switch to the target branch if needed...
434                 test -z "$switch_to" || git checkout "$switch_to"
435                 echo >&2 "Current branch $branch_name is up to date."
436                 exit 0
437         else
438                 echo "Current branch $branch_name is up to date, rebase forced."
439         fi
440 fi
441
442 if test -n "$verbose"
443 then
444         echo "Changes from $mb to $onto:"
445         # We want color (if set), but no pager
446         GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
447 fi
448
449 # Detach HEAD and reset the tree
450 echo "First, rewinding head to replay your work on top of it..."
451 git checkout -q "$onto^0" || die "could not detach HEAD"
452 git update-ref ORIG_HEAD $branch
453
454 # If the $onto is a proper descendant of the tip of the branch, then
455 # we just fast forwarded.
456 if test "$mb" = "$branch"
457 then
458         echo >&2 "Fast-forwarded $branch_name to $onto_name."
459         move_to_original_branch
460         exit 0
461 fi
462
463 if test -n "$rebase_root"
464 then
465         revisions="$onto..$orig_head"
466 else
467         revisions="$upstream..$orig_head"
468 fi
469
470 if test -z "$do_merge"
471 then
472         git format-patch -k --stdout --full-index --ignore-if-in-upstream \
473                 $root_flag "$revisions" |
474         git am $git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
475         move_to_original_branch
476         ret=$?
477         test 0 != $ret -a -d "$GIT_DIR"/rebase-apply &&
478                 echo $head_name > "$GIT_DIR"/rebase-apply/head-name &&
479                 echo $onto > "$GIT_DIR"/rebase-apply/onto &&
480                 echo $orig_head > "$GIT_DIR"/rebase-apply/orig-head
481         exit $ret
482 fi
483
484 # start doing a rebase with git-merge
485 # this is rename-aware if the recursive (default) strategy is used
486
487 mkdir -p "$dotest"
488 echo "$onto" > "$dotest/onto"
489 echo "$onto_name" > "$dotest/onto_name"
490 prev_head=$orig_head
491 echo "$prev_head" > "$dotest/prev_head"
492 echo "$orig_head" > "$dotest/orig-head"
493 echo "$head_name" > "$dotest/head-name"
494
495 msgnum=0
496 for cmt in `git rev-list --reverse --no-merges "$revisions"`
497 do
498         msgnum=$(($msgnum + 1))
499         echo "$cmt" > "$dotest/cmt.$msgnum"
500 done
501
502 echo 1 >"$dotest/msgnum"
503 echo $msgnum >"$dotest/end"
504
505 end=$msgnum
506 msgnum=1
507
508 while test "$msgnum" -le "$end"
509 do
510         call_merge "$msgnum"
511         continue_merge
512 done
513
514 finish_rb_merge