rebase: remove unused rebase state 'prev_head'
[gitweb.git] / git-rebase.sh
index d8e190302668ca352fd58cd052a677347a29cdcd..8c1c4ed02c1fa11c8c7f58c6fa695f151a2ee236 100755 (executable)
@@ -46,7 +46,8 @@ unset newbase
 strategy=recursive
 strategy_opts=
 do_merge=
-dotest="$GIT_DIR"/rebase-merge
+merge_dir="$GIT_DIR"/rebase-merge
+apply_dir="$GIT_DIR"/rebase-apply
 prec=4
 verbose=
 diffstat=
@@ -56,9 +57,24 @@ rebase_root=
 force_rebase=
 allow_rerere_autoupdate=
 
+read_state () {
+       if test -d "$merge_dir"
+       then
+               state_dir="$merge_dir"
+               onto_name=$(cat "$merge_dir"/onto_name) &&
+               end=$(cat "$merge_dir"/end) &&
+               msgnum=$(cat "$merge_dir"/msgnum)
+       else
+               state_dir="$apply_dir"
+       fi &&
+       head_name=$(cat "$state_dir"/head-name) &&
+       onto=$(cat "$state_dir"/onto) &&
+       orig_head=$(cat "$state_dir"/orig-head) &&
+       GIT_QUIET=$(cat "$state_dir"/quiet)
+}
+
 continue_merge () {
-       test -n "$prev_head" || die "prev_head must be defined"
-       test -d "$dotest" || die "$dotest directory does not exist"
+       test -d "$merge_dir" || die "$merge_dir directory does not exist"
 
        unmerged=$(git ls-files -u)
        if test -n "$unmerged"
@@ -68,7 +84,7 @@ continue_merge () {
                die "$RESOLVEMSG"
        fi
 
-       cmt=`cat "$dotest/current"`
+       cmt=`cat "$merge_dir/current"`
        if ! git diff-index --quiet --ignore-submodules HEAD --
        then
                if ! git commit --no-verify -C "$cmt"
@@ -81,7 +97,7 @@ continue_merge () {
                then
                        printf "Committed: %0${prec}d " $msgnum
                fi
-               echo "$cmt $(git rev-parse HEAD^0)" >> "$dotest/rewritten"
+               echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
        else
                if test -z "$GIT_QUIET"
                then
@@ -91,24 +107,19 @@ continue_merge () {
        test -z "$GIT_QUIET" &&
        GIT_PAGER='' git log --format=%s -1 "$cmt"
 
-       prev_head=`git rev-parse HEAD^0`
-       # save the resulting commit so we can read-tree on it later
-       echo "$prev_head" > "$dotest/prev_head"
-
        # onto the next patch:
        msgnum=$(($msgnum + 1))
-       echo "$msgnum" >"$dotest/msgnum"
+       echo "$msgnum" >"$merge_dir/msgnum"
 }
 
 call_merge () {
-       cmt="$(cat "$dotest/cmt.$1")"
-       echo "$cmt" > "$dotest/current"
+       cmt="$(cat "$merge_dir/cmt.$1")"
+       echo "$cmt" > "$merge_dir/current"
        hd=$(git rev-parse --verify HEAD)
        cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
-       msgnum=$(cat "$dotest/msgnum")
-       end=$(cat "$dotest/end")
+       msgnum=$(cat "$merge_dir/msgnum")
        eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
-       eval GITHEAD_$hd='$(cat "$dotest/onto_name")'
+       eval GITHEAD_$hd='$onto_name'
        export GITHEAD_$cmt GITHEAD_$hd
        if test -n "$GIT_QUIET"
        then
@@ -137,10 +148,6 @@ call_merge () {
 }
 
 move_to_original_branch () {
-       test -z "$head_name" &&
-               head_name="$(cat "$dotest"/head-name)" &&
-               onto="$(cat "$dotest"/onto)" &&
-               orig_head="$(cat "$dotest"/orig-head)"
        case "$head_name" in
        refs/*)
                message="rebase finished: $head_name onto $onto"
@@ -154,12 +161,12 @@ move_to_original_branch () {
 
 finish_rb_merge () {
        move_to_original_branch
-       git notes copy --for-rewrite=rebase < "$dotest"/rewritten
+       git notes copy --for-rewrite=rebase < "$merge_dir"/rewritten
        if test -x "$GIT_DIR"/hooks/post-rewrite &&
-               test -s "$dotest"/rewritten; then
-               "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
+               test -s "$merge_dir"/rewritten; then
+               "$GIT_DIR"/hooks/post-rewrite rebase < "$merge_dir"/rewritten
        fi
-       rm -r "$dotest"
+       rm -r "$merge_dir"
        say All done.
 }
 
@@ -183,7 +190,7 @@ is_interactive () {
                export GIT_EDITOR
        fi
 
-       test -n "$interactive_rebase" || test -f "$dotest"/interactive
+       test -n "$interactive_rebase" || test -f "$merge_dir"/interactive
 }
 
 run_pre_rebase_hook () {
@@ -195,7 +202,7 @@ run_pre_rebase_hook () {
        fi
 }
 
-test -f "$GIT_DIR"/rebase-apply/applying &&
+test -f "$apply_dir"/applying &&
        die 'It looks like git-am is in progress. Cannot rebase.'
 
 is_interactive "$@" && exec git-rebase--interactive "$@"
@@ -210,7 +217,7 @@ do
                OK_TO_SKIP_PRE_REBASE=
                ;;
        --continue)
-               test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
+               test -d "$merge_dir" -o -d "$apply_dir" ||
                        die "No rebase in progress?"
 
                git update-index --ignore-submodules --refresh &&
@@ -219,13 +226,9 @@ do
                        echo "mark them as resolved using git add"
                        exit 1
                }
-               if test -d "$dotest"
+               read_state
+               if test -d "$merge_dir"
                then
-                       prev_head=$(cat "$dotest/prev_head")
-                       end=$(cat "$dotest/end")
-                       msgnum=$(cat "$dotest/msgnum")
-                       onto=$(cat "$dotest/onto")
-                       GIT_QUIET=$(cat "$dotest/quiet")
                        continue_merge
                        while test "$msgnum" -le "$end"
                        do
@@ -235,28 +238,20 @@ do
                        finish_rb_merge
                        exit
                fi
-               head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) &&
-               onto=$(cat "$GIT_DIR"/rebase-apply/onto) &&
-               orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) &&
-               GIT_QUIET=$(cat "$GIT_DIR"/rebase-apply/quiet)
                git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
                move_to_original_branch
                exit
                ;;
        --skip)
-               test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
+               test -d "$merge_dir" -o -d "$apply_dir" ||
                        die "No rebase in progress?"
 
                git reset --hard HEAD || exit $?
-               if test -d "$dotest"
+               read_state
+               if test -d "$merge_dir"
                then
                        git rerere clear
-                       prev_head=$(cat "$dotest/prev_head")
-                       end=$(cat "$dotest/end")
-                       msgnum=$(cat "$dotest/msgnum")
                        msgnum=$(($msgnum + 1))
-                       onto=$(cat "$dotest/onto")
-                       GIT_QUIET=$(cat "$dotest/quiet")
                        while test "$msgnum" -le "$end"
                        do
                                call_merge "$msgnum"
@@ -265,31 +260,24 @@ do
                        finish_rb_merge
                        exit
                fi
-               head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) &&
-               onto=$(cat "$GIT_DIR"/rebase-apply/onto) &&
-               orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) &&
-               GIT_QUIET=$(cat "$GIT_DIR"/rebase-apply/quiet)
                git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
                move_to_original_branch
                exit
                ;;
        --abort)
-               test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
+               test -d "$merge_dir" -o -d "$apply_dir" ||
                        die "No rebase in progress?"
 
                git rerere clear
-
-               test -d "$dotest" || dotest="$GIT_DIR"/rebase-apply
-
-               head_name="$(cat "$dotest"/head-name)" &&
+               read_state
                case "$head_name" in
                refs/*)
                        git symbolic-ref HEAD $head_name ||
                        die "Could not move back to $head_name"
                        ;;
                esac
-               git reset --hard $(cat "$dotest/orig-head")
-               rm -r "$dotest"
+               git reset --hard $orig_head
+               rm -r "$state_dir"
                exit
                ;;
        --onto)
@@ -388,31 +376,31 @@ test $# -gt 2 && usage
 
 if test $# -eq 0 && test -z "$rebase_root"
 then
-       test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply || usage
-       test -d "$dotest" -o -f "$GIT_DIR"/rebase-apply/rebasing &&
+       test -d "$merge_dir" -o -d "$apply_dir" || usage
+       test -d "$merge_dir" -o -f "$apply_dir"/rebasing &&
                die 'A rebase is in progress, try --continue, --skip or --abort.'
 fi
 
-# Make sure we do not have $GIT_DIR/rebase-apply
+# Make sure we do not have $apply_dir or $merge_dir
 if test -z "$do_merge"
 then
-       if mkdir "$GIT_DIR"/rebase-apply 2>/dev/null
+       if mkdir "$apply_dir" 2>/dev/null
        then
-               rmdir "$GIT_DIR"/rebase-apply
+               rmdir "$apply_dir"
        else
                echo >&2 '
 It seems that I cannot create a rebase-apply directory, and
 I wonder if you are in the middle of patch application or another
 rebase.  If that is not the case, please
-       rm -fr '"$GIT_DIR"'/rebase-apply
+       rm -fr '"$apply_dir"'
 and run me again.  I am stopping in case you still have something
 valuable there.'
                exit 1
        fi
 else
-       if test -d "$dotest"
+       if test -d "$merge_dir"
        then
-               die "previous rebase directory $dotest still exists." \
+               die "previous rebase directory $merge_dir still exists." \
                        'Try git rebase (--continue | --abort | --skip)'
        fi
 fi
@@ -482,6 +470,7 @@ case "$#" in
        then
                head_name="detached HEAD"
        else
+               echo >&2 "fatal: no such branch: $1"
                usage
        fi
        ;;
@@ -513,7 +502,7 @@ then
        if test -z "$force_rebase"
        then
                # Lazily switch to the target branch if needed...
-               test -z "$switch_to" || git checkout "$switch_to"
+               test -z "$switch_to" || git checkout "$switch_to" --
                say "Current branch $branch_name is up to date."
                exit 0
        else
@@ -560,35 +549,33 @@ then
        git am $git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
        move_to_original_branch
        ret=$?
-       test 0 != $ret -a -d "$GIT_DIR"/rebase-apply &&
-               echo $head_name > "$GIT_DIR"/rebase-apply/head-name &&
-               echo $onto > "$GIT_DIR"/rebase-apply/onto &&
-               echo $orig_head > "$GIT_DIR"/rebase-apply/orig-head &&
-               echo "$GIT_QUIET" > "$GIT_DIR"/rebase-apply/quiet
+       test 0 != $ret -a -d "$apply_dir" &&
+               echo $head_name > "$apply_dir/head-name" &&
+               echo $onto > "$apply_dir/onto" &&
+               echo $orig_head > "$apply_dir/orig-head" &&
+               echo "$GIT_QUIET" > "$apply_dir/quiet"
        exit $ret
 fi
 
 # start doing a rebase with git-merge
 # this is rename-aware if the recursive (default) strategy is used
 
-mkdir -p "$dotest"
-echo "$onto" > "$dotest/onto"
-echo "$onto_name" > "$dotest/onto_name"
-prev_head=$orig_head
-echo "$prev_head" > "$dotest/prev_head"
-echo "$orig_head" > "$dotest/orig-head"
-echo "$head_name" > "$dotest/head-name"
-echo "$GIT_QUIET" > "$dotest/quiet"
+mkdir -p "$merge_dir"
+echo "$onto_name" > "$merge_dir/onto_name"
+echo "$head_name" > "$merge_dir/head-name"
+echo "$onto" > "$merge_dir/onto"
+echo "$orig_head" > "$merge_dir/orig-head"
+echo "$GIT_QUIET" > "$merge_dir/quiet"
 
 msgnum=0
 for cmt in `git rev-list --reverse --no-merges "$revisions"`
 do
        msgnum=$(($msgnum + 1))
-       echo "$cmt" > "$dotest/cmt.$msgnum"
+       echo "$cmt" > "$merge_dir/cmt.$msgnum"
 done
 
-echo 1 >"$dotest/msgnum"
-echo $msgnum >"$dotest/end"
+echo 1 >"$merge_dir/msgnum"
+echo $msgnum >"$merge_dir/end"
 
 end=$msgnum
 msgnum=1