git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] clean: improve -n and -f implementation and documentation

From
Sergey Organov <sorganov@gmail.com>
Date
Mar 1, 2024, 14:34 UTC
Message-ID
<87frxam35f.fsf@osv.gnss.ru>
In-Reply-To
<51a196c0-ea57-4ec5-99ea-c3f09cd90962@gmail.com>
Jean-Noël Avila <avila.jn@gmail.com> writes:
Show 36 quoted lines
> Putting my documentation/translator hat:
>
> Le 29/02/2024 à 20:07, Sergey Organov a écrit :
>> 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.
>> 
>> The error messages now do not mention -n as well, as it seems
>> unnecessary and does not reflect clarified implementation.
>> 
>> Signed-off-by: Sergey Organov <sorganov@gmail.com>
>> ---
>>  Documentation/git-clean.txt |  2 ++
>>  builtin/clean.c             | 26 +++++++++++++-------------
>>  2 files changed, 15 insertions(+), 13 deletions(-)
>> 
>> diff --git a/Documentation/git-clean.txt b/Documentation/git-clean.txt
>> index 69331e3f05a1..662eebb85207 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.
>
> Please use backticks for options, configuration and environment names:
> `clean.requireForce`

I did consider this. However, existing text already has exactly this one unquoted, so I just did the same. Hopefully it will be fixed altogether later, or are you positive I better resend the patch with quotes?

Show 69 quoted lines
>>  
>>  -q::
>>  --quiet::
>> diff --git a/builtin/clean.c b/builtin/clean.c
>> index d90766cad3a0..fcc50d08ee9b 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,21 +946,21 @@ 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; "
>> +	/* Dry run won't remove anything, so requiring force makes no sense */
>> +	if(dry_run)
>> +		require_force = 0;
>> +
>> +	if (!force && !interactive) {
>> +		if (require_force > 0)
>> +			die(_("clean.requireForce set to true and neither -f, nor -i given; "
>> +				  "refusing to clean"));
>> +		else if (require_force < 0)
>> +			die(_("clean.requireForce defaults to true and neither -f, nor -i given; "
>>  				  "refusing to clean"));
>> -		else
>> -			die(_("clean.requireForce defaults to true and neither -i, -n, nor -f given;"
>> -				  " refusing to clean"));
>>  	}
>>  
>
> The last two cases can be coalesced into a single case (the last one),
> because the difference in the messages does not bring more information
> to the user.

Did you misread the patch? There are only 2 cases here, the last (third) one is marked with '-' (removed). Too easy to misread this, I'd say. New code is:

		if (require_force > 0)
			die(_("clean.requireForce set to true and neither -f, nor -i given; "
				  "refusing to clean"));
		else if (require_force < 0)
			die(_("clean.requireForce defaults to true and neither -f, nor -i given; "

and is basically unchanged from the original, except reference to '-n' has been removed. Btw, is now comma needed after -f, and isn't it better to substitute ':' for ';'?

Thank you for review!
-- Sergey Organov
Previous: Jean-Noël AvilaNext: Kristoffer Haugsbakk
Message 25 of 38 in “what should "git clean -n -f [-d] [-x] <pattern>" do?”
  1. Junio C HamanoJan 9, 2024
  2. Sergey OrganovJan 9, 2024
  3. Elijah NewrenJan 19, 2024
  4. Sergey OrganovJan 23, 2024
  5. Junio C HamanoJan 23, 2024
  6. Sergey OrganovJan 24, 2024
  7. Junio C HamanoJan 24, 2024
  8. Sergey OrganovJan 25, 2024
  9. Junio C HamanoJan 25, 2024
  10. Sergey OrganovJan 25, 2024
  11. Sergey OrganovJan 25, 2024
  12. Junio C HamanoJan 26, 2024
  13. Sergey OrganovJan 26, 2024
  14. Junio C HamanoJan 27, 2024
  15. Sergey OrganovJan 27, 2024
  16. Kristoffer HaugsbakkJan 29, 2024
  17. Sergey OrganovJan 31, 2024
  18. Sergey OrganovJan 29, 2024
  19. Jeff KingJan 29, 2024
  20. Sergey OrganovJan 29, 2024
  21. Jeff KingJan 30, 2024
  22. Junio C HamanoJan 30, 2024
  23. clean: improve -n and -f implementation and documentationSergey Organov, Feb 29, 2024
  24. Jean-Noël AvilaMar 1, 2024
  25. Sergey OrganovMar 1, 2024
  26. Kristoffer HaugsbakkMar 1, 2024
  27. Junio C HamanoMar 1, 2024
  28. Jean-Noël AVILAMar 2, 2024
  29. Sergey OrganovMar 2, 2024
  30. Junio C HamanoMar 2, 2024
  31. Sergey OrganovMar 2, 2024
  32. Sergey OrganovMar 3, 2024
  33. Junio C HamanoMar 1, 2024
  34. Junio C HamanoMar 1, 2024
  35. Sergey OrganovMar 1, 2024
  36. Junio C HamanoMar 2, 2024
  37. Sergey OrganovMar 2, 2024
  38. clean: improve -n and -f implementation and documentationSergey Organov, Mar 3, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.