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

git clean -X deletes ignored directory

From
YFYves Fischer <yvesf@xapek.org>
Date
Oct 7, 2020, 19:32 UTC
Message-ID
<995c4c10c68d39e84aa48ae967f81da6@xapek.org>
Hello everyone,
I found a behavior with git-clean that appears to be unexpected.
It is related to the change in 95c11ec which, if I read the history correctly, got later corrected in cada730.

Example (in git repo after build): ====== ./git init test cd test mkdir ignored-subdir touch test.c test.o ignored-subdir/some-file echo \*.o > .gitignore echo ignored-subdir >> .gitignore ../git add . ../git commit -m test ../git clean -X -d -n '*.o' # Would remove ignored-subdir/ <--- why? # Would remove test.o ======

In the example I would have expected that the "ignored-subdir" is not deleted because it's not matched by the path argument. The manpage of git-clean says:
> If any optional <path>... arguments are given, only those paths are affected.
I believe the issue is fixed in cada730 only for files. It seems to persist for directories.
The attached diff has a test that tries to cover this.

Kind regards, Yves

----
diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh
index cb5e34d94c..77d834d34a 100755
--- a/t/t7300-clean.sh
+++ b/t/t7300-clean.sh
@@ -746,4 +746,49 @@ test_expect_success 'clean untracked paths by pathspec' '
        test_must_be_empty actual
 '
 
+# fixed by cada730
+test_expect_success 'git clean x1' '
+
+       git init clean-x1 &&
+       touch clean-x1/test.c clean-x1/test.o clean-x1/other-untracked-file &&
+       echo \*.o >> clean-x1/.gitignore &&
+       echo other-untracked-file >> clean-x1/.gitignore &&
+       git -C clean-x1 add . &&
+       git -C clean-x1 commit -m setup &&
+       git -C clean-x1 clean -X -d -f "*.o" &&
+       ! test -f clean-x1/test.o &&
+       test -f clean-x1/other-untracked-file
+
+'
+
+# fixed by cada730
+test_expect_success 'git clean x2' '
+
+       git init clean-x2 &&
+       touch clean-x2/test.c clean-x2/test.o clean-x2/other-untracked-file &&
+       echo \*.o >> clean-x2/.gitignore &&
+       echo other-untracked-file >> clean-x2/.gitignore &&
+       git -C clean-x2 add . &&
+       git -C clean-x2 commit -m setup &&
+       git -C clean-x2 clean -X -f "*.o" &&
+       ! test -f clean-x2/test.o &&
+       test -f clean-x2/other-untracked-file
+
+'
+
+test_expect_success 'git clean x3' '
+
+       git init clean-x3 &&
+       mkdir other-untracked-dir &&
+       touch clean-x3/test.c clean-x3/test.o clean-x3/other-untracked-dir/foo &&
+       echo \*.o >> clean-x3/.gitignore &&
+       echo other-untracked-dir >> clean-x3/.gitignore &&
+       git -C clean-x3 add . &&
+       git -C clean-x3 commit -m setup &&
+       git -C clean-x3 clean -X -f "*.o" &&
+       ! test -f clean-x3/test.o &&
+       test -f clean-x3/other-untracked-dir/foo
+
+'
+
 test_done
Message 1 of 1 in “git clean -X deletes ignored directory”
  1. Yves FischerOct 7, 2020

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.