Skip to contentSkip to conversation

Improving performance of git clean

PATCH Superseded Erik Elfström 3 participants 14 messages Jun 15, 2016

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

cover Improving performance of git clean

This series addresses a performance issue of git clean previously discussed here:
http://thread.gmane.org/gmane.comp.version-control.git/265560/focus=265560
and here:
http://thread.gmane.org/gmane.comp.version-control.git/266777/focus=266777

The issue manifests when trying to clean a large number of untracked directories. In my case this scenario triggered by a test suite running in the repository creating a directory for each test resulting in a build directory with ~100000 sub directories that needs to be cleaned. For some extreme cases, clean times of more than 1h have been observed.

With this series, the time to clean an untracked directory containing 100000 sub directories goes from 61s to 1.7s.

The main change is to switch the repository check in clean.c:remove_dirs from using refs.c:resolve_gitlink_ref to setup.c:is_git_directory.

One potential issue that is_git_directory contains the following check:

	if (getenv(DB_ENVIRONMENT)) {
		if (access(getenv(DB_ENVIRONMENT), X_OK))
			return 0;
	}

I'm not sure how this will affect this usecase (checking for some other nested git repo). Please give some thought to this when reviewing.

Jeff King also expressed concerns that we may have similar performance issues in other commands and that it could be good to unify these "is this a repo?"-checks. This series only attempts to solve the git-clean case.

Erik Elfström (3):
t7300: add tests to document behavior of clean and nested git
p7300: added performance tests for clean
clean: improve performance when removing lots of directories

 builtin/clean.c       | 23 ++++++++++++---
 t/perf/p7300-clean.sh | 37 +++++++++++++++++++++++
 t/t7300-clean.sh      | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 138 insertions(+), 4 deletions(-)
 create mode 100755 t/perf/p7300-clean.sh
Signature · 1 line
2.4.0.rc0.37.ga3b75b3
#1 lore raw

PATCH 1/3 t7300: add tests to document behavior of clean and nested git

---

These tests were added so that I could understand the corner case behaviors of clean and nested repositories and document the changes in behavior that this series will cause. The ones marked as expect failure will be changed to expect success later in the series.

 t/t7300-clean.sh | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 insertions(+)
t/t7300-clean.sh +82 -0
index 99be5d9..cfdf6d4 100755
@@ -455,6 +455,88 @@ test_expect_success 'nested git work tree' '
 	! test -d bar
 '
 
+test_expect_failure 'nested git (only init) should be kept' '
+	rm -fr foo bar &&
+	mkdir foo bar &&
+	(
+		cd foo &&
+		git init
+	) &&
+	(
+		cd bar &&
+		>goodbye.people
+	) &&
+	git clean -f -d &&
+	test -f foo/.git/HEAD &&
+	! test -d bar
+'
+
+test_expect_failure 'nested git (bare) should be kept' '
+	rm -fr foo bar &&
+	mkdir foo bar &&
+	(
+		cd foo &&
+		git init --bare
+	) &&
+	(
+		cd bar &&
+		>goodbye.people
+	) &&
+	git clean -f -d &&
+	test -f foo/HEAD &&
+	! test -d bar
+'
+
+test_expect_success 'giving path in nested git work tree will remove it' '
+	rm -fr foo &&
+	mkdir foo &&
+	(
+		cd foo &&
+		git init &&
+		mkdir -p bar/baz &&
+		cd bar/baz &&
+		>hello.world
+		git add . &&
+		git commit -a -m nested
+	) &&
+	git clean -f -d foo/bar/baz &&
+	test -f foo/.git/HEAD &&
+	test -d foo/bar/ &&
+	! test -d foo/bar/baz
+'
+
+test_expect_success 'giving path to nested .git will not remove it' '
+	rm -fr foo &&
+	mkdir foo bar &&
+	(
+		cd foo &&
+		git init &&
+		>hello.world
+		git add . &&
+		git commit -a -m nested
+	) &&
+	git clean -f -d foo/.git &&
+	test -f foo/.git/HEAD &&
+	test -d foo/.git/refs &&
+	test -d foo/.git/objects &&
+	test -d bar/
+'
+
+test_expect_success 'giving path to nested .git/ will remove contents' '
+	rm -fr foo bar &&
+	mkdir foo bar &&
+	(
+		cd foo &&
+		git init &&
+		>hello.world
+		git add . &&
+		git commit -a -m nested
+	) &&
+	git clean -f -d foo/.git/ &&
+	test 0 = $(ls -A foo/.git | wc -l) &&
+	test -d foo/.git
+'
+
 test_expect_success 'force removal of nested git work tree' '
 	rm -fr foo bar baz &&
 	mkdir -p foo bar baz/boo &&
