# [PATCH] [GSOC] prune_worktrees(): reimplement with dir_iterator

4 messages from 2017-04-01 to 2017-04-01. Participants: Robert Stanca, Junio C Hamano.
Thread: https://gitlist.dev/t/45580

## Robert Stanca, 2017-04-01 01:21

Subject: [PATCH] [GSOC] prune_worktrees(): reimplement with dir_iterator
Message-ID: <1491009687-10419-1-git-send-email-robert.stanca7@gmail.com>
URL: https://gitlist.dev/e/1491009687-10419-1-git-send-email-robert.stanca7%40gmail.com

```
Replaces recursive traversing of opendir with dir_iterator

Signed-off-by: Robert Stanca <robert.stanca7@gmail.com>
---
 builtin/worktree.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 9993ded..7cfd78c 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -10,6 +10,8 @@
 #include "refs.h"
 #include "utf8.h"
 #include "worktree.h"
+#include "iterator.h"
+#include "dir-iterator.h"
 
 static const char * const worktree_usage[] = {
 	N_("git worktree add [<options>] <path> [<branch>]"),
@@ -91,30 +93,25 @@ static void prune_worktrees(void)
 {
 	struct strbuf reason = STRBUF_INIT;
 	struct strbuf path = STRBUF_INIT;
-	DIR *dir = opendir(git_path("worktrees"));
-	struct dirent *d;
+	struct dir_iterator *diter = dir_iterator_begin(git_path("worktrees"));
 	int ret;
-	if (!dir)
-		return;
-	while ((d = readdir(dir)) != NULL) {
-		if (is_dot_or_dotdot(d->d_name))
-			continue;
+
+	while (dir_iterator_advance(diter) == ITER_OK) {
 		strbuf_reset(&reason);
-		if (!prune_worktree(d->d_name, &reason))
+		if (!prune_worktree(diter->relative_path, &reason))
 			continue;
 		if (show_only || verbose)
 			printf("%s\n", reason.buf);
 		if (show_only)
 			continue;
 		strbuf_reset(&path);
-		strbuf_addstr(&path, git_path("worktrees/%s", d->d_name));
+		strbuf_addstr(&path, git_path("worktrees/%s", diter->relative_path));
 		ret = remove_dir_recursively(&path, 0);
 		if (ret < 0 && errno == ENOTDIR)
 			ret = unlink(path.buf);
 		if (ret)
 			error_errno(_("failed to remove '%s'"), path.buf);
 	}
-	closedir(dir);
 	if (!show_only)
 		rmdir(git_path("worktrees"));
 	strbuf_release(&reason);
-- 
2.7.4


```

## Junio C Hamano, 2017-04-01 02:29

Subject: Re: [PATCH] [GSOC] prune_worktrees(): reimplement with dir_iterator
Message-ID: <xmqqlgrkx3l5.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqlgrkx3l5.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <1491009687-10419-1-git-send-email-robert.stanca7@gmail.com>

```
Robert Stanca <robert.stanca7@gmail.com> writes:

> Replaces recursive traversing of opendir with dir_iterator

The original code for this one, and also the other one, is not
recursive traversing.  This one enumerates all the _direct_
subdirectories of ".git/worktrees", and feeds them to
prune_worktree() without recursing.  The other one scans all the
files _directly_ underneath ".git/objects/pack" to find the ones
that begin with the packtmp prefix, and unlinks them without
recursing.  Neither of them touches anything in subdirectory of
".git/worktrees/" nor ".git/objects/pack/".

Using dir_iterator() to make it recursive is not just overkill but
is a positively wrong change, isn't it?

```

## Robert Stanca, 2017-04-01 13:29

Subject: Re: [PATCH] [GSOC] prune_worktrees(): reimplement with dir_iterator
Message-ID: <CAJYcaSOMkoAnW82g=moQxz0H9t7C+F++V4Ob1-UiTLiw=gm_0g@mail.gmail.com>
URL: https://gitlist.dev/e/CAJYcaSOMkoAnW82g%3DmoQxz0H9t7C%2BF%2B%2BV4Ob1-UiTLiw%3Dgm_0g%40mail.gmail.com
In-Reply-To: <xmqqlgrkx3l5.fsf@gitster.mtv.corp.google.com>

```
On Sat, Apr 1, 2017 at 5:29 AM, Junio C Hamano <gitster@pobox.com> wrote:
>
> Robert Stanca <robert.stanca7@gmail.com> writes:
>
> > Replaces recursive traversing of opendir with dir_iterator
>
> The original code for this one, and also the other one, is not
> recursive traversing.  This one enumerates all the _direct_
> subdirectories of ".git/worktrees", and feeds them to
> prune_worktree() without recursing.  The other one scans all the
> files _directly_ underneath ".git/objects/pack" to find the ones
> that begin with the packtmp prefix, and unlinks them without
> recursing.  Neither of them touches anything in subdirectory of
> ".git/worktrees/" nor ".git/objects/pack/".
>
> Using dir_iterator() to make it recursive is not just overkill but
> is a positively wrong change, isn't it?


Thanks for the review, and yes the commit message is misleading (my
bad there). I understood that remove_temp_files has no recursion
component to it and it removes all ".tmp-id-pack-" files from
/objects/pack , but shouldn't dir-iterator work even if there's no
recursion level?
My understanding of dir-iterator is that it is used for directory
iteration (recursive or not) and where are no subdirectories it's
basically recursive with level = 0 .

```

## Junio C Hamano, 2017-04-01 18:33

Subject: Re: [PATCH] [GSOC] prune_worktrees(): reimplement with dir_iterator
Message-ID: <xmqq7f34ugf2.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq7f34ugf2.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CAJYcaSOMkoAnW82g=moQxz0H9t7C+F++V4Ob1-UiTLiw=gm_0g@mail.gmail.com>

```
Robert Stanca <robert.stanca7@gmail.com> writes:

> On Sat, Apr 1, 2017 at 5:29 AM, Junio C Hamano <gitster@pobox.com> wrote:
>>
>> Using dir_iterator() to make it recursive is not just overkill but
>> is a positively wrong change, isn't it?
>
> Thanks for the review, and yes the commit message is misleading (my
> bad there). I understood that remove_temp_files has no recursion
> component to it and it removes all ".tmp-id-pack-" files from
> /objects/pack , but shouldn't dir-iterator work even if there's no
> recursion level?

The point is what should happen when somebody (perhaps a newer
version of Git, or a third-party add-on that works with Git) adds a
subdirectory in .git/objects/pack/ or .git/worktrees/.  The answer
is that files and directories in such a subdirectory must not be
touched, because prune_worktrees() or remove_temporary_files() do
not know what these files and directories are.

The dir-iterator API does not allow you to say "only scan the
directory without recursing into the subdirectories" so your code
ends up recursing into them, without knowing what the files and
directories inside are (or are not).  As Peff mentioned in his
review on the other one, if we add a knob to dir-iterator API to
allow you to tell it not to recurse, then it would make it possible
to do a faithful conversion from the original, but without such a
change, you are changing the behaviour.

```
