Skip to content

Commit ab81047

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

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

builtin/read-tree.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,6 @@ int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
229229
opts.fn = opts.prefix ? bind_merge : oneway_merge;
230230
break;
231231
case 2:
232-
/*
233-
* TODO: update twoway_merge to handle edit/edit conflicts in
234-
* sparse directories.
235-
*/
236-
ensure_full_index(&the_index);
237232
opts.fn = twoway_merge;
238233
opts.initial_checkout = is_cache_unborn();
239234
break;

t/t1092-sparse-checkout-compatibility.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,9 @@ test_expect_success 'sparse index is not expanded: read-tree' '
14131413
init_repos &&
14141414
14151415
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"
14171419
do
14181420
ensure_not_expanded read-tree -mu $MERGE_TREES &&
14191421
ensure_not_expanded reset --hard || return 1

unpack-trees.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,6 +1360,42 @@ static int is_sparse_directory_entry(struct cache_entry *ce,
13601360
return sparse_dir_matches_path(ce, info, name);
13611361
}
13621362

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+
13631399
/*
13641400
* Note that traverse_by_cache_tree() duplicates some logic in this function
13651401
* 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,
24722508
return 1;
24732509
}
24742510

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+
24752542
static int deleted_entry(const struct cache_entry *ce,
24762543
const struct cache_entry *old,
24772544
struct unpack_trees_options *o)
@@ -2742,6 +2809,14 @@ int twoway_merge(const struct cache_entry * const *src,
27422809
* reject the merge instead.
27432810
*/
27442811
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);
27452820
} else
27462821
return reject_merge(current, o);
27472822
}

0 commit comments

Comments
 (0)