Signature · 1 line
2.4.0.rc0.37.ga3b75b3
#2 lore raw

Re: [PATCH 1/3] t7300: add tests to document behavior of clean and nested git

Quoted 7 lines · Erik Elfström

---

t/t7300-clean.sh +0 -0
index 99be5d9..cfdf6d4 100755

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 463

Quoted 9 lines
t/t7300-clean.sh +9 -0
@@ -455,6 +455,88 @@ test_expect_success 'nested git work tree' '
+test_expect_success 'giving path in nested git work tree will remove it' '
+       rm -fr foo &&
+       mkdir foo &&
+       (
+               cd foo &&
+               git init &&
+               mkdir -p bar/baz &&
+               cd bar/baz &&
+               >hello.world

In my earlier review, I utterly forgot to mention the broken &&-chain here and throughout the patch.

Quoted 8 lines
t/t7300-clean.sh +8 -0
+               git add . &&
+               git commit -a -m nested
+       ) &&
+       git clean -f -d foo/bar/baz &&
+       test -f foo/.git/HEAD &&
+       test -d foo/bar/ &&
+       ! test -d foo/bar/baz
+'
#3 lore raw

Re: [PATCH 1/3] t7300: add tests to document behavior of clean and nested git

Quoted 12 lines · Eric Sunshine
Quoted 11 lines · Erik Elfström

---

t/t7300-clean.sh +4 -0
index 99be5d9..cfdf6d4 100755
@@ -455,6 +455,88 @@ test_expect_success 'nested git work tree' '
+test_expect_success 'giving path in nested git work tree will remove it' '
+       rm -fr foo &&
+       mkdir foo &&
+       (
Quoted 12 lines

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 498

Quoted 5 lines
t/t7300-clean.sh +5 -0
+               cd foo &&
+               git init &&
+               mkdir -p bar/baz &&
+               cd bar/baz &&
+               >hello.world

In my earlier review, I utterly forgot to mention the broken &&-chain here and throughout the patch.

Quoted 3 lines
t/t7300-clean.sh +3 -0
+               git add . &&
+               git commit -a -m nested
+       ) &&

Beside that, all "cd" commands should be done within an own sub-shell. In other words, something like this:

		mkdir -p bar/baz &&
		(
               		cd bar/baz &&
               		>hello.world &&
               		git add . &&
               		git commit -a -m nested
		)

Side note:
Needed to drop Eric:
An error occurred while sending mail. The mail server responded: Requested action not taken: mailbox unavailable invalid DNS MX or A/AAAA resource record. Please check the message recipient [email protected] and try again.

#4 lore raw

Re: [PATCH 1/3] t7300: add tests to document behavior of clean and nested git

In reply to Erik Elfström
Quoted 11 lines · Erik Elfström

---

t/t7300-clean.sh +1 -0
index 99be5d9..cfdf6d4 100755
@@ -455,6 +455,88 @@ test_expect_success 'nested git work tree' '
        ! test -d bar
 '
 
+test_expect_failure 'nested git (only init) should be kept' '

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 468

Quoted 10 lines
t/t7300-clean.sh +10 -0
+       rm -fr foo bar &&
+       mkdir foo bar &&
+       (
+               cd foo &&
+               git init
+       ) &&
+       (
+               cd bar &&
+               >goodbye.people
+       ) &&

(minor; ignore if desired) The above could be simplified to:

    rm -fr foo bar &&
    git init foo &&
    mkdir bar &&
    >bar/goodbye.people &&

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 471

Quoted 3 lines
t/t7300-clean.sh +3 -0
+       git clean -f -d &&
+       test -f foo/.git/HEAD &&
+       ! test -d bar

Here and elsewhere, you could instead use test_path_is_file() and test_path_is_missing(), respectively.

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 484

Quoted 13 lines
t/t7300-clean.sh +13 -0
+'
+
+test_expect_failure 'nested git (bare) should be kept' '
+       rm -fr foo bar &&
+       mkdir foo bar &&
+       (
+               cd foo &&
+               git init --bare
+       ) &&
+       (
+               cd bar &&
+               >goodbye.people
+       ) &&

Simplified:

    rm -fr foo bar &&
    git init --bare foo &&
    mkdir bar &&
    >bar/goodbye.people &&
Quoted 10 lines
t/t7300-clean.sh +10 -0
+       git clean -f -d &&
+       test -f foo/HEAD &&
+       ! test -d bar
+'
+
+test_expect_success 'giving path in nested git work tree will remove it' '
+       rm -fr foo &&
+       mkdir foo &&
+       (
+               cd foo &&

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 504

Quoted 10 lines
t/t7300-clean.sh +10 -0
+               git init &&
+               mkdir -p bar/baz &&
+               cd bar/baz &&
+               >hello.world
+               git add . &&
+               git commit -a -m nested
+       ) &&
+       git clean -f -d foo/bar/baz &&
+       test -f foo/.git/HEAD &&
+       test -d foo/bar/ &&

