]> git.ipfire.org Git - thirdparty/git.git/commitdiff
clean: further clean-up of implementation around "--force"
authorJunio C Hamano <gitster@pobox.com>
Sun, 3 Mar 2024 22:06:00 +0000 (14:06 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 4 Mar 2024 22:05:13 +0000 (14:05 -0800)
We clarified how "clean.requireForce" interacts with the "--dry-run"
option in the previous commit, both in the implementation and in the
documentation.  Even when "git clean" (without other options) is
required to be used with "--force" (i.e. either clean.requireForce
is unset, or explicitly set to true) to protect end-users from
casual invocation of the command by mistake, "--dry-run" does not
require "--force" to be used, because it is already its own
protection mechanism by being a no-op to the working tree files.

The previous commit, however, missed another clean-up opportunity
around the same area.  Just like in the "--dry-run" mode, the
command in the "--interactive" mode does not require "--force",
either.  This is because by going interactive and giving the end
user one more chance to confirm, the mode itself is serving as its
own protection mechanism.

Let's take things one step further, and unify the code that defines
interaction between "--force" and these two other options.  Just
like we added explanation for the reason why "--dry-run" does not
honor "clean.requireForce", give an explanation for the reason why
"--interactive" makes "clean.requireForce" to be ignored.

Finally, add some tests to show the interaction between "--force"
and "--interactive".  We already have tests that show interaction
between "--force" and "--dry-run", but didn't test "--interactive".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config/clean.txt
Documentation/git-clean.txt
builtin/clean.c
t/t7300-clean.sh

index b19ca210f39b57353c0eac3681638238df266cd9..c0188ead4e6cd8b2b38a5feecb20c24897e03161 100644 (file)
@@ -1,3 +1,3 @@
 clean.requireForce::
        A boolean to make git-clean refuse to delete files unless -f
-       or -i is given. Defaults to true.
+       is given. Defaults to true.
index 662eebb852076a22bde18c1fffdb3d1fe3026c77..fd171654163c1fc4e34de3b24cdd4063a604dae1 100644 (file)
@@ -37,7 +37,7 @@ OPTIONS
 --force::
        If the Git configuration variable clean.requireForce is not set
        to false, 'git clean' will refuse to delete files or directories
-       unless given -f or -i.  Git will refuse to modify untracked
+       unless given -f.  Git will refuse to modify untracked
        nested git repositories (directories with a .git subdirectory)
        unless a second -f is given.
 
@@ -45,11 +45,13 @@ OPTIONS
 --interactive::
        Show what would be done and clean files interactively. See
        ``Interactive mode'' for details.
+       Configuration variable `clean.requireForce` is ignored, as
+       this mode gives its own safety protection by going interactive.
 
 -n::
 --dry-run::
        Don't actually remove anything, just show what would be done.
-       Configuration variable clean.requireForce is ignored, as
+       Configuration variable `clean.requireForce` is ignored, as
        nothing will be deleted anyway.
 
 -q::
index 41502dcb0ddeedaa80257097747de86714927de4..29efe841537b341776bdca2fc70f3df4d333e066 100644 (file)
@@ -950,13 +950,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
        argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
                             0);
 
-       /* Dry run won't remove anything, so requiring force makes no sense */
-       if (dry_run)
-               require_force = 0;
-
-       if (require_force != 0 && !force && !interactive)
-               die(_("clean.requireForce is true and neither -f nor -i given:"
-                                 " refusing to clean"));
+       if (require_force != 0 && !force && !interactive && !dry_run)
+               die(_("clean.requireForce is true and -f not given: refusing to clean"));
 
        if (force > 1)
                rm_flags = 0;
index 611b3dd3aedb44c9b5d4657f3058c72a60258cb0..1f7201eb60caf9b31fd3db1a83dc834bb145157c 100755 (executable)
@@ -407,6 +407,12 @@ test_expect_success 'clean.requireForce and -f' '
 
 '
 
+test_expect_success 'clean.requireForce and --interactive' '
+       git clean --interactive </dev/null >output 2>error &&
+       test_grep ! "requireForce is true and" error &&
+       test_grep "\*\*\* Commands \*\*\*" output
+'
+
 test_expect_success 'core.excludesfile' '
 
        echo excludes >excludes &&