From 12a4883feb53d08a80b3c049415105b0543f27a5 Mon Sep 17 00:00:00 2001 From: Sergey Organov Date: Sun, 3 Mar 2024 12:50:32 +0300 Subject: [PATCH 1/2] clean: improve -n and -f implementation and documentation What -n actually does in addition to its documented behavior is ignoring of configuration variable clean.requireForce, that makes sense provided -n prevents files removal anyway. So, first, document this in the manual, and then modify implementation to make this more explicit in the code. Improved implementation also stops to share single internal variable 'force' between command-line -f option and configuration variable clean.requireForce, resulting in more clear logic. Two error messages with slightly different text depending on if clean.requireForce was explicitly set or not, are merged into a single one. The resulting error message now does not mention -n as well, as it neither matches intended clean.requireForce usage nor reflects clarified implementation. Documentation of clean.requireForce is changed accordingly. Signed-off-by: Sergey Organov Signed-off-by: Junio C Hamano --- Documentation/config/clean.txt | 4 ++-- Documentation/git-clean.txt | 2 ++ builtin/clean.c | 23 +++++++++-------------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/Documentation/config/clean.txt b/Documentation/config/clean.txt index f05b9403b5..b19ca210f3 100644 --- a/Documentation/config/clean.txt +++ b/Documentation/config/clean.txt @@ -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 + or -i is given. Defaults to true. diff --git a/Documentation/git-clean.txt b/Documentation/git-clean.txt index 69331e3f05..662eebb852 100644 --- a/Documentation/git-clean.txt +++ b/Documentation/git-clean.txt @@ -49,6 +49,8 @@ OPTIONS -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:: diff --git a/builtin/clean.c b/builtin/clean.c index d90766cad3..41502dcb0d 100644 --- a/builtin/clean.c +++ b/builtin/clean.c @@ -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,17 @@ 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;" + /* 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 (force > 1) rm_flags = 0; From 105ec9ae8d0d100994ddf1ee5e99e4616558ff90 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 3 Mar 2024 14:06:00 -0800 Subject: [PATCH 2/2] 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 --- Documentation/config/clean.txt | 2 +- Documentation/git-clean.txt | 6 ++++-- builtin/clean.c | 9 ++------- t/t7300-clean.sh | 6 ++++++ 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Documentation/config/clean.txt b/Documentation/config/clean.txt index b19ca210f3..c0188ead4e 100644 --- a/Documentation/config/clean.txt +++ b/Documentation/config/clean.txt @@ -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. diff --git a/Documentation/git-clean.txt b/Documentation/git-clean.txt index 662eebb852..fd17165416 100644 --- a/Documentation/git-clean.txt +++ b/Documentation/git-clean.txt @@ -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:: diff --git a/builtin/clean.c b/builtin/clean.c index 41502dcb0d..29efe84153 100644 --- a/builtin/clean.c +++ b/builtin/clean.c @@ -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; diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh index 611b3dd3ae..1f7201eb60 100755 --- a/t/t7300-clean.sh +++ b/t/t7300-clean.sh @@ -407,6 +407,12 @@ test_expect_success 'clean.requireForce and -f' ' ' +test_expect_success 'clean.requireForce and --interactive' ' + git clean --interactive output 2>error && + test_grep ! "requireForce is true and" error && + test_grep "\*\*\* Commands \*\*\*" output +' + test_expect_success 'core.excludesfile' ' echo excludes >excludes &&