rebase: implement --merge via the interactive machinery

As part of an ongoing effort to make rebase have more uniform behavior,
modify the merge backend to behave like the interactive one, by
re-implementing it on top of the latter.

Interactive rebases are implemented in terms of cherry-pick rather than
the merge-recursive builtin, but cherry-pick also calls into the
recursive merge machinery by default and can accept special merge
strategies and/or special strategy options.  As such, there really is
not any need for having both git-rebase--merge and
git-rebase--interactive anymore.  Delete git-rebase--merge.sh and
instead implement it in builtin/rebase.c.

This results in a few deliberate but small user-visible changes:
  * The progress output is modified (see t3406 and t3420 for examples)
  * A few known test failures are now fixed (see t3421)
  * bash-prompt during a rebase --merge is now REBASE-i instead of
    REBASE-m.  Reason: The prompt is a reflection of the backend in use;
    this allows users to report an issue to the git mailing list with
    the appropriate backend information, and allows advanced users to
    know where to search for relevant control files.  (see t9903)

testcase modification notes:
  t3406: --interactive and --merge had slightly different progress output
         while running; adjust a test to match the new expectation
  t3420: these test precise output while running, but rebase--am,
         rebase--merge, and rebase--interactive all were built on very
         different commands (am, merge-recursive, cherry-pick), so the
         tests expected different output for each type.  Now we expect
         --merge and --interactive to have the same output.
  t3421: --interactive fixes some bugs in --merge!  Wahoo!
  t9903: --merge uses the interactive backend so the prompt expected is
         now REBASE-i.

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Elijah Newren 2018-12-11 08:11:39 -08:00 committed by Junio C Hamano
parent c91c944a06
commit 68aa495b59
10 changed files with 43 additions and 297 deletions

1
.gitignore vendored
View file

@ -124,7 +124,6 @@
/git-rebase--am /git-rebase--am
/git-rebase--common /git-rebase--common
/git-rebase--interactive /git-rebase--interactive
/git-rebase--merge
/git-rebase--preserve-merges /git-rebase--preserve-merges
/git-receive-pack /git-receive-pack
/git-reflog /git-reflog

View file

@ -504,15 +504,7 @@ See also INCOMPATIBLE OPTIONS below.
INCOMPATIBLE OPTIONS INCOMPATIBLE OPTIONS
-------------------- --------------------
git-rebase has many flags that are incompatible with each other, The following options:
predominantly due to the fact that it has three different underlying
implementations:
* one based on linkgit:git-am[1] (the default)
* one based on git-merge-recursive (merge backend)
* one based on linkgit:git-cherry-pick[1] (interactive backend)
Flags only understood by the am backend:
* --committer-date-is-author-date * --committer-date-is-author-date
* --ignore-date * --ignore-date
@ -520,15 +512,12 @@ Flags only understood by the am backend:
* --ignore-whitespace * --ignore-whitespace
* -C * -C
Flags understood by both merge and interactive backends: are incompatible with the following options:
* --merge * --merge
* --strategy * --strategy
* --strategy-option * --strategy-option
* --allow-empty-message * --allow-empty-message
Flags only understood by the interactive backend:
* --[no-]autosquash * --[no-]autosquash
* --rebase-merges * --rebase-merges
* --preserve-merges * --preserve-merges
@ -539,7 +528,7 @@ Flags only understood by the interactive backend:
* --edit-todo * --edit-todo
* --root when used in combination with --onto * --root when used in combination with --onto
Other incompatible flag pairs: In addition, the following pairs of options are incompatible:
* --preserve-merges and --interactive * --preserve-merges and --interactive
* --preserve-merges and --signoff * --preserve-merges and --signoff

View file

@ -628,7 +628,6 @@ SCRIPT_LIB += git-parse-remote
SCRIPT_LIB += git-rebase--am SCRIPT_LIB += git-rebase--am
SCRIPT_LIB += git-rebase--common SCRIPT_LIB += git-rebase--common
SCRIPT_LIB += git-rebase--preserve-merges SCRIPT_LIB += git-rebase--preserve-merges
SCRIPT_LIB += git-rebase--merge
SCRIPT_LIB += git-sh-setup SCRIPT_LIB += git-sh-setup
SCRIPT_LIB += git-sh-i18n SCRIPT_LIB += git-sh-i18n

View file

