# [PATCH v2 1/2] clean: avoid looking for nested repositories when unnecessary

3 messages from 2022-02-21 to 2022-02-22. Participants: Patrick Marlier, Elijah Newren.
Thread: https://gitlist.dev/t/57449

## Patrick Marlier, 2022-02-21 09:00

Subject: [PATCH v2 1/2] clean: avoid looking for nested repositories when unnecessary
Message-ID: <20220221090034.4615-1-patrick.marlier@gmail.com>
URL: https://gitlist.dev/e/20220221090034.4615-1-patrick.marlier%40gmail.com

```
With `git clean --ff` we will be deleting nested untracked repositories,
so there is no need to differentiate them from other untracked files.
Use the DIR_NO_GITLINKS flag in dir.flags to signify this and avoid the
is_nonbare_repository_dir() checks.
---
 builtin/clean.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/builtin/clean.c b/builtin/clean.c
index 3ff02bbbff..18b37e3fd9 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -955,9 +955,10 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 				  " refusing to clean"));
 	}
 
-	if (force > 1)
+	if (force > 1) {
 		rm_flags = 0;
-	else
+		dir.flags |= DIR_NO_GITLINKS;
+	} else
 		dir.flags |= DIR_SKIP_NESTED_GIT;
 
 	dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
-- 
2.35.1


```

## Patrick Marlier, 2022-02-21 09:00

Subject: [PATCH v2 2/2] clean: avoid traversing into untracked dirs when unnecessary
Message-ID: <20220221090034.4615-2-patrick.marlier@gmail.com>
URL: https://gitlist.dev/e/20220221090034.4615-2-patrick.marlier%40gmail.com
In-Reply-To: <20220221090034.4615-1-patrick.marlier@gmail.com>

```
When deleting all untracked and ignored files and any nested
repositories (such as with `git clean -ffdx`), we do not need to recurse
into an untracked directory to see if any of the entries under it are
ignored or a nested repository.  Special case this condition to avoid
unnecessary recursion.
---
 builtin/clean.c  |  4 +++-
 t/t7300-clean.sh | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/builtin/clean.c b/builtin/clean.c
index 18b37e3fd9..1b1454d052 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -978,7 +978,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 		remove_directories = 1;
 	}
 
-	if (remove_directories && !ignored_only) {
+	if (remove_directories && ignored && !exclude_list.nr && force > 1)
+		; /* No need to recurse to look for ignored files */
+	else if (remove_directories && !ignored_only) {
 		/*
 		 * We need to know about ignored files too:
 		 *
diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh
index 0399701e62..ceab7c4883 100755
--- a/t/t7300-clean.sh
+++ b/t/t7300-clean.sh
@@ -788,4 +788,28 @@ test_expect_success 'traverse into directories that may have ignored entries' '
 	)
 '
 
+test_expect_success 'avoid traversing into untracked directories' '
+	test_when_finished rm -f output error trace.* &&
+	git init avoid-traversing-untracked-hierarchy &&
+	(
+		cd avoid-traversing-untracked-hierarchy &&
+
+		mkdir -p untracked/subdir/with/b &&
+		mkdir -p untracked/subdir/with/a &&
+		>untracked/subdir/with/a/random-file.txt &&
+
+		GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+		git clean -ffdx
+	) &&
+
+	# Make sure we only visited into the top-level directory, and did
+	# not traverse into the "untracked" subdirectory since it was excluded
+	grep data.*read_directo.*directories-visited trace.output |
+		cut -d "|" -f 9 >trace.relevant &&
+	cat >trace.expect <<-EOF &&
+	 ..directories-visited:1
+	EOF
+	test_cmp trace.expect trace.relevant
+'
+
 test_done
-- 
2.35.1


```

## Elijah Newren, 2022-02-22 01:48

Subject: Re: [PATCH v2 2/2] clean: avoid traversing into untracked dirs when unnecessary
Message-ID: <CABPp-BGdkgaRC+wBobhzyge=di8uE3kzHiO8v26bi2v0kTXerw@mail.gmail.com>
URL: https://gitlist.dev/e/CABPp-BGdkgaRC%2BwBobhzyge%3Ddi8uE3kzHiO8v26bi2v0kTXerw%40mail.gmail.com
In-Reply-To: <20220221090034.4615-2-patrick.marlier@gmail.com>

```
On Mon, Feb 21, 2022 at 1:00 AM Patrick Marlier
<patrick.marlier@gmail.com> wrote:
>
> When deleting all untracked and ignored files and any nested
> repositories (such as with `git clean -ffdx`), we do not need to recurse
> into an untracked directory to see if any of the entries under it are
> ignored or a nested repository.  Special case this condition to avoid
> unnecessary recursion.
> ---
>  builtin/clean.c  |  4 +++-
>  t/t7300-clean.sh | 24 ++++++++++++++++++++++++
>  2 files changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/builtin/clean.c b/builtin/clean.c
> index 18b37e3fd9..1b1454d052 100644
> --- a/builtin/clean.c
> +++ b/builtin/clean.c
> @@ -978,7 +978,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
>                 remove_directories = 1;
>         }
>
> -       if (remove_directories && !ignored_only) {
> +       if (remove_directories && ignored && !exclude_list.nr && force > 1)
> +               ; /* No need to recurse to look for ignored files */
> +       else if (remove_directories && !ignored_only) {
>                 /*
>                  * We need to know about ignored files too:
>                  *
> diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh
> index 0399701e62..ceab7c4883 100755
> --- a/t/t7300-clean.sh
> +++ b/t/t7300-clean.sh
> @@ -788,4 +788,28 @@ test_expect_success 'traverse into directories that may have ignored entries' '
>         )
>  '
>
> +test_expect_success 'avoid traversing into untracked directories' '
> +       test_when_finished rm -f output error trace.* &&
> +       git init avoid-traversing-untracked-hierarchy &&
> +       (
> +               cd avoid-traversing-untracked-hierarchy &&
> +
> +               mkdir -p untracked/subdir/with/b &&
> +               mkdir -p untracked/subdir/with/a &&
> +               >untracked/subdir/with/a/random-file.txt &&
> +
> +               GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
> +               git clean -ffdx
> +       ) &&
> +
> +       # Make sure we only visited into the top-level directory, and did
> +       # not traverse into the "untracked" subdirectory since it was excluded
> +       grep data.*read_directo.*directories-visited trace.output |
> +               cut -d "|" -f 9 >trace.relevant &&
> +       cat >trace.expect <<-EOF &&
> +        ..directories-visited:1
> +       EOF
> +       test_cmp trace.expect trace.relevant
> +'
> +
>  test_done
> --
> 2.35.1

Thanks, this round looks good to me.  Both patches are:

Reviewed-by: Elijah Newren <newren@gmail.com>

```
