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

Re: [BUG] git clean -X skips a directory containing only ignored files

From
MSMichael Schubert <mschub@elegosoft.com>
Date
Jan 31, 2012, 14:47 UTC
Message-ID
<4F27FF01.6040706@elegosoft.com>
In-Reply-To
<CA+yLL67J-7U9z7HVvq5wTc1g4_UCtqYfEyqdt7XR5zDqvQN5NA@mail.gmail.com>
On 01/31/2012 12:36 AM, Paul Berry wrote:
Show 36 quoted lines
> I am trying to use "git clean -X" to remove object files (which
> are gitignored) from my source tree, but it appears to miss
> object files that are in a subdirectory without any git-tracked
> files:
> 
> $ git init test
> Initialized empty Git repository in /home/pberry/tmp/test/.git/
> $ cd test
> $ mkdir foo
> $ touch foo/bar.o
> $ echo '*.o' > .gitignore
> $ git add .gitignore
> $ git commit -mgitignore
> [master (root-commit) 6b5ffcb] gitignore
>  1 files changed, 1 insertions(+), 0 deletions(-)
>  create mode 100644 .gitignore
> $ git status
> # On branch master
> nothing to commit (working directory clean)
> $ git clean -d -X -f
> $ ls foo
> bar.o
> 
> It seems to me that bar.o should have been removed, because
> according to the git-clean docs, -X means "Remove only files
> ignored by git", and bar.o is definitely being ignored by git.
> 
> 
> It looks like a very similar bug was reported back in 2010, but
> not fixed:
> http://git.661346.n2.nabble.com/BUG-git-clean-X-behaviour-when-gitignore-has-sub-directory-entries-td5575307.html.
> I've confirmed that the workaround mentioned by Jonathan Nieder
> in that thread fixes my problem too (removing "dir.flags |=
> DIR_SHOW_OTHER_DIRECTORIES;" from builtin/clean.c).  However I'm
> guessing from Jonathan's comments that it would be better to fix
> this bug elsewhere (somewhere in dir.c perhaps).

Removing DIR_SHOW_OTHER_DIRECTORIES just happens to not trigger this particular "bug" but breaks pretty much everything else.

As a workaround, you could explicitly add the directory to your gitignore file.

Here's a test:
-- >8 --
Subject: [PATCH] t7300-clean: show known breakage with "git clean -d -X"

"git clean -d -X" fails for directories containing only untracked files. Example:

	$ ls -R .
	.:
	foo
	./foo:
	bar.o
	$ cat .gitignore
	*.o
	$ git clean -d -X -f
	$ ! test -d foo || echo fail
Reported-by: Paul Berry <stereotype441@gmail.com>
Signed-off-by: Michael Schubert <mschub@elegosoft.com>
---
 t/t7300-clean.sh |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh
index 800b536..0b6d545 100755
--- a/t/t7300-clean.sh
+++ b/t/t7300-clean.sh
@@ -332,6 +332,13 @@ test_expect_success 'git clean -d -X' '
 
 '
 
+test_expect_failure 'git clean -d -X' '
+  mkdir -p a/b &&
+  touch a/b/c.o &&
+  git clean -d -X &&
+  ! test -d a
+'
+
 test_expect_success 'clean.requireForce defaults to true' '
 
 	git config --unset clean.requireForce &&
-- 
1.7.9.174.g356eff6
Previous: Paul BerryNext: Paul Berry
Message 2 of 5 in “[BUG] git clean -X skips a directory containing only ignored files”
  1. Paul BerryJan 30, 2012
  2. Michael SchubertJan 31, 2012
  3. Paul BerryJan 31, 2012
  4. Andrew WongJan 31, 2012
  5. Michael SchubertFeb 1, 2012

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.