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

Re: [PATCH] mergetool: new config guiDefault supports auto-toggling gui by DISPLAY

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 13, 2022, 21:58 UTC
Message-ID
<xmqqedvbcrnn.fsf@gitster.g>
In-Reply-To
<pull.1381.git.1665590389045.gitgitgadget@gmail.com>
"Tao Klerks via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 28 quoted lines
> +enum difftool_gui_mode {
> +	GUI_DISABLED = -1,
> +	GUI_BY_CONFIG = 0,
> +	GUI_ENABLED = 1
> +};
> +
> +static int difftool_opt_gui(const struct option *opt,
> +			      const char *optarg, int unset)
> +{
> +	enum difftool_gui_mode *mode;
> +	mode = opt->value;
> +
> +	BUG_ON_OPT_ARG(optarg);
> +
> +	if (unset)
> +		*mode = GUI_DISABLED;
> +	else
> +		*mode = GUI_ENABLED;
> +	return 0;
> +}
> +
>  int cmd_difftool(int argc, const char **argv, const char *prefix)
>  {
> -	int use_gui_tool = 0, dir_diff = 0, prompt = -1, symlinks = 0,
> -	    tool_help = 0, no_index = 0;
> +	int dir_diff = 0, prompt = -1, symlinks = 0, tool_help = 0,
> +	    no_index = 0;
> +	enum difftool_gui_mode gui_mode;
This is left uninitialized ...
Show 7 quoted lines
>  	static char *difftool_cmd = NULL, *extcmd = NULL;
>  	struct option builtin_difftool_options[] = {
> -		OPT_BOOL('g', "gui", &use_gui_tool,
> -			 N_("use `diff.guitool` instead of `diff.tool`")),
> +		OPT_CALLBACK_F('g', "gui", &gui_mode, NULL,
> +			       N_("use `diff.guitool` instead of `diff.tool`"),
> +			       PARSE_OPT_NOARG, difftool_opt_gui),

... and its address is used here. When "--no-gui" or "--gui" option is given, the callback function will fill either _DISABLED or _ENABLED to it.

But without any command line argument?  Isn't gui_mode variable ...
Show 7 quoted lines
>  		OPT_BOOL('d', "dir-diff", &dir_diff,
>  			 N_("perform a full-directory diff")),
>  		OPT_SET_INT_F('y', "no-prompt", &prompt,
> @@ -732,13 +755,16 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
>  	} else if (dir_diff)
>  		die(_("options '%s' and '%s' cannot be used together"), "--dir-diff", "--no-index");
>  

... still uninitialized here? The old use_gui_tool was initialized to 0 so it wouldn't have had this problem.

Show 8 quoted lines
> -	die_for_incompatible_opt3(use_gui_tool, "--gui",
> +	die_for_incompatible_opt3(gui_mode == GUI_ENABLED, "--gui",
>  				  !!difftool_cmd, "--tool",
>  				  !!extcmd, "--extcmd");
>  
> -	if (use_gui_tool)
> +	if (gui_mode == GUI_ENABLED)
>  		setenv("GIT_MERGETOOL_GUI", "true", 1);

I suspect that there is no need to introduce a enum. The flow would probably be

 * git_config(difftool_config) would learn to parse the .guiDefault
   option and initialize use_gui_tool to -1 when set to "auto" (and
   to 0 with "false", to 1 with "true").
 * Call to parse_options() then overwrites use_gui_tool with either
   0 or 1 when --no-gui or --gui is given.
 * After parse_options() returns, use_gui_tool can be examined and
   when it is 0 or 1, then nothing need to change.  The current code
   before this patch is doing what the user wants when an explicit
   command line option is given.
 * When use_gui_tool is -1, we need a new code that sets it to
   either 0 or 1 depending on the running environment.

But what is curious is that nothing in C code even looks at .guiDefault configuration, so I am not sure why you would even need to change anything in builtin/difftool.c file at all.

Puzzled.
Show 16 quoted lines
> diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh
> index 9f99201bcca..8dbd04e5c5c 100644
> --- a/git-mergetool--lib.sh
> +++ b/git-mergetool--lib.sh
> @@ -97,7 +97,33 @@ merge_mode () {
>  	test "$TOOL_MODE" = merge
>  }
>  
> +get_gui_default () {
> +	if diff_mode
> +	then
> +		GUI_DEFAULT_KEY="difftool.guiDefault"
> +	else
> +		GUI_DEFAULT_KEY="mergetool.guiDefault"
> +	fi
> +	GUI_DEFAULT_CONFIG_LCASE=$(git config --default false --get $GUI_DEFAULT_KEY  | tr '[:upper:]' '[:lower:]')

Avoid [:class:] when 'A-Z' 'a-z' is sufficient. Easier to read and you do not even need to worry about portability that way.

Show 13 quoted lines
> +	if [ "$GUI_DEFAULT_CONFIG_LCASE" = "auto" ]
> +	then
> +		if [ -n "$DISPLAY" ]
> +		then
> +			GUI_DEFAULT=true
> +		else
> +			GUI_DEFAULT=false
> +		fi
> +	else
> +		GUI_DEFAULT=$(git config --default false --bool --get $GUI_DEFAULT_KEY)
> +	fi
> +	echo $GUI_DEFAULT
> +}

Check Documentation/CodingGuidelines with special attention to the "for shell scripts" section?

Previous: Tao KlerksNext: Tao Klerks
Message 5 of 23 in “mergetool: new config guiDefault supports auto-toggling gui by DISPLAY”
  1. mergetool: new config guiDefault supports auto-toggling gui by DISPLAYTao Klerks via GitGitGadget, Oct 12, 2022
  2. Tao KlerksOct 12, 2022
  3. Junio C HamanoOct 12, 2022
  4. Tao KlerksOct 13, 2022
  5. Junio C HamanoOct 13, 2022
  6. Tao KlerksOct 14, 2022
  7. Junio C HamanoOct 14, 2022
  8. Tao KlerksOct 14, 2022
  9. Junio C HamanoOct 14, 2022
  10. Tao KlerksOct 16, 2022
  11. RFC: mergetool: new config guiDefault supports auto-toggling gui by DISPLAYTao Klerks via GitGitGadget, Oct 14, 2022
  12. Eric SunshineOct 14, 2022
  13. Tao KlerksOct 14, 2022
  14. Junio C HamanoOct 14, 2022
  15. Tao KlerksOct 16, 2022
  16. Junio C HamanoOct 17, 2022
  17. RFC: mergetool: new config guiDefault supports auto-toggling gui by DISPLAYTao Klerks via GitGitGadget, Oct 18, 2022
  18. Tao KlerksFeb 17, 2023
  19. mergetool: new config guiDefault supports auto-toggling gui by DISPLAYTao Klerks via GitGitGadget, Mar 18, 2023
  20. David AguilarApr 4, 2023
  21. Tao KlerksApr 4, 2023
  22. Junio C HamanoApr 4, 2023
  23. David AguilarApr 6, 2023

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.