@ -122,7 +122,7 @@ static void imply_interactive(struct rebase_options *opts, const char *option)
case REBASE_PRESERVE_MERGES: case REBASE_PRESERVE_MERGES:
break; break;
case REBASE_MERGE: case REBASE_MERGE:
/* we silently *upgrade* --merge to --interactive if needed */ /* we now implement --merge via --interactive */
default: default:
opts->type = REBASE_INTERACTIVE; /* implied */ opts->type = REBASE_INTERACTIVE; /* implied */
break; break;
@ -481,10 +481,6 @@ static int run_specific_rebase(struct rebase_options *opts)
backend = "git-rebase--am"; backend = "git-rebase--am";
backend_func = "git_rebase__am"; backend_func = "git_rebase__am";
break; break;
case REBASE_MERGE:
backend = "git-rebase--merge";
backend_func = "git_rebase__merge";
break;
case REBASE_PRESERVE_MERGES: case REBASE_PRESERVE_MERGES:
backend = "git-rebase--preserve-merges"; backend = "git-rebase--preserve-merges";
backend_func = "git_rebase__preserve_merges"; backend_func = "git_rebase__preserve_merges";
@ -1191,6 +1187,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
} }
} }
if (options.type == REBASE_MERGE)
imply_interactive(&options, "--merge");
if (options.root && !options.onto_name) if (options.root && !options.onto_name)
imply_interactive(&options, "--root without --onto"); imply_interactive(&options, "--root without --onto");
@ -1220,10 +1219,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
break; break;
if (is_interactive(&options) && i >= 0) if (is_interactive(&options) && i >= 0)
die(_("cannot combine am options " die(_("cannot combine am options with either "
"with interactive options")); "interactive or merge options"));
if (options.type == REBASE_MERGE && i >= 0)
die(_("cannot combine am options with merge options "));
} }
if (options.signoff) { if (options.signoff) {

View file

@ -218,6 +218,7 @@ then
state_dir="$apply_dir" state_dir="$apply_dir"
elif test -d "$merge_dir" elif test -d "$merge_dir"
then then
type=interactive
if test -d "$merge_dir"/rewritten if test -d "$merge_dir"/rewritten
then then
type=preserve-merges type=preserve-merges
@ -225,10 +226,7 @@ then
preserve_merges=t preserve_merges=t
elif test -f "$merge_dir"/interactive elif test -f "$merge_dir"/interactive
then then
type=interactive
interactive_rebase=explicit interactive_rebase=explicit
else
type=merge
fi fi
state_dir="$merge_dir" state_dir="$merge_dir"
fi fi
@ -477,6 +475,7 @@ then
test -z "$interactive_rebase" && interactive_rebase=implied test -z "$interactive_rebase" && interactive_rebase=implied
fi fi
actually_interactive=
if test -n "$interactive_rebase" if test -n "$interactive_rebase"
then then
if test -z "$preserve_merges" if test -z "$preserve_merges"
@ -485,11 +484,12 @@ then
else else
type=preserve-merges type=preserve-merges
fi fi
actually_interactive=t
state_dir="$merge_dir" state_dir="$merge_dir"
elif test -n "$do_merge" elif test -n "$do_merge"
then then
type=merge interactive_rebase=implied
type=interactive
state_dir="$merge_dir" state_dir="$merge_dir"
else else
type=am type=am
@ -505,13 +505,9 @@ incompatible_opts=$(echo " $git_am_opt " | \
sed -e 's/ -q / /g' -e 's/^ \(.*\) $/\1/') sed -e 's/ -q / /g' -e 's/^ \(.*\) $/\1/')
if test -n "$incompatible_opts" if test -n "$incompatible_opts"
then then
if test -n "$interactive_rebase" if test -n "$actually_interactive" || test "$do_merge"
then then
die "$(gettext "fatal: cannot combine am options with interactive options")" die "$(gettext "fatal: cannot combine am options with either interactive or merge options")"
fi
if test -n "$do_merge"
then
die "$(gettext "fatal: cannot combine am options with merge options")"
fi fi
fi fi
@ -676,7 +672,7 @@ require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
# but this should be done only when upstream and onto are the same # but this should be done only when upstream and onto are the same
# and if this is not an interactive rebase. # and if this is not an interactive rebase.
mb=$(git merge-base "$onto" "$orig_head") mb=$(git merge-base "$onto" "$orig_head")
if test -z "$interactive_rebase" && test "$upstream" = "$onto" && if test -z "$actually_interactive" && test "$upstream" = "$onto" &&
test "$mb" = "$onto" && test -z "$restrict_revision" && test "$mb" = "$onto" && test -z "$restrict_revision" &&
# linear history? # linear history?
! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
@ -726,6 +722,19 @@ then
GIT_PAGER='' git diff --stat --summary "$mb_tree" "$onto" GIT_PAGER='' git diff --stat --summary "$mb_tree" "$onto"
fi fi
if test -z "$actually_interactive" && test "$mb" = "$orig_head"
then
say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
git checkout -q "$onto^0" || die "could not detach HEAD"
# If the $onto is a proper descendant of the tip of the branch, then
# we just fast-forwarded.
git update-ref ORIG_HEAD $orig_head
move_to_original_branch
finish_rebase
exit 0
fi
test -n "$interactive_rebase" && run_specific_rebase test -n "$interactive_rebase" && run_specific_rebase
# Detach HEAD and reset the tree # Detach HEAD and reset the tree
@ -735,16 +744,6 @@ GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
git checkout -q "$onto^0" || die "could not detach HEAD" git checkout -q "$onto^0" || die "could not detach HEAD"
git update-ref ORIG_HEAD $orig_head git update-ref ORIG_HEAD $orig_head
# If the $onto is a proper descendant of the tip of the branch, then
# we just fast-forwarded.
if test "$mb" = "$orig_head"
then
say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
move_to_original_branch
finish_rebase
exit 0
fi
if test -n "$rebase_root" if test -n "$rebase_root"
then then
revisions="$onto..$orig_head" revisions="$onto..$orig_head"

View file

@ -1,166 +0,0 @@
# This shell script fragment is sourced by git-rebase to implement
# its merge-based non-interactive mode that copes well with renamed
# files.
#
# Copyright (c) 2010 Junio C Hamano.
#
prec=4
read_state () {
onto_name=$(cat "$state_dir"/onto_name) &&
end=$(cat "$state_dir"/end) &&
msgnum=$(cat "$state_dir"/msgnum)
}
continue_merge () {
test -d "$state_dir" || die "$state_dir directory does not exist"
unmerged=$(git ls-files -u)
if test -n "$unmerged"
then
echo "You still have unmerged paths in your index"
echo "did you forget to use git add?"
die "$resolvemsg"
fi
cmt=$(cat "$state_dir/current")
if ! git diff-index --quiet --ignore-submodules HEAD --
then
if ! git commit ${gpg_sign_opt:+"$gpg_sign_opt"} $signoff $allow_empty_message \
--no-verify -C "$cmt"
then
echo "Commit failed, please do not call \"git commit\""
echo "directly, but instead do one of the following: "
die "$resolvemsg"
fi
if test -z "$GIT_QUIET"
then
printf "Committed: %0${prec}d " $msgnum
fi
echo "$cmt $(git rev-parse HEAD^0)" >> "$state_dir/rewritten"
else
if test -z "$GIT_QUIET"
then
printf "Already applied: %0${prec}d " $msgnum
fi
fi
test -z "$GIT_QUIET" &&
GIT_PAGER='' git log --format=%s -1 "$cmt"
# onto the next patch:
msgnum=$(($msgnum + 1))
echo "$msgnum" >"$state_dir/msgnum"
}
call_merge () {
msgnum="$1"
echo "$msgnum" >"$state_dir/msgnum"
cmt="$(cat "$state_dir/cmt.$msgnum")"
echo "$cmt" > "$state_dir/current"
git update-ref REBASE_HEAD "$cmt"
hd=$(git rev-parse --verify HEAD)
cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
eval GITHEAD_$hd='$onto_name'
export GITHEAD_$cmt GITHEAD_$hd
if test -n "$GIT_QUIET"
then
GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
fi
test -z "$strategy" && strategy=recursive
# If cmt doesn't have a parent, don't include it as a base
base=$(git rev-parse --verify --quiet $cmt^)
eval 'git merge-$strategy' $strategy_opts $base ' -- "$hd" "$cmt"'
rv=$?
case "$rv" in
0)
unset GITHEAD_$cmt GITHEAD_$hd
return
;;
1)
git rerere $allow_rerere_autoupdate
die "$resolvemsg"
;;
2)
echo "Strategy: $strategy failed, try another" 1>&2
die "$resolvemsg"
;;
*)
die "Unknown exit code ($rv) from command:" \
"git merge-$strategy $cmt^ -- HEAD $cmt"
;;
esac
}
finish_rb_merge () {
move_to_original_branch
if test -s "$state_dir"/rewritten
then
git notes copy --for-rewrite=rebase <"$state_dir"/rewritten
hook="$(git rev-parse --git-path hooks/post-rewrite)"
test -x "$hook" && "$hook" rebase <"$state_dir"/rewritten
fi
say All done.
}
git_rebase__merge () {
case "$action" in
continue)
read_state
continue_merge
while test "$msgnum" -le "$end"
do
call_merge "$msgnum"
continue_merge
done
finish_rb_merge
return
;;
skip)
read_state
git rerere clear
cmt="$(cat "$state_dir/cmt.$msgnum")"
echo "$cmt $(git rev-parse HEAD^0)" >> "$state_dir/rewritten"
msgnum=$(($msgnum + 1))
while test "$msgnum" -le "$end"
do
call_merge "$msgnum"
continue_merge
done
finish_rb_merge
return
;;
show-current-patch)
exec git show REBASE_HEAD --
;;
esac
mkdir -p "$state_dir"
echo "$onto_name" > "$state_dir/onto_name"
write_basic_state
rm -f "$(git rev-parse --git-path REBASE_HEAD)"
msgnum=0
for cmt in $(git rev-list --topo-order --reverse --no-merges "$revisions")
do
msgnum=$(($msgnum + 1))
echo "$cmt" > "$state_dir/cmt.$msgnum"
done
echo 1 >"$state_dir/msgnum"
echo $msgnum >"$state_dir/end"
end=$msgnum
msgnum=1
while test "$msgnum" -le "$end"
do
call_merge "$msgnum"
continue_merge
done
finish_rb_merge
}

