Merge branch 'so/clean-dry-run-without-force'

The implementation in "git clean" that makes "-n" and "-i" ignore
clean.requireForce has been simplified, together with the
documentation.

* so/clean-dry-run-without-force:
  clean: further clean-up of implementation around "--force"
  clean: improve -n and -f implementation and documentation
This commit is contained in:
Junio C Hamano 2024-03-14 14:05:23 -07:00
commit 066124da88
4 changed files with 18 additions and 18 deletions

View file

@ -1,3 +1,3 @@
clean.requireForce::
A boolean to make git-clean do nothing unless given -f,
-i, or -n. Defaults to true.
A boolean to make git-clean refuse to delete files unless -f
is given. Defaults to true.

View 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,10 +45,14 @@ 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
nothing will be deleted anyway.
-q::
--quiet::

View file

@ -25,7 +25,7 @@
#include "help.h"
#include "prompt.h"
static int force = -1; /* unset */
static int require_force = -1; /* unset */
static int interactive;
static struct string_list del_list = STRING_LIST_INIT_DUP;
static unsigned int colopts;
@ -128,7 +128,7 @@ static int git_clean_config(const char *var, const char *value,
}
if (!strcmp(var, "clean.requireforce")) {
force = !git_config_bool(var, value);
require_force = git_config_bool(var, value);
return 0;
}
@ -920,7 +920,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
{
int i, res;
int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;
int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
int ignored_only = 0, force = 0, errors = 0, gone = 1;
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
struct strbuf abs_path = STRBUF_INIT;
struct dir_struct dir = DIR_INIT;
@ -946,22 +946,12 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
};
git_config(git_clean_config, NULL);
if (force < 0)
force = 0;
else
config_set = 1;
argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
0);
if (!interactive && !dry_run && !force) {
if (config_set)
die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
"refusing to clean"));
else
die(_("clean.requireForce defaults to true and neither -i, -n, nor -f 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;

View file

@ -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 &&