Skip to content

Commit 9822175

Browse files
newrengitster
authored andcommitted
Ensure index matches head before invoking merge machinery, round N
This is the bug that just won't die; there always seems to be another form of it somewhere. See the commit message of 55f39cf ("merge: fix misleading pre-merge check documentation", 2018-06-30) for a more detailed explanation), but in short: <quick summary> builtin/merge.c contains this important requirement for merge strategies: ...the index must be in sync with the head commit. The strategies are responsible to ensure this. This condition is important to enforce because there are two likely failure cases when the index isn't in sync with the head commit: * we silently throw away changes the user had staged before the merge * we accidentally (and silently) include changes in the merge that were not part of either of the branches/trees being merged Discarding users' work and mis-merging are both bad outcomes, especially when done silently, so naturally this rule was stated sternly -- but, unfortunately totally ignored in practice unless and until actual bugs were found. But, fear not: the bugs from this were fixed in commit ee6566e ("[PATCH] Rewrite read-tree", 2005-09-05) through a rewrite of read-tree (again, commit 55f39cf has a more detailed explanation of how this affected merge). And it was fixed again in commit 160252f ("git-merge-ours: make sure our index matches HEAD", 2005-11-03) ...and it was fixed again in commit 3ec62ad ("merge-octopus: abort if index does not match HEAD", 2016-04-09) ...and again in commit 65170c0 ("merge-recursive: avoid incorporating uncommitted changes in a merge", 2017-12-21) ...and again in commit eddd1a4 ("merge-recursive: enforce rule that index matches head before merging", 2018-06-30) ...with multiple testcases added to the testsuite that could be enumerated in even more commits. Then, finally, in a patch in the same series as the last fix above, the documentation about this requirement was fixed in commit 55f39cf ("merge: fix misleading pre-merge check documentation", 2018-06-30), and we all lived happily ever after... </quick summary> Unfortunately, "ever after" apparently denotes a limited time and it expired today. The merge-recursive rule to enforce that index matches head was at the beginning of merge_trees() and would only trigger when opt->call_depth was 0. Since merge_recursive() doesn't call merge_trees() until after returning from recursing, this meant that the check wasn't triggered by merge_recursive() until it had first finished all the intermediate merges to create virtual merge bases. That is a potentially HUGE amount of computation (and writing of intermediate merge results into the .git/objects directory) before it errors out and says, in effect, "Sorry, I can't do any merging because you have some local changes that would be overwritten." Trying to enforce that all of merge_trees(), merge_recursive(), and merge_recursive_generic() checked the index == head condition earlier resulted in a bunch of broken tests. It turns out that merge_recursive() has code to drop and reload the cache while recursing to create intermediate virtual merge bases, but unfortunately that code runs even when no recursion is necessary. This unconditional dropping and reloading of the cache masked a few bugs: * builtin/merge-recursive.c: didn't even bother loading the index. * builtin/stash.c: feels like a fake 'builtin' because it repeatedly invokes git subprocesses all over the place, mixed with other operations. In particular, invoking "git reset" will reset the index on disk, but the parent process that invoked it won't automatically have its in-memory index updated. * t3030-merge-recursive.h: this test has always been broken in that it didn't make sure to make index match head before running. But, it didn't care about the index or even the merge result, just the verbose output while running. While commit eddd1a4 ("merge-recursive: enforce rule that index matches head before merging", 2018-06-30) should have uncovered this broken test, it used a test_must_fail wrapper around the merge-recursive call because it was known that the merge resulted in a rename/rename conflict. Thus, that fix only made this test fail for a different reason, and since the index == head check didn't happen until after coming all the way back out of the recursion, the testcase had enough information to pass the one check that it did perform. So, load the index in builtin/merge-recursive.c, reload the in-memory index in builtin/stash.c, and modify the t3030 testcase to correctly setup the index and make sure that the test fails in the expected way (meaning it reports a rename/rename conflict). This makes sure that all callers actually make the index match head. The next commit will then enforce the condition that index matches head earlier so this problem doesn't return in the future. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 10f751c commit 9822175

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

builtin/merge-recursive.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "cache.h"
12
#include "builtin.h"
23
#include "commit.h"
34
#include "tag.h"
@@ -63,6 +64,9 @@ int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
6364
if (argc - i != 3) /* "--" "<head>" "<remote>" */
6465
die(_("not handling anything other than two heads merge."));
6566

67+
if (repo_read_index_unmerged(the_repository))
68+
die_resolve_conflict("merge");
69+
6670
o.branch1 = argv[++i];
6771
o.branch2 = argv[++i];
6872

builtin/stash.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
427427
return error(_("could not save index tree"));
428428

429429
reset_head();
430+
discard_cache();
431+
read_cache();
430432
}
431433
}
432434

t/t3030-merge-recursive.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,15 +667,22 @@ test_expect_success 'merging with triple rename across D/F conflict' '
667667
test_expect_success 'merge-recursive remembers the names of all base trees' '
668668
git reset --hard HEAD &&
669669
670+
# make the index match $c1 so that merge-recursive below does not
671+
# fail early
672+
git diff --binary HEAD $c1 -- | git apply --cached &&
673+
670674
# more trees than static slots used by oid_to_hex()
671675
for commit in $c0 $c2 $c4 $c5 $c6 $c7
672676
do
673677
git rev-parse "$commit^{tree}"
674678
done >trees &&
675679
676-
# ignore the return code -- it only fails because the input is weird
680+
# ignore the return code; it only fails because the input is weird...
677681
test_must_fail git -c merge.verbosity=5 merge-recursive $(cat trees) -- $c1 $c3 >out &&
678682
683+
# ...but make sure it fails in the expected way
684+
test_i18ngrep CONFLICT.*rename/rename out &&
685+
679686
# merge-recursive prints in reverse order, but we do not care
680687
sort <trees >expect &&
681688
sed -n "s/^virtual //p" out | sort >actual &&

0 commit comments

Comments
 (0)