Skip to content

Commit 1556045

Browse files
derrickstoleedscho
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]>
1 parent f2111ce commit 1556045

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

@@ -933,6 +934,66 @@ static int sparse_checkout_reapply(int argc, const char **argv,
933934
return update_working_directory(repo, NULL);
934935
}
935936

937+
static char const * const builtin_sparse_checkout_clean_usage[] = {
938+
"git sparse-checkout clean [-n|--dry-run]",
939+
NULL
940+
};
941+
942+
static const char *msg_remove = N_("Removing %s\n");
943+
944+
static int sparse_checkout_clean(int argc, const char **argv,
945+
const char *prefix,
946+
struct repository *repo)
947+
{
948+
struct strbuf full_path = STRBUF_INIT;
949+
const char *msg = msg_remove;
950+
size_t worktree_len;
951+
952+
struct option builtin_sparse_checkout_clean_options[] = {
953+
OPT_END(),
954+
};
955+
956+
setup_work_tree();
957+
if (!core_apply_sparse_checkout)
958+
die(_("must be in a sparse-checkout to clean directories"));
959+
if (!core_sparse_checkout_cone)
960+
die(_("must be in a cone-mode sparse-checkout to clean directories"));
961+
962+
argc = parse_options(argc, argv, prefix,
963+
builtin_sparse_checkout_clean_options,
964+
builtin_sparse_checkout_clean_usage, 0);
965+
966+
if (repo_read_index(repo) < 0)
967+
die(_("failed to read index"));
968+
969+
if (convert_to_sparse(repo->index, SPARSE_INDEX_MEMORY_ONLY) ||
970+
repo->index->sparse_index == INDEX_EXPANDED)
971+
die(_("failed to convert index to a sparse index; resolve merge conflicts and try again"));
972+
973+
strbuf_addstr(&full_path, repo->worktree);
974+
strbuf_addch(&full_path, '/');
975+
worktree_len = full_path.len;
976+
977+
for (size_t i = 0; i < repo->index->cache_nr; i++) {
978+
struct cache_entry *ce = repo->index->cache[i];
979+
if (!S_ISSPARSEDIR(ce->ce_mode))
980+
continue;
981+
strbuf_setlen(&full_path, worktree_len);
982+
strbuf_add(&full_path, ce->name, ce->ce_namelen);
983+
984+
if (!is_directory(full_path.buf))
985+
continue;
986+
987+
printf(msg, ce->name);
988+
989+
if (remove_dir_recursively(&full_path, 0))
990+
warning_errno(_("failed to remove '%s'"), ce->name);
991+
}
992+
993+
strbuf_release(&full_path);
994+
return 0;
995+
}
996+
936997
static char const * const builtin_sparse_checkout_disable_usage[] = {
937998
"git sparse-checkout disable",
938999
NULL
@@ -1089,6 +1150,7 @@ int cmd_sparse_checkout(int argc,
10891150
OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
10901151
OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
10911152
OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1153+
OPT_SUBCOMMAND("clean", &fn, sparse_checkout_clean),
10921154
OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
10931155
OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
10941156
OPT_END(),

t/t1091-sparse-checkout-builtin.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,5 +1055,43 @@ test_expect_success 'check-rules null termination' '
10551055
test_cmp expect actual
10561056
'
10571057

1058+
test_expect_success 'clean' '
1059+
git -C repo sparse-checkout set --cone deep/deeper1 &&
1060+
mkdir repo/deep/deeper2 repo/folder1 &&
1061+
touch repo/deep/deeper2/file &&
1062+
touch repo/folder1/file &&
1063+
1064+
cat >expect <<-\EOF &&
1065+
Removing deep/deeper2/
1066+
Removing folder1/
1067+
EOF
1068+
1069+
git -C repo sparse-checkout clean >out &&
1070+
test_cmp expect out &&
1071+
1072+
test_path_is_missing repo/deep/deeper2 &&
1073+
test_path_is_missing repo/folder1
1074+
'
1075+
1076+
test_expect_success 'clean with staged sparse change' '
1077+
git -C repo sparse-checkout set --cone deep/deeper1 &&
1078+
mkdir repo/deep/deeper2 repo/folder1 repo/folder2 &&
1079+
touch repo/deep/deeper2/file &&
1080+
touch repo/folder1/file &&
1081+
echo dirty >repo/folder2/a &&
1082+
1083+
git -C repo add --sparse folder1/file &&
1084+
1085+
# deletes deep/deeper2/ but leaves folder1/ and folder2/
1086+
cat >expect <<-\EOF &&
1087+
Removing deep/deeper2/
1088+
EOF
1089+
1090+
git -C repo sparse-checkout clean >out &&
1091+
test_cmp expect out &&
1092+
1093+
test_path_is_missing repo/deep/deeper2 &&
1094+
test_path_exists repo/folder1
1095+
'
10581096

10591097
test_done

0 commit comments

Comments
 (0)