Code

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