threads / discuss / 59520

Add a way to disable «git clean» per repo

Subject: Add a way to disable «git clean» per repo

## tl;dr

4 messages between Apr 1, 2023 and Apr 10, 2023.

replies: 3people: 3as markdown or json

Guillem Jover· Apr 1, 2023, 20:50 UTC · lore
Hi!
[ I initially filed this in the Debian bug tracking system as it
  seemed to me that filing a feature request on a mailing list had
  a great potential to get lost. But I guess I can try anyway. :) ]

For repositories that are not tracking code, say when storing your ~/ under git, or to store say collections of data files such as photos, texts or similar, you might end up using .gitignore to unclutter «git status». The problem is that both ignored and non-ignored untracked files can be “precious”, as in not version-tracked by losing them might imply data loss.

Accidentally running «git clean -xdf» or «git clean -Xdf» might be catastrophic there. But for the ~/ case (or any such tracking in a parent of git trees, this is even worse, as an accidental «cd» too much while in some code repo might end up accidentally recursively running those «git clean» on an unexpected working tree and all of its subdirectories (except for other git working trees).

I tend to be rather careful with this, but I recently had a scare where this happened to me, and lost a few (not essential) files before I noticed and Ctrl-C'd git. I then set out to try to disable git clean on these situations, but I see no way to do that as aliases do not work with built-ins, and adding a ~/bin/git wrapper to check for this seems extremely cumbersome.

It would thus be nice if there was an option that could be set to completely disable «git clean» on a repo. I guess it might make sense to disable for any untracked file, or perhaps for ignored and/or non-ignored. So that these kind of work trees could be protected.

Thanks, Guillem

Junio C Hamano· Apr 3, 2023, 17:36 UTC · re: Guillem Jover · lore

Re: Add a way to disable «git clean» per repo

Guillem Jover <guillem@hadrons.org> writes:
> Accidentally running «git clean -xdf» or «git clean -Xdf» might be
> catastrophic there.

So would accidentally running "rm -fr" there be catastrophic, too. I doubt it would make much sense to file a feature request to Debian or GNU/FSF to disable "rm -r" in certain directories. I am not sure why "git clean" should be any different.

Commands like "git clean" require "-f" before they become overly destructuve for a reason. clean.requireForce defaults to true for the same reason.

Guillem Jover· Apr 9, 2023, 17:01 UTC · re: Junio C Hamano · lore

Re: Add a way to disable «git clean» per repo

Hi!
On Mon, 2023-04-03 at 10:36:25 -0700, Junio C Hamano wrote:
Show 5 quoted lines
> Guillem Jover <guillem@hadrons.org> writes:
> > Accidentally running «git clean -xdf» or «git clean -Xdf» might be
> > catastrophic there.
> 
> So would accidentally running "rm -fr" there be catastrophic, too.
Sure.
> I doubt it would make much sense to file a feature request to Debian
> or GNU/FSF to disable "rm -r" in certain directories.  I am not sure
> why "git clean" should be any different.

Right, but I see a substantial difference though, «git clean» is part of the git toolset to manage among other things specific work trees, where that behavior is controlled through configuration, and is as such confined within those specific realms, where also the properties of what is being tracked might be different. (With GNU coreutils rm you can confine it within one filesystem with --one-file-system, but TBH I've never had the need to use it AFAIR, and it's not enabled by default.)

> Commands like "git clean" require "-f" before they become overly
> destructuve for a reason.  clean.requireForce defaults to true for
> the same reason.

Right, I guess that's another reason for me why I see these («rm» vs «git clean») as not being entirely comparable. Using «rm» requires in most cases no force options, even when removing recursively (with -r), while «git clean» by default will fail fatally (for all invocations AFAICS?), so perhaps I'm holding it wrong, but when you end up invoking a command very often (f.ex. to make sure your project is building from a clean state), which requires using a force option (because passing -i would become very cumbersome very quick), that becomes a habit or part of your muscle-memory (perhaps a bad one), that means I tend to not pay as much attention as I'd do when running «rm -rf» (also because of the confinement I mentioned above).

For now it occurred to me that I could create dummy git repos in parent directories to act as «git clean» barriers, so that it does not propagate further up in the directory tree, but that still seems like a hack, and I'd really like to protect specific work trees where I know I never want to be able to run «git clean».

Thanks, Guillem

Thomas Guyot· Apr 10, 2023, 13:32 UTC · re: Guillem Jover · lore

Re: Add a way to disable «git clean» per repo

On 2023-04-09 13:01, Guillem Jover wrote:
Show 12 quoted lines
>> I doubt it would make much sense to file a feature request to Debian
>> or GNU/FSF to disable "rm -r" in certain directories.  I am not sure
>> why "git clean" should be any different.
> Right, but I see a substantial difference though, «git clean» is
> part of the git toolset to manage among other things specific work
> trees, where that behavior is controlled through configuration, and
> is as such confined within those specific realms, where also the
> properties of what is being tracked might be different.
> (With GNU coreutils rm you can confine it within one filesystem with
> --one-file-system, but TBH I've never had the need to use it AFAIR,
> and it's not enabled by default.)
>
Hi Guillem,

I agree with Junio here - there are many ways you could adapt your use of Git to safely manage these types of repos... for starters I don't like storing the .git directly in the folders I'm tracking, I always use separate repos with a script to compare the contents vs the real filesystem. Something like:

#!/bin/sh diff -ur `hostname -s`/ / |grep -v "^Only in /"

In this example the repo tracks files from / on multiple hosts using hostname as the first component; diff won't descend into "new" dirs so this runs very fast too, and can be piped to diffstat to get a summary of changed files.

Also if you fear accidentally recalling a dangerous clean command from history, you can set HISTCONTROL=ignoreboth then prefix any dangerous command with a space, or use HISTIGNORE to selectively ignore got clean commands. That works with other dangerous commands like sudo reboot, rm -rf, etc...

The HISTCONTROL way is even better imho as I would always save/recall the command with -n appended (no-op) to review what would be done (ex sometimes there could be a virtualenv in the way that I forgot to remove from ignores) and only when I'm happy I'd remove the -n and insert a space in front so the command doesn't get saved.

Regards,
-- 
Thomas

← back to recent threads