Skip to content

Commit 5644ca2

Browse files
newrengitster
authored andcommitted
sparse-checkout: provide a new reapply subcommand
If commands like merge or rebase materialize files as part of their work, or a previous sparse-checkout command failed to update individual files due to dirty changes, users may want a command to simply 'reapply' the sparsity rules. Provide one. Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 681c637 commit 5644ca2

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

Documentation/git-sparse-checkout.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ C-style quoted strings.
7070
`core.sparseCheckoutCone` is enabled, the given patterns are interpreted
7171
as directory names as in the 'set' subcommand.
7272

73+
'reapply::
74+
Reapply the sparsity pattern rules to paths in the working tree.
75+
Commands like merge or rebase can materialize paths to do their
76+
work (e.g. in order to show you a conflict), and other
77+
sparse-checkout commands might fail to sparsify an individual file
78+
(e.g. because it has unstaged changes or conflicts). In such
79+
cases, it can make sense to run `git sparse-checkout reapply` later
80+
after cleaning up affected paths (e.g. resolving conflicts, undoing
81+
or committing changes, etc.).
82+
7383
'disable'::
7484
Disable the `core.sparseCheckout` config setting, and restore the
7585
working directory to include all files. Leaves the sparse-checkout

builtin/sparse-checkout.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
static const char *empty_base = "";
1919

2020
static char const * const builtin_sparse_checkout_usage[] = {
21-
N_("git sparse-checkout (init|list|set|add|disable) <options>"),
21+
N_("git sparse-checkout (init|list|set|add|reapply|disable) <options>"),
2222
NULL
2323
};
2424

@@ -554,6 +554,12 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
554554
return modify_pattern_list(argc, argv, m);
555555
}
556556

557+
static int sparse_checkout_reapply(int argc, const char **argv)
558+
{
559+
repo_read_index(the_repository);
560+
return update_working_directory(NULL);
561+
}
562+
557563
static int sparse_checkout_disable(int argc, const char **argv)
558564
{
559565
struct pattern_list pl;
@@ -603,6 +609,8 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
603609
return sparse_checkout_set(argc, argv, prefix, REPLACE);
604610
if (!strcmp(argv[0], "add"))
605611
return sparse_checkout_set(argc, argv, prefix, ADD);
612+
if (!strcmp(argv[0], "reapply"))
613+
return sparse_checkout_reapply(argc, argv);
606614
if (!strcmp(argv[0], "disable"))
607615
return sparse_checkout_disable(argc, argv);
608616
}

t/t1091-sparse-checkout-builtin.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,47 @@ test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged stat
370370
git -C unmerged sparse-checkout disable
371371
'
372372

373+
test_expect_success 'sparse-checkout reapply' '
374+
git clone repo tweak &&
375+
376+
echo dirty >tweak/deep/deeper2/a &&
377+
378+
cat >input <<-EOF &&
379+
0 0000000000000000000000000000000000000000 folder1/a
380+
100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
381+
EOF
382+
git -C tweak update-index --index-info <input &&
383+
384+
git -C tweak sparse-checkout init --cone 2>err &&
385+
test_i18ngrep "warning.*The following paths are not up to date" err &&
386+
test_i18ngrep "warning.*The following paths are unmerged" err &&
387+
388+
git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
389+
test_i18ngrep "warning.*The following paths are not up to date" err &&
390+
test_i18ngrep "warning.*The following paths are unmerged" err &&
391+
392+
git -C tweak sparse-checkout reapply 2>err &&
393+
test_i18ngrep "warning.*The following paths are not up to date" err &&
394+
test_path_is_file tweak/deep/deeper2/a &&
395+
test_i18ngrep "warning.*The following paths are unmerged" err &&
396+
test_path_is_file tweak/folder1/a &&
397+
398+
git -C tweak checkout HEAD deep/deeper2/a &&
399+
git -C tweak sparse-checkout reapply 2>err &&
400+
test_i18ngrep ! "warning.*The following paths are not up to date" err &&
401+
test_path_is_missing tweak/deep/deeper2/a &&
402+
test_i18ngrep "warning.*The following paths are unmerged" err &&
403+
test_path_is_file tweak/folder1/a &&
404+
405+
git -C tweak add folder1/a &&
406+
git -C tweak sparse-checkout reapply 2>err &&
407+
test_must_be_empty err &&
408+
test_path_is_missing tweak/deep/deeper2/a &&
409+
test_path_is_missing tweak/folder1/a &&
410+
411+
git -C tweak sparse-checkout disable
412+
'
413+
373414
test_expect_success 'cone mode: set with core.ignoreCase=true' '
374415
rm repo/.git/info/sparse-checkout &&
375416
git -C repo sparse-checkout init --cone &&

0 commit comments

Comments
 (0)