Alternative, here and elsewhere: test_path_is_dir()

Quoted 22 lines
t/t7300-clean.sh +22 -0
+       ! test -d foo/bar/baz
+'
+
+test_expect_success 'giving path to nested .git will not remove it' '
+       rm -fr foo &&
+       mkdir foo bar &&
+       (
+               cd foo &&
+               git init &&
+               >hello.world
+               git add . &&
+               git commit -a -m nested
+       ) &&
+       git clean -f -d foo/.git &&
+       test -f foo/.git/HEAD &&
+       test -d foo/.git/refs &&
+       test -d foo/.git/objects &&
+       test -d bar/
+'
+
+test_expect_success 'giving path to nested .git/ will remove contents' '
+       rm -fr foo bar &&

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 536

Quoted 10 lines
t/t7300-clean.sh +10 -0
+       mkdir foo bar &&
+       (
+               cd foo &&
+               git init &&
+               >hello.world
+               git add . &&
+               git commit -a -m nested
+       ) &&
+       git clean -f -d foo/.git/ &&
+       test 0 = $(ls -A foo/.git | wc -l) &&

Although in the latest POSIX, -A may not be portable.

Alternative: test_dir_is_empty()

Quoted 8 lines
t/t7300-clean.sh +3 -1
+       test -d foo/.git
+'
+
 test_expect_success 'force removal of nested git work tree' '
        rm -fr foo bar baz &&
        mkdir -p foo bar baz/boo &&
--

2.4.0.rc0.37.ga3b75b3

#5 lore raw

Re: [PATCH 1/3] t7300: add tests to document behavior of clean and nested git

will fix!

Quoted 126 lines · Eric Sunshine
Quoted 11 lines · Erik Elfström

---

t/t7300-clean.sh +1 -0
index 99be5d9..cfdf6d4 100755
@@ -455,6 +455,88 @@ test_expect_success 'nested git work tree' '
        ! test -d bar
 '
 
+test_expect_failure 'nested git (only init) should be kept' '

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 468

Quoted 10 lines
t/t7300-clean.sh +10 -0
+       rm -fr foo bar &&
+       mkdir foo bar &&
+       (
+               cd foo &&
+               git init
+       ) &&
+       (
+               cd bar &&
+               >goodbye.people
+       ) &&

(minor; ignore if desired) The above could be simplified to:

    rm -fr foo bar &&
    git init foo &&
    mkdir bar &&
    >bar/goodbye.people &&

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 471

Quoted 3 lines
t/t7300-clean.sh +3 -0
+       git clean -f -d &&
+       test -f foo/.git/HEAD &&
+       ! test -d bar

Here and elsewhere, you could instead use test_path_is_file() and test_path_is_missing(), respectively.

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 484

Quoted 13 lines
t/t7300-clean.sh +13 -0
+'
+
+test_expect_failure 'nested git (bare) should be kept' '
+       rm -fr foo bar &&
+       mkdir foo bar &&
+       (
+               cd foo &&
+               git init --bare
+       ) &&
+       (
+               cd bar &&
+               >goodbye.people
+       ) &&

Simplified:

    rm -fr foo bar &&
    git init --bare foo &&
    mkdir bar &&
    >bar/goodbye.people &&
Quoted 10 lines
t/t7300-clean.sh +10 -0
+       git clean -f -d &&
+       test -f foo/HEAD &&
+       ! test -d bar
+'
+
+test_expect_success 'giving path in nested git work tree will remove it' '
+       rm -fr foo &&
+       mkdir foo &&
+       (
+               cd foo &&

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 504

Quoted 10 lines
t/t7300-clean.sh +10 -0
+               git init &&
+               mkdir -p bar/baz &&
+               cd bar/baz &&
+               >hello.world
+               git add . &&
+               git commit -a -m nested
+       ) &&
+       git clean -f -d foo/bar/baz &&
+       test -f foo/.git/HEAD &&
+       test -d foo/bar/ &&

Alternative, here and elsewhere: test_path_is_dir()

Quoted 22 lines
t/t7300-clean.sh +22 -0
+       ! test -d foo/bar/baz
+'
+
+test_expect_success 'giving path to nested .git will not remove it' '
+       rm -fr foo &&
+       mkdir foo bar &&
+       (
+               cd foo &&
+               git init &&
+               >hello.world
+               git add . &&
+               git commit -a -m nested
+       ) &&
+       git clean -f -d foo/.git &&
+       test -f foo/.git/HEAD &&
+       test -d foo/.git/refs &&
+       test -d foo/.git/objects &&
+       test -d bar/
+'
+
+test_expect_success 'giving path to nested .git/ will remove contents' '
+       rm -fr foo bar &&

