Skip to content

Commit c1f183f

Browse files
derrickstoleedscho
authored andcommitted
sparse-checkout: add --verbose option to 'clean'
The 'git sparse-checkout clean' subcommand is focused on directories, deleting any tracked sparse directories to clean up the worktree and make the sparse index feature work optimally. However, this directory-focused approach can leave users wondering why those directories exist at all. In my experience, these files are left over due to ignore or exclude patterns, Windows file handles, or possibly merge conflict resolutions. Add a new '--verbose' option for users to see all the files that are being deleted (with '--force') or would be deleted (with '--dry-run'). Signed-off-by: Derrick Stolee <[email protected]>
1 parent 1a74f67 commit c1f183f

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

Documentation/git-sparse-checkout.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ The `--dry-run` option will list the directories that would be removed
128128
without deleting them. Running in this mode can be helpful to predict the
129129
behavior of the clean comand or to determine which kinds of files are left
130130
in the sparse directories.
131+
+
132+
The `--verbose` option will list every file within the directories that
133+
are considered for removal. This option is helpful to determine if those
134+
files are actually important or perhaps to explain why the directory is
135+
still present despite the current sparse-checkout.
131136

132137
'disable'::
133138
Disable the `core.sparseCheckout` config setting, and restore the

builtin/sparse-checkout.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,26 @@ static char const * const builtin_sparse_checkout_clean_usage[] = {
939939
NULL
940940
};
941941

942+
static int list_file_iterator(const char *path, const void *data)
943+
{
944+
const char *msg = data;
945+
946+
printf(msg, path);
947+
return 0;
948+
}
949+
950+
static void list_every_file_in_dir(const char *msg,
951+
const char *directory)
952+
{
953+
struct strbuf path = STRBUF_INIT;
954+
955+
strbuf_addstr(&path, directory);
956+
fprintf(stderr, "list every file in %s\n", directory);
957+
958+
for_each_file_in_dir(&path, list_file_iterator, msg);
959+
strbuf_release(&path);
960+
}
961+
942962
static const char *msg_remove = N_("Removing %s\n");
943963
static const char *msg_would_remove = N_("Would remove %s\n");
944964

@@ -949,12 +969,13 @@ static int sparse_checkout_clean(int argc, const char **argv,
949969
struct strbuf full_path = STRBUF_INIT;
950970
const char *msg = msg_remove;
951971
size_t worktree_len;
952-
int force = 0, dry_run = 0;
972+
int force = 0, dry_run = 0, verbose = 0;
953973
int require_force = 1;
954974

955975
struct option builtin_sparse_checkout_clean_options[] = {
956976
OPT__DRY_RUN(&dry_run, N_("dry run")),
957977
OPT__FORCE(&force, N_("force"), PARSE_OPT_NOCOMPLETE),
978+
OPT__VERBOSE(&verbose, N_("report each affected file, not just directories")),
958979
OPT_END(),
959980
};
960981

@@ -996,7 +1017,10 @@ static int sparse_checkout_clean(int argc, const char **argv,
9961017
if (!is_directory(full_path.buf))
9971018
continue;
9981019

999-
printf(msg, ce->name);
1020+
if (verbose)
1021+
list_every_file_in_dir(msg, ce->name);
1022+
else
1023+
printf(msg, ce->name);
10001024

10011025
if (dry_run <= 0 &&
10021026
remove_dir_recursively(&full_path, 0))

t/t1091-sparse-checkout-builtin.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,9 +1057,9 @@ test_expect_success 'check-rules null termination' '
10571057

10581058
test_expect_success 'clean' '
10591059
git -C repo sparse-checkout set --cone deep/deeper1 &&
1060-
mkdir repo/deep/deeper2 repo/folder1 &&
1060+
mkdir -p repo/deep/deeper2 repo/folder1/extra/inside &&
10611061
touch repo/deep/deeper2/file &&
1062-
touch repo/folder1/file &&
1062+
touch repo/folder1/extra/inside/file &&
10631063
10641064
test_must_fail git -C repo sparse-checkout clean 2>err &&
10651065
grep "refusing to clean" err &&
@@ -1076,7 +1076,15 @@ test_expect_success 'clean' '
10761076
git -C repo sparse-checkout clean --dry-run >out &&
10771077
test_cmp expect out &&
10781078
test_path_exists repo/deep/deeper2 &&
1079-
test_path_exists repo/folder1 &&
1079+
test_path_exists repo/folder1/extra/inside/file &&
1080+
1081+
cat >expect <<-\EOF &&
1082+
Would remove deep/deeper2/file
1083+
Would remove folder1/extra/inside/file
1084+
EOF
1085+
1086+
git -C repo sparse-checkout clean --dry-run --verbose >out &&
1087+
test_cmp expect out &&
10801088
10811089
cat >expect <<-\EOF &&
10821090
Removing deep/deeper2/

0 commit comments

Comments
 (0)