merge-tree: handle directory/empty conflict correctly
git-merge-tree causes a null pointer dereference when a directory entry exists in only one or two of the three trees being compared with no corresponding entry in the other tree(s). When this happens, we want to handle the entry as a directory and not attempt to mark it as a file merge. Do this by setting the entries bit in the directory mask when the entry is missing or when it is a directory, only performing the file comparison when we know that a file entry exists. Reported-by: Andreas Jacobsen <andreas@andreasjacobsen.com> Signed-off-by: John Keeping <john@keeping.me.uk> Tested-by: Andreas Jacobsen <andreas@andreasjacobsen.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
John Keeping committed
May 6, 2013 at 16:20 UTC
94883b4302118fe6ea50028d02bb453c9af38b66
2 files changed
+56
-1
builtin/merge-tree.c
+5
-1
@@ -251,7 +251,11 @@ static void unresolved(const struct traverse_info *info, struct name_entry n[3])
251
252
for (i = 0; i < 3; i++) {
253
mask |= (1 << i);
254
- if (n[i].mode && S_ISDIR(n[i].mode))
254
+ /*
255
+ * Treat missing entries as directories so that we return
256
+ * after unresolved_directory has handled this.
257
+ */
258
+ if (!n[i].mode || S_ISDIR(n[i].mode))
259
dirmask |= (1 << i);
260
}
261
t/t4300-merge-tree.sh
+51
@@ -259,6 +259,57 @@ EXPECTED
259
test_cmp expected actual
260
'
261
262
+test_expect_success 'tree add A, B (same)' '
263
+ cat >expect <<-\EOF &&
264
+ EOF
265
+ git reset --hard initial &&
266
+ mkdir sub &&
267
+ test_commit "add sub/file" "sub/file" "file" add-tree-A &&
268
+ git merge-tree initial add-tree-A add-tree-A >actual &&
269
+ test_cmp expect actual
270
+'
271
+
272
+test_expect_success 'tree add A, B (different)' '
273
+ cat >expect <<-\EOF &&
274
+ added in both
275
+ our 100644 43d5a8ed6ef6c00ff775008633f95787d088285d sub/file
276
+ their 100644 ba629238ca89489f2b350e196ca445e09d8bb834 sub/file
277
+ @@ -1 +1,5 @@
278
+ +<<<<<<< .our
279
+ AAA
280
+ +=======
281
+ +BBB
282
+ +>>>>>>> .their
283
+ EOF
284
+ git reset --hard initial &&
285
+ mkdir sub &&
286
+ test_commit "add sub/file" "sub/file" "AAA" add-tree-a-b-A &&
287
+ git reset --hard initial &&
288
+ mkdir sub &&
289
+ test_commit "add sub/file" "sub/file" "BBB" add-tree-a-b-B &&
290
+ git merge-tree initial add-tree-a-b-A add-tree-a-b-B >actual &&
291
+ test_cmp expect actual
292
+'
293
+
294
+test_expect_success 'tree unchanged A, removed B' '
295
+ cat >expect <<-\EOF &&
296
+ removed in remote
297
+ base 100644 43d5a8ed6ef6c00ff775008633f95787d088285d sub/file
298
+ our 100644 43d5a8ed6ef6c00ff775008633f95787d088285d sub/file
299
+ @@ -1 +0,0 @@
300
+ -AAA
301
+ EOF
302
+ git reset --hard initial &&
303
+ mkdir sub &&
304
+ test_commit "add sub/file" "sub/file" "AAA" tree-remove-b-initial &&
305
+ git rm sub/file &&
306
+ test_tick &&
307
+ git commit -m "remove sub/file" &&
308
+ git tag tree-remove-b-B &&
309
+ git merge-tree tree-remove-b-initial tree-remove-b-initial tree-remove-b-B >actual &&
310
+ test_cmp expect actual
311
+'
312
+
313
test_expect_success 'turn file to tree' '
314
git reset --hard initial &&
315
rm initial-file &&