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