merge-recursive: fix assumption that head tree being merged is HEAD

`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>

Elijah Newren committed Jun 30, 2018 at 18:25 UTC e1f8694f3394caf3d3cd57c6c7593f0b0cdb1f9e
5 files changed +21 -13
builtin/am.c
+4 -3
@@ -1763,7 +1763,7 @@ static void am_run(struct am_state *state, int resume)
1763
1764 refresh_and_write_cache();
1765
1766 - if (index_has_changes(&the_index, &sb)) {
1766 + if (index_has_changes(&the_index, NULL, &sb)) {
1767 write_state_bool(state, "dirtyindex", 1);
1768 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
1769 }
@@ -1820,7 +1820,8 @@ static void am_run(struct am_state *state, int resume)
1820 * Applying the patch to an earlier tree and merging
1821 * the result may have produced the same tree as ours.
1822 */
1823 - if (!apply_status && !index_has_changes(&the_index, NULL)) {
1823 + if (!apply_status &&
1824 + !index_has_changes(&the_index, NULL, NULL)) {
1825 say(state, stdout, _("No changes -- Patch already applied."));
1826 goto next;
1827 }
@@ -1878,7 +1879,7 @@ static void am_resolve(struct am_state *state)
1879
1880 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1881
1881 - if (!index_has_changes(&the_index, NULL)) {
1882 + if (!index_has_changes(&the_index, NULL, NULL)) {
1883 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1884 "If there is nothing left to stage, chances are that something else\n"
1885 "already introduced the same changes; you might want to skip this patch."));
cache.h
+7 -4
@@ -218,6 +218,7 @@ struct cache_entry {
218 /* Forward structure decls */
219 struct pathspec;
220 struct child_process;
221 +struct tree;
222
223 /*
224 * 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
628 extern int unmerged_index(const struct index_state *);
629
630 /**
630 - * Returns 1 if istate differs from HEAD, 0 otherwise. When on an unborn
631 - * branch, returns 1 if there are entries in istate, 0 otherwise. If an
632 - * strbuf is provided, the space-separated list of files that differ will
633 - * be appended to it.
631 + * Returns 1 if istate differs from tree, 0 otherwise. If tree is NULL,
632 + * compares istate to HEAD. If tree is NULL and on an unborn branch,
633 + * returns 1 if there are entries in istate, 0 otherwise. If an strbuf is
634 + * provided, the space-separated list of files that differ will be appended
635 + * to it.
636 */
637 extern int index_has_changes(const struct index_state *istate,
638 + struct tree *tree,
639 struct strbuf *sb);
640
641 extern int verify_path(const char *path, unsigned mode);
merge-recursive.c
+1 -1
@@ -1983,7 +1983,7 @@ int merge_trees(struct merge_options *o,
1983 if (oid_eq(&common->object.oid, &merge->object.oid)) {
1984 struct strbuf sb = STRBUF_INIT;
1985
1986 - if (!o->call_depth && index_has_changes(&the_index, &sb)) {
1986 + if (!o->call_depth && index_has_changes(&the_index, head, &sb)) {
1987 err(o, _("Your local changes to the following files would be overwritten by merge:\n %s"),
1988 sb.buf);
1989 return -1;
read-cache.c
+8 -4
@@ -1986,22 +1986,26 @@ int unmerged_index(const struct index_state *istate)
1986 return 0;
1987 }
1988
1989 -int index_has_changes(const struct index_state *istate, struct strbuf *sb)
1989 +int index_has_changes(const struct index_state *istate,
1990 + struct tree *tree,
1991 + struct strbuf *sb)
1992 {
1991 - struct object_id head;
1993 + struct object_id cmp;
1994 int i;
1995
1996 if (istate != &the_index) {
1997 BUG("index_has_changes cannot yet accept istate != &the_index; do_diff_cache needs updating first.");
1998 }
1997 - if (!get_oid_tree("HEAD", &head)) {
1999 + if (tree)
2000 + cmp = tree->object.oid;
2001 + if (tree || !get_oid_tree("HEAD", &cmp)) {
2002 struct diff_options opt;
2003
2004 diff_setup(&opt);
2005 opt.flags.exit_with_status = 1;
2006 if (!sb)
2007 opt.flags.quick = 1;
2004 - do_diff_cache(&head, &opt);
2008 + do_diff_cache(&cmp, &opt);
2009 diffcore_std(&opt);
2010 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
2011 if (i)
t/t6044-merge-unrelated-index-changes.sh
+1 -1
@@ -126,7 +126,7 @@ test_expect_success 'recursive, when merge branch matches merge base' '
126 test_path_is_missing .git/MERGE_HEAD
127 '
128
129 -test_expect_failure 'merge-recursive, when index==head but head!=HEAD' '
129 +test_expect_success 'merge-recursive, when index==head but head!=HEAD' '
130 git reset --hard &&
131 git checkout C^0 &&
132