Skip to contentSkip to conversation

clean: avoid looking for nested repository when appropriate

PATCH Superseded Patrick Marlier 2 participants 4 messages Feb 15, 2022 – Feb 16, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 clean: avoid looking for nested repository when appropriate

avoiding the unnecessary checks for is_nonbare_repository_dir() via setting DIR_NO_GITLINKS

---
 builtin/clean.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
builtin/clean.c +3 -2
index 3ff02bbbff..18b37e3fd9 100644
@@ -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;
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/2 clean: avoid to differentiate untracked and ignored when appropriate

---
 builtin/clean.c  |  4 +++-
 t/t7300-clean.sh | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
builtin/clean.c +3 -1
index 18b37e3fd9..1b1454d052 100644
@@ -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:
 		 *
t/t7300-clean.sh +24 -0
index 0399701e62..684eba914b 100755
@@ -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.* &&
+	test_create_repo 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
Signature · 1 line
2.35.1
#3 lore raw
In reply to Patrick Marlier

Hi,

Quoted 2 lines · Patrick Marlier

avoiding the unnecessary checks for is_nonbare_repository_dir() via setting DIR_NO_GITLINKS

Looks great, but a few details about commit messages that we like to see:

* Please wrap commit messages at 72 characters
* Describe your changes in imperative mood (i.e. "Avoid the
unnecessary" rather than "avoiding the unnecessary")
* Use complete sentences for everything other than the subject.

So, perhaps:

"""
clean: avoid looking for nested repositories when unnecessary

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.
"""

Quoted 23 lines
---
 builtin/clean.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
builtin/clean.c +3 -3
index 3ff02bbbff..18b37e3fd9 100644
@@ -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

#2 lore raw

Re: [PATCH 2/2] clean: avoid to differentiate untracked and ignored when appropriate

In reply to Patrick Marlier

On Tue, Feb 15, 2022 at 2:16 PM Patrick Marlier
<[email protected]> wrote:

Could I suggest a bit longer commit message? Perhaps:

"""
clean: avoid traversing into untracked dirs when unnecessary

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.
"""

Quoted 25 lines
---
 builtin/clean.c  |  4 +++-
 t/t7300-clean.sh | 24 ++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
builtin/clean.c +3 -1
index 18b37e3fd9..1b1454d052 100644
@@ -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:
                 *
t/t7300-clean.sh +0 -0
index 0399701e62..684eba914b 100755

t/t7300-clean.sh test_expect_success 'traverse into directories that may have ignored entries' ' · line 793

Quoted 6 lines
t/t7300-clean.sh +3 -0
@@ -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.* &&
+       test_create_repo avoid-traversing-untracked-hierarchy &&

I think you may have been copying from an example earlier in the file that I like wrote. As someone else recently pointed out to me, t/test-lib-functions.sh points out that test_create_repo is deprecated and we should just use "git init <dirname>" instead.

Quoted 23 lines
t/t7300-clean.sh +21 -1
+       (
+               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
--
Quoted 1 line

2.35.1

Looks good, other than those minor items. Thanks for working on this!