Skip to content

Commit 105ec9a

Browse files
committed
clean: further clean-up of implementation around "--force"
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 <[email protected]>
1 parent 12a4883 commit 105ec9a

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

Documentation/config/clean.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
clean.requireForce::
22
A boolean to make git-clean refuse to delete files unless -f
3-
or -i is given. Defaults to true.
3+
is given. Defaults to true.

Documentation/git-clean.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,21 @@ OPTIONS
3737
--force::
3838
If the Git configuration variable clean.requireForce is not set
3939
to false, 'git clean' will refuse to delete files or directories
40-
unless given -f or -i. Git will refuse to modify untracked
40+
unless given -f. Git will refuse to modify untracked
4141
nested git repositories (directories with a .git subdirectory)
4242
unless a second -f is given.
4343

4444
-i::
4545
--interactive::
4646
Show what would be done and clean files interactively. See
4747
``Interactive mode'' for details.
48+
Configuration variable `clean.requireForce` is ignored, as
49+
this mode gives its own safety protection by going interactive.
4850

4951
-n::
5052
--dry-run::
5153
Don't actually remove anything, just show what would be done.
52-
Configuration variable clean.requireForce is ignored, as
54+
Configuration variable `clean.requireForce` is ignored, as
5355
nothing will be deleted anyway.
5456

5557
-q::

builtin/clean.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -950,13 +950,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
950950
argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
951951
0);
952952

953-
/* Dry run won't remove anything, so requiring force makes no sense */
954-
if (dry_run)
955-
require_force = 0;
956-
957-
if (require_force != 0 && !force && !interactive)
958-
die(_("clean.requireForce is true and neither -f nor -i given:"
959-
" refusing to clean"));
953+
if (require_force != 0 && !force && !interactive && !dry_run)
954+
die(_("clean.requireForce is true and -f not given: refusing to clean"));
960955

961956
if (force > 1)
962957
rm_flags = 0;

t/t7300-clean.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ test_expect_success 'clean.requireForce and -f' '
407407
408408
'
409409

410+
test_expect_success 'clean.requireForce and --interactive' '
411+
git clean --interactive </dev/null >output 2>error &&
412+
test_grep ! "requireForce is true and" error &&
413+
test_grep "\*\*\* Commands \*\*\*" output
414+
'
415+
410416
test_expect_success 'core.excludesfile' '
411417
412418
echo excludes >excludes &&

0 commit comments

Comments
 (0)