Skip to content

Commit 6e6743e

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: add basics of '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. This necessarily removes all files contained within, including tracked and untracked files. Of particular importance are ignored and excluded files which would normally be ignored even by 'git clean -f' unless the '-x' or '-X' option is provided. 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. The current implementation always deletes these sparse directories without warning. This is unacceptable for a released version, but those features will be added in changes coming immediately after this one. Note that untracked directories within the sparse-checkout remain. Further, directories that contain staged changes or files in merge conflict states 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. There are some interesting cases around merge conflict resolution, but that will be carefully analyzed in the future. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1c9cdbb commit 6e6743e

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

Documentation/git-sparse-checkout.adoc

Lines changed: 10 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,15 @@ 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, though it does not require enabling the sparse index
121+
feature via the `index.sparse=true` configuration.
122+
114123
'disable'::
115124
Disable the `core.sparseCheckout` config setting, and restore the
116125
working directory to include all files.

builtin/sparse-checkout.c

Lines changed: 63 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,66 @@ 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 const char *msg_remove = N_("Removing %s\n");
934+
935+
static int sparse_checkout_clean(int argc, const char **argv,
936+
const char *prefix,
937+
struct repository *repo)
938+
{
939+
struct strbuf full_path = STRBUF_INIT;
940+
const char *msg = msg_remove;
941+
size_t worktree_len;
942+
943+
struct option builtin_sparse_checkout_clean_options[] = {
944+
OPT_END(),
945+
};
946+
947+
setup_work_tree();
948+
if (!repo->settings.sparse_checkout)
949+
die(_("must be in a sparse-checkout to clean directories"));
950+
if (!repo->settings.sparse_checkout_cone)
951+
die(_("must be in a cone-mode sparse-checkout to clean directories"));
952+
953+
argc = parse_options(argc, argv, prefix,
954+
builtin_sparse_checkout_clean_options,
955+
builtin_sparse_checkout_clean_usage, 0);
956+
957+
if (repo_read_index(repo) < 0)
958+
die(_("failed to read index"));
959+
960+
if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY) ||
961+
repo->index->sparse_index == INDEX_EXPANDED)
962+
die(_("failed to convert index to a sparse index; resolve merge conflicts and try again"));
963+
964+
strbuf_addstr(&full_path, repo->worktree);
965+
strbuf_addch(&full_path, '/');
966+
worktree_len = full_path.len;
967+
968+
for (size_t i = 0; i < repo->index->cache_nr; i++) {
969+
struct cache_entry *ce = repo->index->cache[i];
970+
if (!S_ISSPARSEDIR(ce->ce_mode))
971+
continue;
972+
strbuf_setlen(&full_path, worktree_len);
973+
strbuf_add(&full_path, ce->name, ce->ce_namelen);
974+
975+
if (!is_directory(full_path.buf))
976+
continue;
977+
978+
printf(msg, ce->name);
979+
980+
if (remove_dir_recursively(&full_path, 0))
981+
warning_errno(_("failed to remove '%s'"), ce->name);
982+
}
983+
984+
strbuf_release(&full_path);
985+
return 0;
986+
}
987+
927988
static char const * const builtin_sparse_checkout_disable_usage[] = {
928989
"git sparse-checkout disable",
929990
NULL
@@ -1079,6 +1140,7 @@ int cmd_sparse_checkout(int argc,
10791140
OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
10801141
OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
10811142
OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1143+
OPT_SUBCOMMAND("clean", &fn, sparse_checkout_clean),
10821144
OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
10831145
OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
10841146
OPT_END(),

t/t1091-sparse-checkout-builtin.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,5 +1050,43 @@ 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+
Removing deep/deeper2/
1061+
Removing folder1/
1062+
EOF
1063+
1064+
git -C repo sparse-checkout clean >out &&
1065+
test_cmp expect out &&
1066+
1067+
test_path_is_missing repo/deep/deeper2 &&
1068+
test_path_is_missing repo/folder1
1069+
'
1070+
1071+
test_expect_success 'clean with staged sparse change' '
1072+
git -C repo sparse-checkout set --cone deep/deeper1 &&
1073+
mkdir repo/deep/deeper2 repo/folder1 repo/folder2 &&
1074+
touch repo/deep/deeper2/file &&
1075+
touch repo/folder1/file &&
1076+
echo dirty >repo/folder2/a &&
1077+
1078+
git -C repo add --sparse folder1/file &&
1079+
1080+
# deletes deep/deeper2/ but leaves folder1/ and folder2/
1081+
cat >expect <<-\EOF &&
1082+
Removing deep/deeper2/
1083+
EOF
1084+
1085+
git -C repo sparse-checkout clean >out &&
1086+
test_cmp expect out &&
1087+
1088+
test_path_is_missing repo/deep/deeper2 &&
1089+
test_path_exists repo/folder1
1090+
'
10531091

10541092
test_done

0 commit comments

Comments
 (0)