git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] clean: respect pathspecs with "-d"

From
Jeff King <peff@peff.net>
Date
Mar 10, 2014, 20:37 UTC
Message-ID
<20140310203730.GD24568@sigill.intra.peff.net>
In-Reply-To
<20140310200235.GA14112@ruderich.org>
On Mon, Mar 10, 2014 at 09:02:35PM +0100, Simon Ruderich wrote:
Show 21 quoted lines
> On Mon, Mar 10, 2014 at 01:22:15PM -0400, Jeff King wrote:
> > +test_expect_success 'git clean -d respects pathspecs' '
> > +	mkdir foo &&
> > +	mkdir foobar &&
> > +	git clean -df foobar &&
> > +	test_path_is_dir foo &&
> > +	test_path_is_missing foobar
> > +'
> > +
> >  test_done
> 
> I think we should also test removing foo, which was also in the
> original report, to make sure we don't match prefixes, e.g.:
> 
> test_expect_success 'git clean -d respects pathspecs' '
> 	mkdir foo &&
> 	mkdir foobar &&
> 	git clean -df foo &&
> 	test_path_is_missing foo &&
> 	test_path_is_dir foobar
> '

Yeah, it probably makes sense to test both ways (though the root cause and fix are the same). Those mkdirs need to be "mkdir -p", though.

Here's an updated patch with your suggestion:
-- >8 --
Subject: clean: respect pathspecs with "-d"

git-clean uses read_directory to fill in a `struct dir` with potential hits. However, read_directory does not actually check against our pathspec. It uses a simplified version that may turn up false positives. As a result, we need to check that any hits match our pathspec. We do so reliably for non-directories. For directories, if "-d" is not given we check that the pathspec matched exactly (i.e., we are even stricter, and require an explicit "git clean foo" to clean "foo/"). But if "-d" is given, rather than relaxing the exact match to allow a recursive match, we do not check the pathspec at all.

This regression was introduced in 113f10f (Make git-clean a builtin, 2007-11-11).

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/clean.c  |  5 +++--
 t/t7300-clean.sh | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/builtin/clean.c b/builtin/clean.c
index 114d7bf..31c1488 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -947,14 +947,15 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 		if (pathspec.nr)
 			matches = dir_path_match(ent, &pathspec, 0, NULL);
 
+		if (pathspec.nr && !matches)
+			continue;
+
 		if (S_ISDIR(st.st_mode)) {
 			if (remove_directories || (matches == MATCHED_EXACTLY)) {
 				rel = relative_path(ent->name, prefix, &buf);
 				string_list_append(&del_list, rel);
 			}
 		} else {
-			if (pathspec.nr && !matches)
-				continue;
 			rel = relative_path(ent->name, prefix, &buf);
 			string_list_append(&del_list, rel);
 		}
diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh
index 710be90..74de814 100755
--- a/t/t7300-clean.sh
+++ b/t/t7300-clean.sh
@@ -511,4 +511,20 @@ test_expect_success SANITY 'git clean -d with an unreadable empty directory' '
 	! test -d foo
 '
 
+test_expect_success 'git clean -d respects pathspecs (dir is prefix of pathspec)' '
+	mkdir -p foo &&
+	mkdir -p foobar &&
+	git clean -df foobar &&
+	test_path_is_dir foo &&
+	test_path_is_missing foobar
+'
+
+test_expect_success 'git clean -d respects pathspecs (pathspec is prefix of dir)' '
+	mkdir -p foo &&
+	mkdir -p foobar &&
+	git clean -df foo &&
+	test_path_is_missing foo &&
+	test_path_is_dir foobar
+'
+
 test_done
-- 
1.9.0.403.g7a2f4b0
Previous: Simon RuderichNext: Jeff King
Message 5 of 6 in “Accidentially deleted directory, bug in git clean -d?”
  1. Robin PedersenMar 10, 2014
  2. clean: respect pathspecs with "-d"Jeff King, Mar 10, 2014
  3. Jeff KingMar 10, 2014
  4. Simon RuderichMar 10, 2014
  5. Jeff KingMar 10, 2014
  6. clean: simplify dir/not-dir logicJeff King, Mar 10, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.