t/t7300-clean.sh test_expect_success 'nested git work tree' ' · line 536

Quoted 10 lines
t/t7300-clean.sh +10 -0
+       mkdir foo bar &&
+       (
+               cd foo &&
+               git init &&
+               >hello.world
+               git add . &&
+               git commit -a -m nested
+       ) &&
+       git clean -f -d foo/.git/ &&
+       test 0 = $(ls -A foo/.git | wc -l) &&

Although in the latest POSIX, -A may not be portable.

Alternative: test_dir_is_empty()

Quoted 8 lines
t/t7300-clean.sh +3 -1
+       test -d foo/.git
+'
+
 test_expect_success 'force removal of nested git work tree' '
        rm -fr foo bar baz &&
        mkdir -p foo bar baz/boo &&
--

2.4.0.rc0.37.ga3b75b3

#6 lore raw

PATCH 2/3 p7300: added performance tests for clean

In reply to Erik Elfström
---
 t/perf/p7300-clean.sh | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100755 t/perf/p7300-clean.sh
t/perf/p7300-clean.sh new file +37 -0
new file mode 100755
index 0000000..3f56fb2
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description="Test git-clean performance"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+test_expect_success 'setup untracked directory with many sub dirs' '
+	rm -rf 500_sub_dirs 50000_sub_dirs clean_test_dir &&
+	mkdir 500_sub_dirs 50000_sub_dirs clean_test_dir &&
+	for i in $(seq 1 500)
+	do
+		mkdir 500_sub_dirs/dir$i
+	done &&
+	for i in $(seq 1 100)
+	do
+		cp -r 500_sub_dirs 50000_sub_dirs/dir$i
+	done
+'
+
+test_perf 'clean many untracked sub dirs, check for nested git' '
+	rm -rf clean_test_dir/50000_sub_dirs_cpy &&
+	cp -r 50000_sub_dirs clean_test_dir/50000_sub_dirs_cpy &&
+	git clean -q -f -d  clean_test_dir/ &&
+	test 0 = $(ls -A clean_test_dir/ | wc -l)
+'
+
+test_perf 'clean many untracked sub dirs, ignore nested git' '
+	rm -rf clean_test_dir/50000_sub_dirs_cpy &&
+	cp -r 50000_sub_dirs clean_test_dir/50000_sub_dirs_cpy &&
+	git clean -q -f -f -d  clean_test_dir/ &&
+	test 0 = $(ls -A clean_test_dir/ | wc -l)
+'
+
+test_done
Signature · 1 line
2.4.0.rc0.37.ga3b75b3
#7 lore raw

Re: [PATCH 2/3] p7300: added performance tests for clean

Quoted 15 lines · Erik Elfström
---
 t/perf/p7300-clean.sh | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
 create mode 100755 t/perf/p7300-clean.sh
t/perf/p7300-clean.sh new file +3 -0
new file mode 100755
index 0000000..3f56fb2
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description="Test git-clean performance"

t/perf/p7300-clean.sh line 13

Quoted 10 lines
t/perf/p7300-clean.sh new file +10 -0
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+test_expect_success 'setup untracked directory with many sub dirs' '
+	rm -rf 500_sub_dirs 50000_sub_dirs clean_test_dir &&
+	mkdir 500_sub_dirs 50000_sub_dirs clean_test_dir &&
+	for i in $(seq 1 500)

I think "seq" is bash-only, and can be easily replaced by "test_seq"

Quoted 4 lines
t/perf/p7300-clean.sh new file +4 -0
+	do
+		mkdir 500_sub_dirs/dir$i
+	done &&
+	for i in $(seq 1 100)

t/perf/p7300-clean.sh line 27

Quoted 10 lines
t/perf/p7300-clean.sh new file +10 -0
+	do
+		cp -r 500_sub_dirs 50000_sub_dirs/dir$i
+	done
+'
+
+test_perf 'clean many untracked sub dirs, check for nested git' '
+	rm -rf clean_test_dir/50000_sub_dirs_cpy &&
+	cp -r 50000_sub_dirs clean_test_dir/50000_sub_dirs_cpy &&
+	git clean -q -f -d  clean_test_dir/ &&
+	test 0 = $(ls -A clean_test_dir/ | wc -l)

Is the "ls -A" portable on all systems:
http://pubs.opengroup.org/onlinepubs/009695399/utilities/ls.html

My feeling is that the "emptiness" of a directory can by tested by simply removing it:

t/perf/p7300-clean.sh line 29

Quoted 2 lines
t/perf/p7300-clean.sh new file +2 -0
+	git clean -q -f -d  clean_test_dir/ &&
+	rmdir clean_test_dir

