Skip to content

Commit e1f8694

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9270239 commit e1f8694

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

builtin/am.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ static void am_run(struct am_state *state, int resume)
17631763

17641764
refresh_and_write_cache();
17651765

1766-
if (index_has_changes(&the_index, &sb)) {
1766+
if (index_has_changes(&the_index, NULL, &sb)) {
17671767
write_state_bool(state, "dirtyindex", 1);
17681768
die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
17691769
}
@@ -1820,7 +1820,8 @@ static void am_run(struct am_state *state, int resume)
18201820
* Applying the patch to an earlier tree and merging
18211821
* the result may have produced the same tree as ours.
18221822
*/
1823-
if (!apply_status && !index_has_changes(&the_index, NULL)) {
1823+
if (!apply_status &&
1824+
!index_has_changes(&the_index, NULL, NULL)) {
18241825
say(state, stdout, _("No changes -- Patch already applied."));
18251826
goto next;
18261827
}
@@ -1878,7 +1879,7 @@ static void am_resolve(struct am_state *state)
18781879

18791880
say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
18801881

1881-
if (!index_has_changes(&the_index, NULL)) {
1882+
if (!index_has_changes(&the_index, NULL, NULL)) {
18821883
printf_ln(_("No changes - did you forget to use 'git add'?\n"
18831884
"If there is nothing left to stage, chances are that something else\n"
18841885
"already introduced the same changes; you might want to skip this patch."));

cache.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ struct cache_entry {
218218
/* Forward structure decls */
219219
struct pathspec;
220220
struct child_process;
221+
struct tree;
221222

222223
/*
223224
* 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
627628
extern int unmerged_index(const struct index_state *);
628629

629630
/**
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.
634636
*/
635637
extern int index_has_changes(const struct index_state *istate,
638+
struct tree *tree,
636639
struct strbuf *sb);
637640

638641
extern int verify_path(const char *path, unsigned mode);

merge-recursive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,7 @@ int merge_trees(struct merge_options *o,
19831983
if (oid_eq(&common->object.oid, &merge->object.oid)) {
19841984
struct strbuf sb = STRBUF_INIT;
19851985

1986-
if (!o->call_depth && index_has_changes(&the_index, &sb)) {
1986+
if (!o->call_depth && index_has_changes(&the_index, head, &sb)) {
19871987
err(o, _("Your local changes to the following files would be overwritten by merge:\n %s"),
19881988
sb.buf);
19891989
return -1;

read-cache.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,22 +1986,26 @@ int unmerged_index(const struct index_state *istate)
19861986
return 0;
19871987
}
19881988

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)
19901992
{
1991-
struct object_id head;
1993+
struct object_id cmp;
19921994
int i;
19931995

19941996
if (istate != &the_index) {
19951997
BUG("index_has_changes cannot yet accept istate != &the_index; do_diff_cache needs updating first.");
19961998
}
1997-
if (!get_oid_tree("HEAD", &head)) {
1999+
if (tree)
2000+
cmp = tree->object.oid;
2001+
if (tree || !get_oid_tree("HEAD", &cmp)) {
19982002
struct diff_options opt;
19992003

20002004
diff_setup(&opt);
20012005
opt.flags.exit_with_status = 1;
20022006
if (!sb)
20032007
opt.flags.quick = 1;
2004-
do_diff_cache(&head, &opt);
2008+
do_diff_cache(&cmp, &opt);
20052009
diffcore_std(&opt);
20062010
for (i = 0; sb && i < diff_queued_diff.nr; i++) {
20072011
if (i)

t/t6044-merge-unrelated-index-changes.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ test_expect_success 'recursive, when merge branch matches merge base' '
126126
test_path_is_missing .git/MERGE_HEAD
127127
'
128128

129-
test_expect_failure 'merge-recursive, when index==head but head!=HEAD' '
129+
test_expect_success 'merge-recursive, when index==head but head!=HEAD' '
130130
git reset --hard &&
131131
git checkout C^0 &&
132132

0 commit comments

Comments
 (0)