View file

@ -17,14 +17,9 @@ test_expect_success 'setup' '
git tag start git tag start
' '
cat >expect <<\EOF
Already applied: 0001 A
Already applied: 0002 B
Committed: 0003 Z
EOF
test_expect_success 'rebase -m' ' test_expect_success 'rebase -m' '
git rebase -m master >report && git rebase -m master >report &&
>expect &&
sed -n -e "/^Already applied: /p" \ sed -n -e "/^Already applied: /p" \
-e "/^Committed: /p" report >actual && -e "/^Committed: /p" report >actual &&
test_cmp expect actual test_cmp expect actual

View file

@ -53,41 +53,6 @@ create_expected_success_interactive () {
EOF EOF
} }
create_expected_success_merge () {
cat >expected <<-EOF
$(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
HEAD is now at $(git rev-parse --short feature-branch) third commit
First, rewinding head to replay your work on top of it...
Merging unrelated-onto-branch with HEAD~1
Merging:
$(git rev-parse --short unrelated-onto-branch) unrelated commit
$(git rev-parse --short feature-branch^) second commit
found 1 common ancestor:
$(git rev-parse --short feature-branch~2) initial commit
[detached HEAD $(git rev-parse --short rebased-feature-branch~1)] second commit
Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:14:13 2005 -0700
2 files changed, 2 insertions(+)
create mode 100644 file1
create mode 100644 file2
Committed: 0001 second commit
Merging unrelated-onto-branch with HEAD~0
Merging:
$(git rev-parse --short rebased-feature-branch~1) second commit
$(git rev-parse --short feature-branch) third commit
found 1 common ancestor:
$(git rev-parse --short feature-branch~1) second commit
[detached HEAD $(git rev-parse --short rebased-feature-branch)] third commit
Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:15:13 2005 -0700
1 file changed, 1 insertion(+)
create mode 100644 file3
Committed: 0002 third commit
All done.
Applied autostash.
EOF
}
create_expected_failure_am () { create_expected_failure_am () {
cat >expected <<-EOF cat >expected <<-EOF
$(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual) $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
@ -112,43 +77,6 @@ create_expected_failure_interactive () {
EOF EOF
} }
create_expected_failure_merge () {
cat >expected <<-EOF
$(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
HEAD is now at $(git rev-parse --short feature-branch) third commit
First, rewinding head to replay your work on top of it...
Merging unrelated-onto-branch with HEAD~1
Merging:
$(git rev-parse --short unrelated-onto-branch) unrelated commit
$(git rev-parse --short feature-branch^) second commit
found 1 common ancestor:
$(git rev-parse --short feature-branch~2) initial commit
[detached HEAD $(git rev-parse --short rebased-feature-branch~1)] second commit
Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:14:13 2005 -0700
2 files changed, 2 insertions(+)
create mode 100644 file1
create mode 100644 file2
Committed: 0001 second commit
Merging unrelated-onto-branch with HEAD~0
Merging:
$(git rev-parse --short rebased-feature-branch~1) second commit
$(git rev-parse --short feature-branch) third commit
found 1 common ancestor:
$(git rev-parse --short feature-branch~1) second commit
[detached HEAD $(git rev-parse --short rebased-feature-branch)] third commit
Author: A U Thor <author@example.com>
Date: Thu Apr 7 15:15:13 2005 -0700
1 file changed, 1 insertion(+)
create mode 100644 file3
Committed: 0002 third commit
All done.
Applying autostash resulted in conflicts.
Your changes are safe in the stash.
You can run "git stash pop" or "git stash drop" at any time.
EOF
}
testrebase () { testrebase () {
type=$1 type=$1
dotest=$2 dotest=$2
@ -177,6 +105,9 @@ testrebase () {
test_expect_success "rebase$type --autostash: check output" ' test_expect_success "rebase$type --autostash: check output" '
test_when_finished git branch -D rebased-feature-branch && test_when_finished git branch -D rebased-feature-branch &&
suffix=${type#\ --} && suffix=${suffix:-am} && suffix=${type#\ --} && suffix=${suffix:-am} &&
if test ${suffix} = "merge"; then
suffix=interactive
fi &&
create_expected_success_$suffix && create_expected_success_$suffix &&
test_i18ncmp expected actual test_i18ncmp expected actual
' '
@ -274,6 +205,9 @@ testrebase () {
test_expect_success "rebase$type: check output with conflicting stash" ' test_expect_success "rebase$type: check output with conflicting stash" '
test_when_finished git branch -D rebased-feature-branch && test_when_finished git branch -D rebased-feature-branch &&
suffix=${type#\ --} && suffix=${suffix:-am} && suffix=${type#\ --} && suffix=${suffix:-am} &&
if test ${suffix} = "merge"; then
suffix=interactive
fi &&
create_expected_failure_$suffix && create_expected_failure_$suffix &&
test_i18ncmp expected actual test_i18ncmp expected actual
' '

View file

@ -111,7 +111,7 @@ test_run_rebase () {
" "
} }
test_run_rebase success '' test_run_rebase success ''
test_run_rebase failure -m test_run_rebase success -m
test_run_rebase success -i test_run_rebase success -i
test_have_prereq !REBASE_P || test_run_rebase success -p test_have_prereq !REBASE_P || test_run_rebase success -p
@ -126,7 +126,7 @@ test_run_rebase () {
" "
} }
test_run_rebase success '' test_run_rebase success ''
test_run_rebase failure -m test_run_rebase success -m
test_run_rebase success -i test_run_rebase success -i
test_have_prereq !REBASE_P || test_run_rebase success -p test_have_prereq !REBASE_P || test_run_rebase success -p
@ -141,7 +141,7 @@ test_run_rebase () {
" "
} }
test_run_rebase success '' test_run_rebase success ''
test_run_rebase failure -m test_run_rebase success -m
test_run_rebase success -i test_run_rebase success -i
test_have_prereq !REBASE_P || test_run_rebase success -p test_have_prereq !REBASE_P || test_run_rebase success -p
@ -284,7 +284,7 @@ test_run_rebase () {
" "
} }
test_run_rebase success '' test_run_rebase success ''
test_run_rebase failure -m test_run_rebase success -m
test_run_rebase success -i test_run_rebase success -i
test_have_prereq !REBASE_P || test_run_rebase success -p test_have_prereq !REBASE_P || test_run_rebase success -p
@ -315,7 +315,7 @@ test_run_rebase () {
" "
} }
test_run_rebase success '' test_run_rebase success ''
test_run_rebase failure -m test_run_rebase success -m
test_run_rebase success -i test_run_rebase success -i
test_have_prereq !REBASE_P || test_run_rebase failure -p test_have_prereq !REBASE_P || test_run_rebase failure -p

View file

@ -180,7 +180,7 @@ test_expect_success 'prompt - interactive rebase' '
' '
test_expect_success 'prompt - rebase merge' ' test_expect_success 'prompt - rebase merge' '
printf " (b2|REBASE-m 1/3)" >expected && printf " (b2|REBASE-i 1/3)" >expected &&
git checkout b2 && git checkout b2 &&
test_when_finished "git checkout master" && test_when_finished "git checkout master" &&
test_must_fail git rebase --merge b1 b2 && test_must_fail git rebase --merge b1 b2 &&