(And similar below)

#8 lore raw

Re: [PATCH 2/3] p7300: added performance tests for clean

Quoted 15 lines · Torsten Bögershausen
Quoted 14 lines · Erik Elfström

---

t/perf/p7300-clean.sh new file +6 -0
new file mode 100755
index 0000000..3f56fb2
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description="Test git-clean performance"
+
+. ./perf-lib.sh
+
Quoted 8 lines

t/perf/p7300-clean.sh line 13

Quoted 7 lines
t/perf/p7300-clean.sh new file +7 -0
+test_perf_large_repo
+test_checkout_worktree
+
+test_expect_success 'setup untracked directory with many sub dirs' '
+     rm -rf 500_sub_dirs 50000_sub_dirs clean_test_dir &&
+     mkdir 500_sub_dirs 50000_sub_dirs clean_test_dir &&
+     for i in $(seq 1 500)

I think "seq" is bash-only, and can be easily replaced by "test_seq"

test_seq is definitely desirable. 'seq' is not present on some systems I use.

Quoted 3 lines

t/perf/p7300-clean.sh line 15

Quoted 3 lines
t/perf/p7300-clean.sh new file +2 -0
+     do
+             mkdir 500_sub_dirs/dir$i

You may want:

    mkdir 500_sub_dirs/dir$i || return $?

to catch failure of 'mkdir'.

Quoted 4 lines

t/perf/p7300-clean.sh line 19

Quoted 4 lines
t/perf/p7300-clean.sh new file +4 -0
+     done &&
+     for i in $(seq 1 100)
+     do
+             cp -r 500_sub_dirs 50000_sub_dirs/dir$i

Ditto:

    cp -r 500_sub_dirs 50000_sub_dirs/dir$i || return $?
Quoted 12 lines

t/perf/p7300-clean.sh line 27

Quoted 8 lines
t/perf/p7300-clean.sh new file +8 -0
+     done
+'
+
+test_perf 'clean many untracked sub dirs, check for nested git' '
+     rm -rf clean_test_dir/50000_sub_dirs_cpy &&
+     cp -r 50000_sub_dirs clean_test_dir/50000_sub_dirs_cpy &&
+     git clean -q -f -d  clean_test_dir/ &&
+     test 0 = $(ls -A clean_test_dir/ | wc -l)

Is the "ls -A" portable on all systems:
http://pubs.opengroup.org/onlinepubs/009695399/utilities/ls.html

Quoted 4 lines

My feeling is that the "emptiness" of a directory can by tested by simply removing it:

t/perf/p7300-clean.sh line 29

Quoted 2 lines
t/perf/p7300-clean.sh new file +2 -0
+     git clean -q -f -d  clean_test_dir/ &&
+     rmdir clean_test_dir

(And similar below)

There's also the test_dir_is_empty() function which makes the intent even clearer than 'rmdir'.

#9 lore raw

Re: [PATCH 2/3] p7300: added performance tests for clean

Will fix!

Also I forgot to ask, does anyone have a good way of moving the copy out of the performance timing?

After the fix this test spends more time copying than cleaning and that is not so good. I'm not very good at shell scripting and the only way I could think of was to make multiple copies at the start and then in the test check and clean the first non empty directory. That feels kind of ugly and will fail if a different number of iterations per test is used. Any suggestions?

I looked a bit at the framework code but I couldn't figure out if it was easy to add a "setup" option to be called be before each iteration.

Quoted 65 lines · Eric Sunshine
Quoted 15 lines · Torsten Bögershausen
Quoted 14 lines · Erik Elfström

---

t/perf/p7300-clean.sh new file +6 -0
new file mode 100755
index 0000000..3f56fb2
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description="Test git-clean performance"
+
+. ./perf-lib.sh
+
Quoted 8 lines

t/perf/p7300-clean.sh line 13

Quoted 7 lines
t/perf/p7300-clean.sh new file +7 -0
+test_perf_large_repo
+test_checkout_worktree
+
+test_expect_success 'setup untracked directory with many sub dirs' '
+     rm -rf 500_sub_dirs 50000_sub_dirs clean_test_dir &&
+     mkdir 500_sub_dirs 50000_sub_dirs clean_test_dir &&
+     for i in $(seq 1 500)

I think "seq" is bash-only, and can be easily replaced by "test_seq"

test_seq is definitely desirable. 'seq' is not present on some systems I use.

Quoted 3 lines

t/perf/p7300-clean.sh line 15

Quoted 3 lines
t/perf/p7300-clean.sh new file +2 -0
+     do
+             mkdir 500_sub_dirs/dir$i

You may want:

    mkdir 500_sub_dirs/dir$i || return $?

