merge-recursive: fix assumption that head tree being merged is HEAD
authorElijah Newren <newren@gmail.com>
Sun, 1 Jul 2018 01:25:00 +0000 (18:25 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 11 Jul 2018 16:38:36 +0000 (09:38 -0700)
`git merge-recursive` does a three-way merge between user-specified trees
base, head, and remote. Since the user is allowed to specify head, we can
not necesarily assume that head == HEAD.

Modify index_has_changes() to take an extra argument specifying the tree
to compare against. If NULL, it will compare to HEAD. We then use this
from merge-recursive to make sure we compare to the user-specified head.

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/am.c
cache.h
merge-recursive.c
read-cache.c
t/t6044-merge-unrelated-index-changes.sh
index 2d800568ac6e63e6542bdb53780fcb7640b0bd49..3d827d1a93bcc6d296ac7da112ce781a5fa082b5 100644 (file)
@@ -1763,7 +1763,7 @@ static void am_run(struct am_state *state, int resume)
 
        refresh_and_write_cache();
 
-       if (index_has_changes(&the_index, &sb)) {
+       if (index_has_changes(&the_index, NULL, &sb)) {
                write_state_bool(state, "dirtyindex", 1);
                die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
        }
@@ -1820,7 +1820,8 @@ static void am_run(struct am_state *state, int resume)
                         * Applying the patch to an earlier tree and merging
                         * the result may have produced the same tree as ours.
                         */
-                       if (!apply_status && !index_has_changes(&the_index, NULL)) {
+                       if (!apply_status &&
+                           !index_has_changes(&the_index, NULL, NULL)) {
                                say(state, stdout, _("No changes -- Patch already applied."));
                                goto next;
                        }
@@ -1878,7 +1879,7 @@ static void am_resolve(struct am_state *state)
 
        say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
 
-       if (!index_has_changes(&the_index, NULL)) {
+       if (!index_has_changes(&the_index, NULL, NULL)) {
                printf_ln(_("No changes - did you forget to use 'git add'?\n"
                        "If there is nothing left to stage, chances are that something else\n"
                        "already introduced the same changes; you might want to skip this patch."));
diff --git a/cache.h b/cache.h
index 169c809c6c4321f7a18aefb92c06a050d45afcdf..fad10c3bfe564caec98d53d1406eacb4ad6bf35a 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -218,6 +218,7 @@ struct cache_entry {
 /* Forward structure decls */
 struct pathspec;
 struct child_process;
+struct tree;
 
 /*
  * Copy the sha1 and stat state of a cache entry from one to
@@ -627,12 +628,14 @@ extern void move_index_extensions(struct index_state *dst, struct index_state *s
 extern int unmerged_index(const struct index_state *);
 
 /**
- * Returns 1 if istate differs from HEAD, 0 otherwise.  When on an unborn
- * branch, returns 1 if there are entries in istate, 0 otherwise.  If an
- * strbuf is provided, the space-separated list of files that differ will
- * be appended to it.
+ * Returns 1 if istate differs from tree, 0 otherwise.  If tree is NULL,
+ * compares istate to HEAD.  If tree is NULL and on an unborn branch,
+ * returns 1 if there are entries in istate, 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(const struct index_state *istate,
+                            struct tree *tree,
                             struct strbuf *sb);
 
 extern int verify_path(const char *path, unsigned mode);
index 2fb1bdb0107fb0b38aec873e8b75eaa95b68ba50..171587bc7aab23bd7576ca7a5c2ad7d5ef42a942 100644 (file)
@@ -1983,7 +1983,7 @@ int merge_trees(struct merge_options *o,
        if (oid_eq(&common->object.oid, &merge->object.oid)) {
                struct strbuf sb = STRBUF_INIT;
 
-               if (!o->call_depth && index_has_changes(&the_index, &sb)) {
+               if (!o->call_depth && index_has_changes(&the_index, head, &sb)) {
                        err(o, _("Your local changes to the following files would be overwritten by merge:\n  %s"),
                            sb.buf);
                        return -1;
index f333a517f7d674e95b42ccab9808f276438e64b9..639c1fffa0635e8829ad76d74407b06447e42b1f 100644 (file)
@@ -1986,22 +1986,26 @@ int unmerged_index(const struct index_state *istate)
        return 0;
 }
 
-int index_has_changes(const struct index_state *istate, struct strbuf *sb)
+int index_has_changes(const struct index_state *istate,
+                     struct tree *tree,
+                     struct strbuf *sb)
 {
-       struct object_id head;
+       struct object_id cmp;
        int i;
 
        if (istate != &the_index) {
                BUG("index_has_changes cannot yet accept istate != &the_index; do_diff_cache needs updating first.");
        }
-       if (!get_oid_tree("HEAD", &head)) {
+       if (tree)
+               cmp = tree->object.oid;
+       if (tree || !get_oid_tree("HEAD", &cmp)) {
                struct diff_options opt;
 
                diff_setup(&opt);
                opt.flags.exit_with_status = 1;
                if (!sb)
                        opt.flags.quick = 1;
-               do_diff_cache(&head, &opt);
+               do_diff_cache(&cmp, &opt);
                diffcore_std(&opt);
                for (i = 0; sb && i < diff_queued_diff.nr; i++) {
                        if (i)
index d55f1649f0f0f2f3a524bfb97c053ea50045b23b..ef8a502baf1d056443cddfa237ab7ab2a6ae35f9 100755 (executable)
@@ -126,7 +126,7 @@ test_expect_success 'recursive, when merge branch matches merge base' '
        test_path_is_missing .git/MERGE_HEAD
 '
 
-test_expect_failure 'merge-recursive, when index==head but head!=HEAD' '
+test_expect_success 'merge-recursive, when index==head but head!=HEAD' '
        git reset --hard &&
        git checkout C^0 &&