Skip to content

Commit 4c452b4

Browse files
committed
sparse-checkout: add 'clean' command
When users change their sparse-checkout definitions to add new directories and remove old ones, there may be a few reasons why directories no longer in scope remain (ignored or excluded files still exist, Windows handles are still open, etc.). When these files still exist, the sparse index feature notices that a tracked, but sparse, directory still exists on disk and thus the index expands. This causes a performance hit _and_ the advice printed isn't very helpful. Using 'git clean' isn't enough (generally '-dfx' may be needed) but also this may not be sufficient. Add a new subcommand to 'git sparse-checkout' that removes these tracked-but-sparse directories, including any excluded or ignored files underneath. This is the most extreme method for doing this, but it works when the sparse-checkout is in cone mode and is expected to rescope based on directories, not files. Be sure to add a --dry-run option so users can predict what will be deleted. In general, output the directories that are being removed so users can know what was removed. Note that untracked directories within the sparse-checkout remain. Further, directories that contain staged changes are not deleted. This is a detail that is partly hidden by the implementation which relies on collapsing the index to a sparse index in-memory and only deleting directories that are listed as sparse in the index. If a staged change exists, then that entry is not stored as a sparse tree entry and thus remains on-disk until committed or reset. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 92d0cd4 commit 4c452b4

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed

Documentation/git-sparse-checkout.adoc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-sparse-checkout - Reduce your working tree to a subset of tracked files
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git sparse-checkout' (init | list | set | add | reapply | disable | check-rules) [<options>]
12+
'git sparse-checkout' (init | list | set | add | reapply | disable | check-rules | clean) [<options>]
1313

1414

1515
DESCRIPTION
@@ -111,6 +111,17 @@ flags, with the same meaning as the flags from the `set` command, in order
111111
to change which sparsity mode you are using without needing to also respecify
112112
all sparsity paths.
113113

114+
'clean'::
115+
Remove all files in tracked directories that are outside of the
116+
sparse-checkout definition. This subcommand requires cone-mode
117+
sparse-checkout to be sure that we know which directories are
118+
both tracked and all contained paths are not in the sparse-checkout.
119+
This command can be used to be sure the sparse index works
120+
efficiently.
121+
+
122+
The `clean` command can also take the `--dry-run` (`-n`) option to list
123+
the directories it would remove without performing any filesystem changes.
124+
114125
'disable'::
115126
Disable the `core.sparseCheckout` config setting, and restore the
116127
working directory to include all files.

builtin/sparse-checkout.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define DISABLE_SIGN_COMPARE_WARNINGS
33

44
#include "builtin.h"
5+
#include "abspath.h"
56
#include "config.h"
67
#include "dir.h"
78
#include "environment.h"
@@ -23,7 +24,7 @@
2324
static const char *empty_base = "";
2425

2526
static char const * const builtin_sparse_checkout_usage[] = {
26-
N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
27+
N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules | clean) [<options>]"),
2728
NULL
2829
};
2930

@@ -924,6 +925,73 @@ static int sparse_checkout_reapply(int argc, const char **argv,
924925
return update_working_directory(repo, NULL);
925926
}
926927

