merge: fix numerus bugs around "trivial merge" area

The "trivial merge" codepath wants to optimize itself by making an
internal call to the read-tree machinery, but it does not read the index
before doing so, and the codepath is never exercised.  Incidentally, this
failure to read the index upfront means that the safety to refuse doing
anything when the index is unmerged does not kick in, either.

These two problem are fixed by using read_cache_unmerged() that does read
the index before checking if it is unmerged at the beginning of
cmd_merge().

The primary logic of the merge, however, assumes that the process never
reads the index in-core, and the call to write_cache_as_tree() it makes
from write_tree_trivial() will always read from the on-disk index that is
prepared the strategy back-ends.  This assumption is now broken by the
above fix.  To fix this issue, we now call discard_cache() before calling
write_tree_trivial() when it wants to write the on-disk index as a tree.

When multiple strategies are tried, their results are evaluated by reading
the resulting index and inspecting it.  The codepath needs to make a call
to read_cache() for each successful strategy, and for that to work, they
need to discard_cache() the one read by the previous round.

Also the "trivial merge" forgot that the current commit is one of the
parents of the resulting commit.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2008-08-23 12:56:57 -07:00
parent 913e0e99b6
commit 446247db78
4 changed files with 32 additions and 8 deletions

View File

@ -564,8 +564,6 @@ static int checkout_fast_forward(unsigned char *head, unsigned char *remote)
struct dir_struct dir; struct dir_struct dir;
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file)); struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
if (read_cache_unmerged())
die("you need to resolve your current index first");
refresh_cache(REFRESH_QUIET); refresh_cache(REFRESH_QUIET);
fd = hold_locked_index(lock_file, 1); fd = hold_locked_index(lock_file, 1);
@ -651,13 +649,15 @@ static void add_strategies(const char *string, unsigned attr)
static int merge_trivial(void) static int merge_trivial(void)
{ {
unsigned char result_tree[20], result_commit[20]; unsigned char result_tree[20], result_commit[20];
struct commit_list parent; struct commit_list *parent = xmalloc(sizeof(struct commit_list *));
write_tree_trivial(result_tree); write_tree_trivial(result_tree);
printf("Wonderful.\n"); printf("Wonderful.\n");
parent.item = remoteheads->item; parent->item = lookup_commit(head);
parent.next = NULL; parent->next = xmalloc(sizeof(struct commit_list *));
commit_tree(merge_msg.buf, result_tree, &parent, result_commit); parent->next->item = remoteheads->item;
parent->next->next = NULL;
commit_tree(merge_msg.buf, result_tree, parent, result_commit);
finish(result_commit, "In-index merge"); finish(result_commit, "In-index merge");
drop_save(); drop_save();
return 0; return 0;
@ -743,6 +743,7 @@ static int evaluate_result(void)
int cnt = 0; int cnt = 0;
struct rev_info rev; struct rev_info rev;
discard_cache();
if (read_cache() < 0) if (read_cache() < 0)
die("failed to read the cache"); die("failed to read the cache");
@ -776,7 +777,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
struct commit_list **remotes = &remoteheads; struct commit_list **remotes = &remoteheads;
setup_work_tree(); setup_work_tree();
if (unmerged_cache()) if (read_cache_unmerged())
die("You are in the middle of a conflicted merge."); die("You are in the middle of a conflicted merge.");
/* /*
@ -1073,6 +1074,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
} }
/* Automerge succeeded. */ /* Automerge succeeded. */
discard_cache();
write_tree_trivial(result_tree); write_tree_trivial(result_tree);
automerge_was_ok = 1; automerge_was_ok = 1;
break; break;

View File

@ -269,6 +269,17 @@ test_expect_success 'merge-recursive result' '
' '
test_expect_success 'fail if the index has unresolved entries' '
rm -fr [abcd] &&
git checkout -f "$c1" &&
test_must_fail git merge "$c5" &&
test_must_fail git merge "$c5" 2> out &&
grep "You are in the middle of a conflicted merge" out
'
test_expect_success 'merge-recursive remove conflict' ' test_expect_success 'merge-recursive remove conflict' '
rm -fr [abcd] && rm -fr [abcd] &&

View File

@ -498,4 +498,13 @@ test_expect_success 'merge fast-forward in a dirty tree' '
test_debug 'gitk --all' test_debug 'gitk --all'
test_expect_success 'in-index merge' '
git reset --hard c0 &&
git merge --no-ff -s resolve c1 > out &&
grep "Wonderful." out &&
verify_parents $c0 $c1
'
test_debug 'gitk --all'
test_done test_done

View File

@ -36,7 +36,9 @@ test_expect_success 'merge c1 to c2' '
git diff --exit-code && git diff --exit-code &&
test -f c0.c && test -f c0.c &&
test -f c1.c && test -f c1.c &&
test -f c2.c test -f c2.c &&
test 3 = $(git ls-tree -r HEAD | wc -l) &&
test 3 = $(git ls-files | wc -l)
' '
test_expect_success 'merge c2 to c3 (fails)' ' test_expect_success 'merge c2 to c3 (fails)' '