to catch failure of 'mkdir'.

Quoted 4 lines

t/perf/p7300-clean.sh line 19

Quoted 4 lines
t/perf/p7300-clean.sh new file +4 -0
+     done &&
+     for i in $(seq 1 100)
+     do
+             cp -r 500_sub_dirs 50000_sub_dirs/dir$i

Ditto:

    cp -r 500_sub_dirs 50000_sub_dirs/dir$i || return $?
Quoted 12 lines

t/perf/p7300-clean.sh line 27

Quoted 8 lines
t/perf/p7300-clean.sh new file +8 -0
+     done
+'
+
+test_perf 'clean many untracked sub dirs, check for nested git' '
+     rm -rf clean_test_dir/50000_sub_dirs_cpy &&
+     cp -r 50000_sub_dirs clean_test_dir/50000_sub_dirs_cpy &&
+     git clean -q -f -d  clean_test_dir/ &&
+     test 0 = $(ls -A clean_test_dir/ | wc -l)

Is the "ls -A" portable on all systems:
http://pubs.opengroup.org/onlinepubs/009695399/utilities/ls.html

Quoted 4 lines

My feeling is that the "emptiness" of a directory can by tested by simply removing it:

t/perf/p7300-clean.sh line 29

Quoted 2 lines
t/perf/p7300-clean.sh new file +2 -0
+     git clean -q -f -d  clean_test_dir/ &&
+     rmdir clean_test_dir

(And similar below)

There's also the test_dir_is_empty() function which makes the intent even clearer than 'rmdir'.

#10 lore raw

PATCH 3/3 clean: improve performance when removing lots of directories

In reply to Erik Elfström

Before this change, clean used resolve_gitlink_ref to check for the presence of nested git repositories. This had the drawback of creating a ref_cache entry for every directory that should potentially be cleaned. The linear search through the ref_cache list caused a massive performance hit for large number of directories.

Teach clean.c:remove_dirs to use setup.c:is_git_directory instead. is_git_directory will actually open HEAD and parse the HEAD ref but this implies a nested git repository and should be rare when cleaning.

Using is_git_directory should give a more standardized check for what is and what isn't a git repository but also gives a slight behavioral change. We will now detect and respect bare and empty nested git repositories (only init run). Update t7300 to reflect this.

The time to clean an untracked directory containing 100000 sub directories went from 61s to 1.7s after this change.

---
 builtin/clean.c  | 23 +++++++++++++++++++----
 t/t7300-clean.sh |  4 ++--
 2 files changed, 21 insertions(+), 6 deletions(-)
builtin/clean.c +19 -4
index 98c103f..e951bd9 100644
@@ -10,7 +10,6 @@
 #include "cache.h"
 #include "dir.h"
 #include "parse-options.h"
-#include "refs.h"
 #include "string-list.h"
 #include "quote.h"
 #include "column.h"
@@ -148,6 +147,24 @@ static int exclude_cb(const struct option *opt, const char *arg, int unset)
 	return 0;
 }
 
