public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx•de>
To: Devste Devste <devstemail@gmail•com>
Cc: git@vger•kernel.org
Subject: Re: git clean deletes negated pathspec if directory causing data loss
Date: Thu, 30 Oct 2025 09:49:41 +0100 (CET)	[thread overview]
Message-ID: <a77f4e6f-86a3-ea15-feb5-4d08636ffd94@gmx.de> (raw)
In-Reply-To: <CANM0SV3qgqjWeAu+NvfPxeDLgo6-eRWKmi9xkpUtN6w7LJh7oQ@mail.gmail.com>

Hi Devste,

On Wed, 29 Oct 2025, Devste Devste wrote:

> Anyone?
> 
> On Sun, 5 Oct 2025 at 13:33, Devste Devste <devstemail@gmail•com> wrote:
> >
> > Just to clarify: it's not about the negation not working, but clean
> > ignores the pathspec for ignored directories completely. I only want
> > to delete .rej and .log files, but it will also delete gitignored
> > directories (that may or may not contain any .rej or .log files)
> >
> > On Sun, 5 Oct 2025 at 13:27, Devste Devste <devstemail@gmail•com> wrote:
> > >
> > > I am using:
> > > git version 2.51.0.windows.1
> > >
> > > Run:
> > > echo .idea/dictionaries >> .gitignore
> > > mkdir -p .idea/dictionaries
> > > touch .idea/dictionaries/foo.xml
> > > git clean -f -f -d -X --dry-run -- ':!/.idea/dictionaries' ':*.rej' ':/*/*.log'
> > >
> > > Outputs:
> > > Would remove .idea/dictionaries/
> > >
> > > No matter how you specify the pathspec (':!.idea',...) it always wants
> > > to delete the .idea/dictionaries directory, even though it does not
> > > contain any .rej or .log files and is explicitly set to excluded

I can reproduce, both on Windows and on Linux. (Note that I prefer the
`:(exclude).idea/directories` form because it is more descriptive and it
also does not run afoul of Bash's special handling of the exclamation
point).

Unfortunately, the code in question is quite convoluted, and the intention
is also not quite clear. The main problem seems to be to agree on what
`-X` should mean in conjunction with `:(exclude)`.

One interpretation (which I assume is yours): When `-X` implicitly adds
items to be removed, `:(exclude)` should be able to remove them again.

The interpretation of the authors of the logic I see in the source code,
though, seem to treat the `-X` as a completely separate mechanism that
overrides whatever `:(exclude)` may be specified on the command-line.

Honestly, I am unsure how to resolve this, especially given that the
overall architecture of `dir.c` (which contains the business logic of
exclusions specified e.g. by `.gitignore) seems to have grown so
organically as to result in a complex, hard-to-reason-about state.

In case the Git maintainer (whose call it is, ultimately, on which side to
land regarding above-mentioned options) sides with your interpretation,
here is a patch to add a regression test demonstrating your use case:

-- snipsnap --
diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh
index 6f16f389319..6bc7c42a572 100755
--- a/t/t7300-clean.sh
+++ b/t/t7300-clean.sh
@@ -362,6 +362,14 @@ test_expect_success 'git clean -d -X with ignored tracked directory' '
 
 '
 
+test_expect_failure 'git clean -d -X with :(exclude)' '
+	test_when_finished "rm -rf build" &&
+	mkdir -p build &&
+	touch build/lib.so &&
+	git clean -d -X -- ":(exclude)build" &&
+	test_path_is_file build/lib.sh
+'
+
 test_expect_success 'clean.requireForce defaults to true' '
 
 	git config --unset clean.requireForce &&

  reply	other threads:[~2025-10-30  8:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-05 11:27 git clean deletes negated pathspec if directory causing data loss Devste Devste
2025-10-05 11:33 ` Devste Devste
2025-10-29 20:00   ` Devste Devste
2025-10-30  8:49     ` Johannes Schindelin [this message]
2025-10-30 17:04       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a77f4e6f-86a3-ea15-feb5-4d08636ffd94@gmx.de \
    --to=johannes.schindelin@gmx$(echo .)de \
    --cc=devstemail@gmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox