Skip to content

Commit 843d94b

Browse files
committed
Merge branch 'ew/empty-merge-with-dirty-index'
"git merge -s recursive" did not correctly abort when the index is dirty, if the merged tree happened to be the same as the current HEAD, which has been fixed. * ew/empty-merge-with-dirty-index: merge-recursive: avoid incorporating uncommitted changes in a merge move index_has_changes() from builtin/am.c to merge.c for reuse t6044: recursive can silently incorporate dirty changes in a merge
2 parents fa62d03 + b6825b5 commit 843d94b

File tree

5 files changed

+70
-42
lines changed

5 files changed

+70
-42
lines changed

builtin/am.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,43 +1146,6 @@ static void refresh_and_write_cache(void)
11461146
die(_("unable to write index file"));
11471147
}
11481148

1149-
/**
1150-
* Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
1151-
* branch, returns 1 if there are entries in the index, 0 otherwise. If an
1152-
* strbuf is provided, the space-separated list of files that differ will be
1153-
* appended to it.
1154-
*/
1155-
static int index_has_changes(struct strbuf *sb)
1156-
{
1157-
struct object_id head;
1158-
int i;
1159-
1160-
if (!get_oid_tree("HEAD", &head)) {
1161-
struct diff_options opt;
1162-
1163-
diff_setup(&opt);
1164-
opt.flags.exit_with_status = 1;
1165-
if (!sb)
1166-
opt.flags.quick = 1;
1167-
do_diff_cache(&head, &opt);
1168-
diffcore_std(&opt);
1169-
for (i = 0; sb && i < diff_queued_diff.nr; i++) {
1170-
if (i)
1171-
strbuf_addch(sb, ' ');
1172-
strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
1173-
}
1174-
diff_flush(&opt);
1175-
return opt.flags.has_changes != 0;
1176-
} else {
1177-
for (i = 0; sb && i < active_nr; i++) {
1178-
if (i)
1179-
strbuf_addch(sb, ' ');
1180-
strbuf_addstr(sb, active_cache[i]->name);
1181-
}
1182-
return !!active_nr;
1183-
}
1184-
}
1185-
11861149
/**
11871150
* Dies with a user-friendly message on how to proceed after resolving the
11881151
* problem. This message can be overridden with state->resolvemsg.

cache.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,15 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsi
644644
extern int discard_index(struct index_state *);
645645
extern void move_index_extensions(struct index_state *dst, struct index_state *src);
646646
extern int unmerged_index(const struct index_state *);
647+
648+
/**
649+
* Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
650+
* branch, returns 1 if there are entries in the index, 0 otherwise. If an
651+
* strbuf is provided, the space-separated list of files that differ will be
652+
* appended to it.
653+
*/
654+
extern int index_has_changes(struct strbuf *sb);
655+
647656
extern int verify_path(const char *path);
648657
extern int strcmp_offset(const char *s1, const char *s2, size_t *first_change);
649658
extern int index_dir_exists(struct index_state *istate, const char *name, int namelen);

merge-recursive.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,13 @@ int merge_trees(struct merge_options *o,
19521952
}
19531953

19541954
if (oid_eq(&common->object.oid, &merge->object.oid)) {
1955+
struct strbuf sb = STRBUF_INIT;
1956+
1957+
if (index_has_changes(&sb)) {
1958+
err(o, _("Dirty index: cannot merge (dirty: %s)"),
1959+
sb.buf);
1960+
return 0;
1961+
}
19551962
output(o, 0, _("Already up to date!"));
19561963
*result = head;
19571964
return 1;

merge.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "cache.h"
2+
#include "diff.h"
3+
#include "diffcore.h"
24
#include "lockfile.h"
35
#include "commit.h"
46
#include "run-command.h"
@@ -15,6 +17,37 @@ static const char *merge_argument(struct commit *commit)
1517
return EMPTY_TREE_SHA1_HEX;
1618
}
1719

20+
int index_has_changes(struct strbuf *sb)
21+
{
22+
struct object_id head;
23+
int i;
24+
25+
if (!get_oid_tree("HEAD", &head)) {
26+
struct diff_options opt;
27+
28+
diff_setup(&opt);
29+
opt.flags.exit_with_status = 1;
30+
if (!sb)
31+
opt.flags.quick = 1;
32+
do_diff_cache(&head, &opt);
33+
diffcore_std(&opt);
34+
for (i = 0; sb && i < diff_queued_diff.nr; i++) {
35+
if (i)
36+
strbuf_addch(sb, ' ');
37+
strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
38+
}
39+
diff_flush(&opt);
40+
return opt.flags.has_changes != 0;
41+
} else {
42+
for (i = 0; sb && i < active_nr; i++) {
43+
if (i)
44+
strbuf_addch(sb, ' ');
45+
strbuf_addstr(sb, active_cache[i]->name);
46+
}
47+
return !!active_nr;
48+
}
49+
}
50+
1851
int try_merge_command(const char *strategy, size_t xopts_nr,
1952
const char **xopts, struct commit_list *common,
2053
const char *head_arg, struct commit_list *remotes)

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ test_description="merges with unrelated index changes"
66

77
# Testcase for some simple merges
88
# A
9-
# o-----o B
9+
# o-------o B
1010
# \
11-
# \---o C
11+
# \-----o C
1212
# \
13-
# \-o D
13+
# \---o D
1414
# \
15-
# o E
15+
# \-o E
16+
# \
17+
# o F
1618
# Commit A: some file a
1719
# Commit B: adds file b, modifies end of a
1820
# Commit C: adds file c
1921
# Commit D: adds file d, modifies beginning of a
2022
# Commit E: renames a->subdir/a, adds subdir/e
23+
# Commit F: empty commit
2124

2225
test_expect_success 'setup trivial merges' '
2326
test_seq 1 10 >a &&
@@ -29,6 +32,7 @@ test_expect_success 'setup trivial merges' '
2932
git branch C &&
3033
git branch D &&
3134
git branch E &&
35+
git branch F &&
3236
3337
git checkout B &&
3438
echo b >b &&
@@ -52,7 +56,10 @@ test_expect_success 'setup trivial merges' '
5256
git mv a subdir/a &&
5357
echo e >subdir/e &&
5458
git add subdir &&
55-
test_tick && git commit -m E
59+
test_tick && git commit -m E &&
60+
61+
git checkout F &&
62+
test_tick && git commit --allow-empty -m F
5663
'
5764

5865
test_expect_success 'ff update' '
@@ -105,6 +112,15 @@ test_expect_success 'recursive' '
105112
test_must_fail git merge -s recursive C^0
106113
'
107114

115+
test_expect_success 'recursive, when merge branch matches merge base' '
116+
git reset --hard &&
117+
git checkout B^0 &&
118+
119+
touch random_file && git add random_file &&
120+
121+
test_must_fail git merge -s recursive F^0
122+
'
123+
108124
test_expect_success 'octopus, unrelated file touched' '
109125
git reset --hard &&
110126
git checkout B^0 &&

0 commit comments

Comments
 (0)