+static int is_git_repository(struct strbuf *path)
+{
+	int ret = 0;
+	if (is_git_directory(path->buf))
+		ret = 1;
+	else {
+		int orig_path_len = path->len;
+		if (path->buf[orig_path_len - 1] != '/')
+			strbuf_addch(path, '/');
+		strbuf_addstr(path, ".git");
+		if (is_git_directory(path->buf))
+			ret = 1;
+		strbuf_setlen(path, orig_path_len);
+	}
+
+	return ret;
+}
+
 static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
 		int dry_run, int quiet, int *dir_gone)
 {
@@ -155,13 +172,11 @@ static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
 	struct strbuf quoted = STRBUF_INIT;
 	struct dirent *e;
 	int res = 0, ret = 0, gone = 1, original_len = path->len, len;
-	unsigned char submodule_head[20];
 	struct string_list dels = STRING_LIST_INIT_DUP;
 
 	*dir_gone = 1;
 
-	if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
-			!resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
+	if ((force_flag & REMOVE_DIR_KEEP_NESTED_GIT) && is_git_repository(path)) {
 		if (!quiet) {
 			quote_path_relative(path->buf, prefix, &quoted);
 			printf(dry_run ?  _(msg_would_skip_git_dir) : _(msg_skip_git_dir),
t/t7300-clean.sh +2 -2
index cfdf6d4..ada569d 100755
@@ -455,7 +455,7 @@ test_expect_success 'nested git work tree' '
 	! test -d bar
 '
 
-test_expect_failure 'nested git (only init) should be kept' '
+test_expect_success 'nested git (only init) should be kept' '
 	rm -fr foo bar &&
 	mkdir foo bar &&
 	(
@@ -471,7 +471,7 @@ test_expect_failure 'nested git (only init) should be kept' '
 	! test -d bar
 '
 
-test_expect_failure 'nested git (bare) should be kept' '
+test_expect_success 'nested git (bare) should be kept' '
 	rm -fr foo bar &&
 	mkdir foo bar &&
 	(
Signature · 1 line
2.4.0.rc0.37.ga3b75b3
#11 lore raw

Re: [PATCH 3/3] clean: improve performance when removing lots of directories

Quoted 11 lines · Erik Elfström

Before this change, clean used resolve_gitlink_ref to check for the presence of nested git repositories. This had the drawback of creating a ref_cache entry for every directory that should potentially be cleaned. The linear search through the ref_cache list caused a massive performance hit for large number of directories.

Teach clean.c:remove_dirs to use setup.c:is_git_directory instead. is_git_directory will actually open HEAD and parse the HEAD ref but this implies a nested git repository and should be rare when cleaning.

Quoted 7 lines

Using is_git_directory should give a more standardized check for what is and what isn't a git repository but also gives a slight behavioral change. We will now detect and respect bare and empty nested git repositories (only init run). Update t7300 to reflect this.

The time to clean an untracked directory containing 100000 sub directories went from 61s to 1.7s after this change.

Impressive.

Quoted 2 lines

It is customary for your sign-off to be last.

More below...

Quoted 7 lines

---

builtin/clean.c +0 -0
index 98c103f..e951bd9 100644
@@ -148,6 +147,24 @@ static int exclude_cb(const struct option *opt, const char *arg, int unset)
        return 0;

builtin/clean.c static int exclude_cb(const struct option *opt, const char *arg, int unset) · line 157

Quoted 10 lines
builtin/clean.c +8 -0
 }
 
+static int is_git_repository(struct strbuf *path)
+{
+       int ret = 0;
+       if (is_git_directory(path->buf))
+               ret = 1;
+       else {
+               int orig_path_len = path->len;
+               if (path->buf[orig_path_len - 1] != '/')

Minor: I don't know how others feel about it, but I always find it a bit disturbing to see a potential negative array access without a safety check that orig_path_len is not 0, either directly in the conditional or as a documenting assert().

Quoted 13 lines
builtin/clean.c +10 -0
+                       strbuf_addch(path, '/');
+               strbuf_addstr(path, ".git");
+               if (is_git_directory(path->buf))
+                       ret = 1;
+               strbuf_setlen(path, orig_path_len);
+       }
+
+       return ret;
+}
+
 static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                int dry_run, int quiet, int *dir_gone)
 {
#12 lore raw

Re: [PATCH 3/3] clean: improve performance when removing lots of directories

Quoted 38 lines · Eric Sunshine
Quoted 11 lines · Erik Elfström

Before this change, clean used resolve_gitlink_ref to check for the presence of nested git repositories. This had the drawback of creating a ref_cache entry for every directory that should potentially be cleaned. The linear search through the ref_cache list caused a massive performance hit for large number of directories.

Teach clean.c:remove_dirs to use setup.c:is_git_directory instead. is_git_directory will actually open HEAD and parse the HEAD ref but this implies a nested git repository and should be rare when cleaning.

Quoted 7 lines

Using is_git_directory should give a more standardized check for what is and what isn't a git repository but also gives a slight behavioral change. We will now detect and respect bare and empty nested git repositories (only init run). Update t7300 to reflect this.

The time to clean an untracked directory containing 100000 sub directories went from 61s to 1.7s after this change.

Impressive.

Quoted 2 lines

It is customary for your sign-off to be last.

More below...

Quoted 9 lines

---

builtin/clean.c +0 -0
index 98c103f..e951bd9 100644
@@ -148,6 +147,24 @@ static int exclude_cb(const struct option *opt, const char *arg, int unset)
        return 0;
 }
 
Quoted 14 lines

builtin/clean.c static int exclude_cb(const struct option *opt, const char *arg, int unset) · line 157

Quoted 8 lines
builtin/clean.c +8 -0
+static int is_git_repository(struct strbuf *path)
+{
+       int ret = 0;
+       if (is_git_directory(path->buf))
+               ret = 1;
+       else {
+               int orig_path_len = path->len;
+               if (path->buf[orig_path_len - 1] != '/')

Minor: I don't know how others feel about it, but I always find it a bit disturbing to see a potential negative array access without a safety check that orig_path_len is not 0, either directly in the conditional or as a documenting assert().

I think I would prefer to accept empty input and return false rather than assert. What to you think about:

static int is_git_repository(struct strbuf *path)

{
    int ret = 0;
    size_t orig_path_len = path->len;
    if (orig_path_len == 0)
        ret = 0;
    else if (is_git_directory(path->buf))
        ret = 1;
    else {
        if (path->buf[orig_path_len - 1] != '/')
            strbuf_addch(path, '/');
        strbuf_addstr(path, ".git");
        if (is_git_directory(path->buf))
            ret = 1;
        strbuf_setlen(path, orig_path_len);
    }

    return ret;
}

Also I borrowed this pattern from remove_dirs and it has the same problem. Should I add something like this as a separate commit?

builtin/clean.c +0 -0
index ccffd8a..88850e3 100644
@@ -173,7 +173,8 @@ static int remove_dirs(struct strbuf *path, const

char *prefix, int force_flag,

        DIR *dir;
        struct strbuf quoted = STRBUF_INIT;
        struct dirent *e;
-       int res = 0, ret = 0, gone = 1, original_len = path->len, len;
+       int res = 0, ret = 0, gone = 1;
+       size_t original_len = path->len, len;
        struct string_list dels = STRING_LIST_INIT_DUP;

        *dir_gone = 1;
@@ -201,6 +202,7 @@ static int remove_dirs(struct strbuf *path, const
char *prefix, int force_flag,
                return res;
        }
+       assert(original_len > 0 && "expects non-empty path");
        if (path->buf[original_len - 1] != '/')
                strbuf_addch(path, '/');
Quoted 13 lines
Quoted 13 lines
builtin/clean.c +10 -0
+                       strbuf_addch(path, '/');
+               strbuf_addstr(path, ".git");
+               if (is_git_directory(path->buf))
+                       ret = 1;
+               strbuf_setlen(path, orig_path_len);
+       }
+
+       return ret;
+}
+
 static int remove_dirs(struct strbuf *path, const char *prefix, int force_flag,
                int dry_run, int quiet, int *dir_gone)
 {
#13 lore raw

Re: [PATCH 3/3] clean: improve performance when removing lots of directories

Quoted 23 lines · erik elfström
Quoted 10 lines · Eric Sunshine
Quoted 9 lines · Erik Elfström
builtin/clean.c +5 -0
index 98c103f..e951bd9 100644
+static int is_git_repository(struct strbuf *path)
+{
+       int ret = 0;
+       if (is_git_directory(path->buf))
+               ret = 1;
Quoted 8 lines

builtin/clean.c

Quoted 3 lines
builtin/clean.c +3 -0
+       else {
+               int orig_path_len = path->len;
+               if (path->buf[orig_path_len - 1] != '/')

Minor: I don't know how others feel about it, but I always find it a bit disturbing to see a potential negative array access without a safety check that orig_path_len is not 0, either directly in the conditional or as a documenting assert().

I think I would prefer to accept empty input and return false rather than assert. What to you think about:

Quoted 6 lines

static int is_git_repository(struct strbuf *path)

{
    int ret = 0;
    size_t orig_path_len = path->len;
    if (orig_path_len == 0)
        ret = 0;

My concern in raising the issue is that someone reviewing the patch or reading the code later won't necessarily know whether you took the potential negative array access into account and dismissed it as "can't happen", or if you overlooked the possibility altogether. Had there been an explicit check in the code (either assert() or other special handling such as returning 'false'), a comment in the code, or mention in the commit message, then it would have been clear that you took the case into consideration, and I wouldn't have worried about it.

As for the this proposed version of is_git_repository(), I don't have strong feelings, and can formulate arguments either way. If it doesn't make sense for is_git_repository() ever to be called with empty input, then assert() may be the better choice for documenting that fact. However, if you foresee some need for allowing empty input, or if you audited the functionality and found that it can already be called with empty input, then returning 'false' makes sense. Use your best judgment.

Quoted 23 lines
    else if (is_git_directory(path->buf))
        ret = 1;
    else {
        if (path->buf[orig_path_len - 1] != '/')
            strbuf_addch(path, '/');
        strbuf_addstr(path, ".git");
        if (is_git_directory(path->buf))
            ret = 1;
        strbuf_setlen(path, orig_path_len);
    }

    return ret;
}

Also I borrowed this pattern from remove_dirs and it has the same problem. Should I add something like this as a separate commit?

builtin/clean.c +0 -0
index ccffd8a..88850e3 100644

builtin/clean.c static int remove_dirs(struct strbuf *path, const · line 208

Quoted 7 lines
builtin/clean.c +1 -0
@@ -201,6 +202,7 @@ static int remove_dirs(struct strbuf *path, const
 char *prefix, int force_flag,
                return res;
        }
 
+       assert(original_len > 0 && "expects non-empty path");
        if (path->buf[original_len - 1] != '/')
                strbuf_addch(path, '/');

I personally wouldn't mind such a patch. (I'm not sure that the string within the assert() adds much value, and it's a not-much-used idiom within the Git source.)