read-tree: make two-way merge sparse-aware

Enable two-way merge with 'git read-tree' without expanding the sparse index. When in a sparse index, a two-way merge will trivially succeed as long as there are not changes to the same sparse directory in multiple trees (i.e., sparse directory-level "edit-edit" conflicts). If there are such conflicts, the merge will fail despite the possibility that individual files could merge cleanly. In order to resolve these "edit-edit" conflicts, "conflicted" sparse directories are - rather than rejected - merged by traversing their associated trees by OID. For each child of the sparse directory: 1. Files are merged as normal (see Documentation/git-read-tree.txt for details). 2. Subdirectories are treated as sparse directories and merged in 'twoway_merge'. If there are no conflicts, they are merged according to the rules in Documentation/git-read-tree.txt; otherwise, the subdirectory is recursively traversed and merged. This process allows sparse directories to be individually merged at the necessary depth *without* expanding a full index. The 't/t1092-sparse-checkout-compatibility.sh' test 'read-tree --merge with edit/edit conflicts in sparse directories' tests two-way merges with 1) changes inside sparse directories that do not conflict and 2) changes that do conflict (with the correct file(s) reported in the error message). Additionally, add two-way merge cases to 'sparse index is not expanded: read-tree' to confirm that the index is not expanded regardless of whether edit/edit conflicts are present in a sparse directory. Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Mar 1, 2022 at 20:24 UTC ab81047a6c5c4f98640f8a2f56e138367cfdada9
3 files changed +78 -6
builtin/read-tree.c
-5
@@ -229,11 +229,6 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
229 opts.fn = opts.prefix ? bind_merge : oneway_merge;
230 break;
231 case 2:
232 - /*
233 - * TODO: update twoway_merge to handle edit/edit conflicts in
234 - * sparse directories.
235 - */
236 - ensure_full_index(&the_index);
232 opts.fn = twoway_merge;
233 opts.initial_checkout = is_cache_unborn();
234 break;
t/t1092-sparse-checkout-compatibility.sh
+3 -1
@@ -1413,7 +1413,9 @@ test_expect_success 'sparse index is not expanded: read-tree' '
1413 init_repos &&
1414
1415 ensure_not_expanded checkout -b test-branch update-folder1 &&
1416 - for MERGE_TREES in "update-folder2"
1416 + for MERGE_TREES in "base update-folder2" \
1417 + "base rename-base" \
1418 + "update-folder2"
1419 do
1420 ensure_not_expanded read-tree -mu $MERGE_TREES &&
1421 ensure_not_expanded reset --hard || return 1
unpack-trees.c
+75
@@ -1360,6 +1360,42 @@ static int is_sparse_directory_entry(struct cache_entry *ce,
1360 return sparse_dir_matches_path(ce, info, name);
1361 }
1362
1363 +static int unpack_sparse_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *names, struct traverse_info *info)
1364 +{
1365 + struct cache_entry *src[MAX_UNPACK_TREES + 1] = { NULL, };
1366 + struct unpack_trees_options *o = info->data;
1367 + int ret;
1368 +
1369 + assert(o->merge);
1370 +
1371 + /*
1372 + * Unlike in 'unpack_callback', where src[0] is derived from the index when
1373 + * merging, src[0] is a transient cache entry derived from the first tree
1374 + * provided. Create the temporary entry as if it came from a non-sparse index.
1375 + */
1376 + if (!is_null_oid(&names[0].oid)) {
1377 + src[0] = create_ce_entry(info, &names[0], 0,
1378 + &o->result, 1,
1379 + dirmask & (1ul << 0));
1380 + src[0]->ce_flags |= (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
1381 + }
1382 +
1383 + /*
1384 + * 'unpack_single_entry' assumes that src[0] is derived directly from
1385 + * the index, rather than from an entry in 'names'. This is *not* true when
1386 + * merging a sparse directory, in which case names[0] is the "index" source
1387 + * entry. To match the expectations of 'unpack_single_entry', shift past the
1388 + * "index" tree (i.e., names[0]) and adjust 'names', 'n', 'mask', and
1389 + * 'dirmask' accordingly.
1390 + */
1391 + ret = unpack_single_entry(n - 1, mask >> 1, dirmask >> 1, src, names + 1, info);
1392 +
1393 + if (src[0])
1394 + discard_cache_entry(src[0]);
1395 +
1396 + return ret >= 0 ? mask : -1;
1397 +}
1398 +
1399 /*
1400 * Note that traverse_by_cache_tree() duplicates some logic in this function
1401 * without actually calling it. If you change the logic here you may need to
@@ -2472,6 +2508,37 @@ static int merged_entry(const struct cache_entry *ce,
2508 return 1;
2509 }
2510
2511 +static int merged_sparse_dir(const struct cache_entry * const *src, int n,
2512 + struct unpack_trees_options *o)
2513 +{
2514 + struct tree_desc t[MAX_UNPACK_TREES + 1];
2515 + void * tree_bufs[MAX_UNPACK_TREES + 1];
2516 + struct traverse_info info;
2517 + int i, ret;
2518 +
2519 + /*
2520 + * Create the tree traversal information for traversing into *only* the
2521 + * sparse directory.
2522 + */
2523 + setup_traverse_info(&info, src[0]->name);
2524 + info.fn = unpack_sparse_callback;
2525 + info.data = o;
2526 + info.show_all_errors = o->show_all_errors;
2527 + info.pathspec = o->pathspec;
2528 +
2529 + /* Get the tree descriptors of the sparse directory in each of the merging trees */
2530 + for (i = 0; i < n; i++)
2531 + tree_bufs[i] = fill_tree_descriptor(o->src_index->repo, &t[i],
2532 + src[i] && !is_null_oid(&src[i]->oid) ? &src[i]->oid : NULL);
2533 +
2534 + ret = traverse_trees(o->src_index, n, t, &info);
2535 +
2536 + for (i = 0; i < n; i++)
2537 + free(tree_bufs[i]);
2538 +
2539 + return ret;
2540 +}
2541 +
2542 static int deleted_entry(const struct cache_entry *ce,
2543 const struct cache_entry *old,
2544 struct unpack_trees_options *o)
@@ -2742,6 +2809,14 @@ int twoway_merge(const struct cache_entry * const *src,
2809 * reject the merge instead.
2810 */
2811 return merged_entry(newtree, current, o);
2812 + } else if (S_ISSPARSEDIR(current->ce_mode)) {
2813 + /*
2814 + * The sparse directories differ, but we don't know whether that's
2815 + * because of two different files in the directory being modified
2816 + * (can be trivially merged) or if there is a real file conflict.
2817 + * Merge the sparse directory by OID to compare file-by-file.
2818 + */
2819 + return merged_sparse_dir(src, 3, o);
2820 } else
2821 return reject_merge(current, o);
2822 }