928+
static char const * const builtin_sparse_checkout_clean_usage[] = {
929+
"git sparse-checkout clean [-n|--dry-run]",
930+
NULL
931+
};
932+
933+
static struct sparse_checkout_clean_opts {
934+
int dry_run;
935+
} clean_opts;
936+
937+
static const char *msg_remove = N_("Removing %s\n");
938+
static const char *msg_would_remove = N_("Would remove %s\n");
939+
940+
static int sparse_checkout_clean(int argc, const char **argv,
941+
const char *prefix,
942+
struct repository *repo)
943+
{
944+
struct strbuf full_path = STRBUF_INIT;
945+
size_t worktree_len;
946+
static struct option builtin_sparse_checkout_clean_options[] = {
947+
OPT_BOOL('n', "dry-run", &clean_opts.dry_run,
948+
N_("list the directories that would be removed without making filesystem changes")),
949+
OPT_END(),
950+
};
951+
952+
setup_work_tree();
953+
if (!repo->settings.sparse_checkout)
954+
die(_("must be in a sparse-checkout to clean directories"));
955+
if (!repo->settings.sparse_checkout_cone)
956+
die(_("must be in a cone-mode sparse-checkout to clean directories"));
957+
958+
argc = parse_options(argc, argv, prefix,
959+
builtin_sparse_checkout_clean_options,
960+
builtin_sparse_checkout_clean_usage, 0);
961+
962+
if (repo_read_index(repo) < 0)
963+
die(_("failed to read index"));
964+
965+
if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY))
966+
die(_("failed to convert index to a sparse index"));
967+
968+
strbuf_addstr(&full_path, repo->worktree);
969+
strbuf_addch(&full_path, '/');
970+
worktree_len = full_path.len;
971+
972+
for (size_t i = 0; i < repo->index->cache_nr; i++) {
973+
struct cache_entry *ce = repo->index->cache[i];
974+
if (!S_ISSPARSEDIR(ce->ce_mode))
975+
continue;
976+
strbuf_setlen(&full_path, worktree_len);
977+
strbuf_add(&full_path, ce->name, ce->ce_namelen);
978+
979+
if (!is_directory(full_path.buf))
980+
continue;
981+
982+
if (!clean_opts.dry_run) {
983+
printf(msg_remove, ce->name);
984+
if (remove_dir_recursively(&full_path, 0))
985+
warning_errno(_("failed to remove '%s'"), ce->name);
986+
} else {
987+
printf(msg_would_remove, ce->name);
988+
}
989+
}
990+
991+
strbuf_release(&full_path);
992+
return 0;
993+
}
994+
927995
static char const * const builtin_sparse_checkout_disable_usage[] = {
928996
"git sparse-checkout disable",
929997
NULL
@@ -1079,6 +1147,7 @@ int cmd_sparse_checkout(int argc,
10791147
OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
10801148
OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
10811149
OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1150+
OPT_SUBCOMMAND("clean", &fn, sparse_checkout_clean),
10821151
OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
10831152
OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
10841153
OPT_END(),

t/t1091-sparse-checkout-builtin.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,5 +1050,62 @@ test_expect_success 'check-rules null termination' '
10501050
test_cmp expect actual
10511051
'
10521052

1053+
test_expect_success 'clean' '
1054+
git -C repo sparse-checkout set --cone deep/deeper1 &&
1055+
mkdir repo/deep/deeper2 repo/folder1 &&
1056+
touch repo/deep/deeper2/file &&
1057+
touch repo/folder1/file &&
1058+
1059+
cat >expect <<-\EOF &&
1060+
Would remove deep/deeper2/
1061+
Would remove folder1/
1062+
EOF
1063+
1064+
git -C repo sparse-checkout clean --dry-run >out &&
1065+
test_cmp expect out &&
1066+
1067+
test_path_exists repo/deep/deeper2 &&
1068+
test_path_exists repo/folder1 &&
1069+
1070+
cat >expect <<-\EOF &&
1071+
Removing deep/deeper2/
1072+
Removing folder1/
1073+
EOF
1074+
1075+
git -C repo sparse-checkout clean >out &&
1076+
test_cmp expect out &&
1077+
1078+
! test_path_exists repo/deep/deeper2 &&
1079+
! test_path_exists repo/folder1
1080+
'
1081+
1082+
test_expect_success 'clean with staged sparse change' '
1083+
git -C repo sparse-checkout set --cone deep/deeper1 &&
1084+
mkdir repo/deep/deeper2 repo/folder1 &&
1085+
touch repo/deep/deeper2/file &&
1086+
touch repo/folder1/file &&
1087+
1088+
git -C repo add --sparse folder1/file &&
1089+
1090+
cat >expect <<-\EOF &&
1091+
Would remove deep/deeper2/
1092+
EOF
1093+
1094+
git -C repo sparse-checkout clean --dry-run >out &&
1095+
test_cmp expect out &&
1096+
1097+
test_path_exists repo/deep/deeper2 &&
1098+
test_path_exists repo/folder1 &&
1099+
1100+
cat >expect <<-\EOF &&
1101+
Removing deep/deeper2/
1102+
EOF
1103+
1104+
git -C repo sparse-checkout clean >out &&
1105+
test_cmp expect out &&
1106+
1107+
! test_path_exists repo/deep/deeper2 &&
1108+
test_path_exists repo/folder1
1109+
'
10531110

10541111
test_done

0 commit comments

Comments
 (0)