From eab3f2850e7cc63a8087fbeae608a1b72ea70c7b Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Thu, 21 Dec 2017 11:19:05 -0800 Subject: t6044: recursive can silently incorporate dirty changes in a merge The recursive merge strategy has some special handling when the tree for the merge branch exactly matches the merge base, but that code path is missing checks for the index having changes relative to HEAD. Add a testcase covering this scenario. Reported-by: Andreas Krey Signed-off-by: Elijah Newren Signed-off-by: Junio C Hamano --- t/t6044-merge-unrelated-index-changes.sh | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/t/t6044-merge-unrelated-index-changes.sh b/t/t6044-merge-unrelated-index-changes.sh index 01023486c5..5e472be92b 100755 --- a/t/t6044-merge-unrelated-index-changes.sh +++ b/t/t6044-merge-unrelated-index-changes.sh @@ -6,18 +6,21 @@ test_description="merges with unrelated index changes" # Testcase for some simple merges # A -# o-----o B +# o-------o B # \ -# \---o C +# \-----o C # \ -# \-o D +# \---o D # \ -# o E +# \-o E +# \ +# o F # Commit A: some file a # Commit B: adds file b, modifies end of a # Commit C: adds file c # Commit D: adds file d, modifies beginning of a # Commit E: renames a->subdir/a, adds subdir/e +# Commit F: empty commit test_expect_success 'setup trivial merges' ' test_seq 1 10 >a && @@ -29,6 +32,7 @@ test_expect_success 'setup trivial merges' ' git branch C && git branch D && git branch E && + git branch F && git checkout B && echo b >b && @@ -52,7 +56,10 @@ test_expect_success 'setup trivial merges' ' git mv a subdir/a && echo e >subdir/e && git add subdir && - test_tick && git commit -m E + test_tick && git commit -m E && + + git checkout F && + test_tick && git commit --allow-empty -m F ' test_expect_success 'ff update' ' @@ -105,6 +112,15 @@ test_expect_success 'recursive' ' test_must_fail git merge -s recursive C^0 ' +test_expect_failure 'recursive, when merge branch matches merge base' ' + git reset --hard && + git checkout B^0 && + + touch random_file && git add random_file && + + test_must_fail git merge -s recursive F^0 +' + test_expect_success 'octopus, unrelated file touched' ' git reset --hard && git checkout B^0 && -- cgit v1.2.1 From b101793c431b858e49b909bb309a87145fb7348c Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Thu, 21 Dec 2017 11:19:06 -0800 Subject: move index_has_changes() from builtin/am.c to merge.c for reuse index_has_changes() is a function we want to reuse outside of just am, making it also available for merge-recursive and merge-ort. Signed-off-by: Elijah Newren Signed-off-by: Junio C Hamano --- builtin/am.c | 37 ------------------------------------- cache.h | 9 +++++++++ merge.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index d7513f5375..71619f01aa 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -1142,43 +1142,6 @@ static void refresh_and_write_cache(void) die(_("unable to write index file")); } -/** - * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn - * branch, returns 1 if there are entries in the index, 0 otherwise. If an - * strbuf is provided, the space-separated list of files that differ will be - * appended to it. - */ -static int index_has_changes(struct strbuf *sb) -{ - struct object_id head; - int i; - - if (!get_oid_tree("HEAD", &head)) { - struct diff_options opt; - - diff_setup(&opt); - DIFF_OPT_SET(&opt, EXIT_WITH_STATUS); - if (!sb) - DIFF_OPT_SET(&opt, QUICK); - do_diff_cache(&head, &opt); - diffcore_std(&opt); - for (i = 0; sb && i < diff_queued_diff.nr; i++) { - if (i) - strbuf_addch(sb, ' '); - strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path); - } - diff_flush(&opt); - return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0; - } else { - for (i = 0; sb && i < active_nr; i++) { - if (i) - strbuf_addch(sb, ' '); - strbuf_addstr(sb, active_cache[i]->name); - } - return !!active_nr; - } -} - /** * Dies with a user-friendly message on how to proceed after resolving the * problem. This message can be overridden with state->resolvemsg. diff --git a/cache.h b/cache.h index 6440e2bf21..caf610d3b3 100644 --- a/cache.h +++ b/cache.h @@ -608,6 +608,15 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsi extern int discard_index(struct index_state *); extern void move_index_extensions(struct index_state *dst, struct index_state *src); extern int unmerged_index(const struct index_state *); + +/** + * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn + * branch, returns 1 if there are entries in the index, 0 otherwise. If an + * strbuf is provided, the space-separated list of files that differ will be + * appended to it. + */ +extern int index_has_changes(struct strbuf *sb); + extern int verify_path(const char *path); extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change); extern int index_dir_exists(struct index_state *istate, const char *name, int namelen); diff --git a/merge.c b/merge.c index 1d441ad942..b0714bc85e 100644 --- a/merge.c +++ b/merge.c @@ -1,4 +1,6 @@ #include "cache.h" +#include "diff.h" +#include "diffcore.h" #include "lockfile.h" #include "commit.h" #include "run-command.h" @@ -15,6 +17,37 @@ static const char *merge_argument(struct commit *commit) return EMPTY_TREE_SHA1_HEX; } +int index_has_changes(struct strbuf *sb) +{ + struct object_id head; + int i; + + if (!get_oid_tree("HEAD", &head)) { + struct diff_options opt; + + diff_setup(&opt); + DIFF_OPT_SET(&opt, EXIT_WITH_STATUS); + if (!sb) + DIFF_OPT_SET(&opt, QUICK); + do_diff_cache(&head, &opt); + diffcore_std(&opt); + for (i = 0; sb && i < diff_queued_diff.nr; i++) { + if (i) + strbuf_addch(sb, ' '); + strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path); + } + diff_flush(&opt); + return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0; + } else { + for (i = 0; sb && i < active_nr; i++) { + if (i) + strbuf_addch(sb, ' '); + strbuf_addstr(sb, active_cache[i]->name); + } + return !!active_nr; + } +} + int try_merge_command(const char *strategy, size_t xopts_nr, const char **xopts, struct commit_list *common, const char *head_arg, struct commit_list *remotes) -- cgit v1.2.1 From 65170c07d466b18364e0d2b6a360900c073b600f Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Thu, 21 Dec 2017 11:19:07 -0800 Subject: merge-recursive: avoid incorporating uncommitted changes in a merge builtin/merge.c contains this important requirement for merge strategies: /* * At this point, we need a real merge. No matter what strategy * we use, it would operate on the index, possibly affecting the * working tree, and when resolved cleanly, have the desired * tree in the index -- this means that the index must be in * sync with the head commit. The strategies are responsible * to ensure this. */ merge-recursive does not do this check directly, instead it relies on unpack_trees() to do it. However, merge_trees() has a special check for the merge branch exactly matching the merge base; when it detects that situation, it returns early without calling unpack_trees(), because it knows that the HEAD commit already has the correct result. Unfortunately, it didn't check that the index matched HEAD, so after it returned, the outer logic ended up creating a merge commit that included something other than HEAD. Signed-off-by: Elijah Newren Signed-off-by: Junio C Hamano --- merge-recursive.c | 7 +++++++ t/t6044-merge-unrelated-index-changes.sh | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/merge-recursive.c b/merge-recursive.c index 9fb0b9f8fd..be01f528cb 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -1952,6 +1952,13 @@ int merge_trees(struct merge_options *o, } if (oid_eq(&common->object.oid, &merge->object.oid)) { + struct strbuf sb = STRBUF_INIT; + + if (index_has_changes(&sb)) { + err(o, _("Dirty index: cannot merge (dirty: %s)"), + sb.buf); + return 0; + } output(o, 0, _("Already up to date!")); *result = head; return 1; diff --git a/t/t6044-merge-unrelated-index-changes.sh b/t/t6044-merge-unrelated-index-changes.sh index 5e472be92b..23b86fb977 100755 --- a/t/t6044-merge-unrelated-index-changes.sh +++ b/t/t6044-merge-unrelated-index-changes.sh @@ -112,7 +112,7 @@ test_expect_success 'recursive' ' test_must_fail git merge -s recursive C^0 ' -test_expect_failure 'recursive, when merge branch matches merge base' ' +test_expect_success 'recursive, when merge branch matches merge base' ' git reset --hard && git checkout B^0 && -- cgit v1.2.1