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

Re: [PATCH 3/9] subtree: add 'die_incompatible_opt' function to reduce duplication

From
Philippe Blain <levraiphilippeblain@gmail.com>
Date
Oct 26, 2022, 21:23 UTC
Message-ID
<949edc06-8252-6995-addf-cfba398f73dd@gmail.com>
In-Reply-To
<221021.86v8oddtuj.gmgdl@evledraar.gmail.com>
Hi Ævar,
Le 2022-10-21 à 12:22, Ævar Arnfjörð Bjarmason a écrit :
Show 84 quoted lines
> 
> On Fri, Oct 21 2022, Philippe Blain via GitGitGadget wrote:
> 
>> From: Philippe Blain <levraiphilippeblain@gmail.com>
>>
>> 9a3e3ca2ba (subtree: be stricter about validating flags, 2021-04-27)
>> added validation code to check that options given to 'git subtree <cmd>'
>> made sense with the command being used.
>>
>> Refactor these checks by adding a 'die_incompatible_opt' function to
>> reduce code duplication.
> 
> This looks good, but...
> 
>> Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com>
>> ---
>>  contrib/subtree/git-subtree.sh | 32 ++++++++++++++++++++------------
>>  1 file changed, 20 insertions(+), 12 deletions(-)
>>
>> diff --git a/contrib/subtree/git-subtree.sh b/contrib/subtree/git-subtree.sh
>> index 49ef493ef92..f5eab198c80 100755
>> --- a/contrib/subtree/git-subtree.sh
>> +++ b/contrib/subtree/git-subtree.sh
>> @@ -102,6 +102,14 @@ assert () {
>>  	fi
>>  }
>>  
>> +# Usage: die_incompatible_opt OPTION COMMAND
>> +die_incompatible_opt () {
>> +	assert test "$#" = 2
>> +	opt="$1"
>> +	arg_command="$2"
>> +	die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
>> +}
>> +
>>  main () {
>>  	if test $# -eq 0
>>  	then
>> @@ -176,16 +184,16 @@ main () {
>>  			arg_debug=1
>>  			;;
>>  		--annotate)
>> -			test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
>> +			test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
>>  			arg_split_annotate="$1"
>>  			shift
>>  			;;
> 
> Since this is all in this form I wonder why not (maybe adding some "local" and/or "&&" too while at it):
> 
> 	die_if_other_opt {
> 		assert test "$#" = 3
> 		other="$1"
>                 shift
>                 if test -z "$other"
> 		then
> 			return
> 		fi
> 		[...the rest of your version]
> 	}
> 
> Then:
> 
>>  		--no-annotate)
>> -			test -n "$allow_split" || die "The '$opt' flag does not make sense with 'git subtree $arg_command'."
>> +			test -n "$allow_split" || die_incompatible_opt "$opt" "$arg_command"
> 
> Instead of this:
> 
> 	die_if_other_opt "$allow_split" "$opt" "$arg_command"
> 
> Or actually just:
> 
> 	die_if_other_opt "$allow_split"
> 
> Maybe you disagree, but since the function will see the variables & this
> is purely a helper for this getopts parse loop I think it's fine just to
> assume we can read "$opt" and "$arg_command" there..., so, urm, maybe just:
> 
> 	test -n "$allow_split" || die_incompatible_opt
> 
> ? :) Anyway, an easy bike shed, you should go for whatever variant you
> prefer :) Thanks for indulging me.
> 
Thanks for your suggestion, but I think I'll stick to what I have for now :)
Philippe.
Previous: Ævar Arnfjörð BjarmasonNext: Philippe Blain via GitGitGadget
Message 4 of 20 in “subtree: fix split and merge after annotated tag was squash-merged”
  1. 0/9 subtree: fix split and merge after annotated tag was squash-mergedPhilippe Blain via GitGitGadget, Oct 21, 2022
  2. 3/9 subtree: add 'die_incompatible_opt' function to reduce duplicationPhilippe Blain via GitGitGadget, Oct 21, 2022
  3. Ævar Arnfjörð BjarmasonOct 21, 2022
  4. Philippe BlainOct 26, 2022
  5. 1/9 test-lib-functions: mark 'test_commit' variables as 'local'Philippe Blain via GitGitGadget, Oct 21, 2022
  6. Junio C HamanoOct 21, 2022
  7. Philippe BlainOct 26, 2022
  8. Junio C HamanoOct 26, 2022
  9. 5/9 subtree: define a variable before its first use in 'find_latest_squash'Philippe Blain via GitGitGadget, Oct 21, 2022
  10. 2/9 subtree: use 'git rev-parse --verify [--quiet]' for better error messagesPhilippe Blain via GitGitGadget, Oct 21, 2022
  11. 4/9 subtree: prefix die messages with 'fatal'Philippe Blain via GitGitGadget, Oct 21, 2022
  12. Ævar Arnfjörð BjarmasonOct 21, 2022
  13. Philippe BlainOct 26, 2022
  14. 7/9 subtree: process 'git-subtree-split' trailer in separate functionPhilippe Blain via GitGitGadget, Oct 21, 2022
  15. 9/9 subtree: fix split after annotated tag was squashed mergedPhilippe Blain via GitGitGadget, Oct 21, 2022
  16. Ævar Arnfjörð BjarmasonOct 21, 2022
  17. Eric SunshineOct 21, 2022
  18. Philippe BlainOct 26, 2022
  19. 8/9 subtree: fix squash merging after annotated tag was squashed mergedPhilippe Blain via GitGitGadget, Oct 21, 2022
  20. 6/9 subtree: use named variables instead of "$@" in cmd_pullPhilippe Blain via GitGitGadget, Oct 21, 2022

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.