Skip to content

Commit 12a4883

Browse files
sorganovgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b387623 commit 12a4883

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

Documentation/config/clean.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
clean.requireForce::
2-
A boolean to make git-clean do nothing unless given -f,
3-
-i, or -n. Defaults to true.
2+
A boolean to make git-clean refuse to delete files unless -f
3+
or -i is given. Defaults to true.

Documentation/git-clean.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ OPTIONS
4949
-n::
5050
--dry-run::
5151
Don't actually remove anything, just show what would be done.
52+
Configuration variable clean.requireForce is ignored, as
53+
nothing will be deleted anyway.
5254

5355
-q::
5456
--quiet::

builtin/clean.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "help.h"
2626
#include "prompt.h"
2727

28-
static int force = -1; /* unset */
28+
static int require_force = -1; /* unset */
2929
static int interactive;
3030
static struct string_list del_list = STRING_LIST_INIT_DUP;
3131
static unsigned int colopts;
@@ -128,7 +128,7 @@ static int git_clean_config(const char *var, const char *value,
128128
}
129129

130130
if (!strcmp(var, "clean.requireforce")) {
131-
force = !git_config_bool(var, value);
131+
require_force = git_config_bool(var, value);
132132
return 0;
133133
}
134134

@@ -920,7 +920,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
920920
{
921921
int i, res;
922922
int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;
923-
int ignored_only = 0, config_set = 0, errors = 0, gone = 1;
923+
int ignored_only = 0, force = 0, errors = 0, gone = 1;
924924
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
925925
struct strbuf abs_path = STRBUF_INIT;
926926
struct dir_struct dir = DIR_INIT;
@@ -946,22 +946,17 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
946946
};
947947

948948
git_config(git_clean_config, NULL);
949-
if (force < 0)
950-
force = 0;
951-
else
952-
config_set = 1;
953949

954950
argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
955951
0);
956952

957-
if (!interactive && !dry_run && !force) {
958-
if (config_set)
959-
die(_("clean.requireForce set to true and neither -i, -n, nor -f given; "
960-
"refusing to clean"));
961-
else
962-
die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
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:"
963959
" refusing to clean"));
964-
}
965960

966961
if (force > 1)
967962
rm_flags = 0;

0 commit comments

Comments
 (0)