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

Re: [PATCH v3 4/6] mergetool: fallback to tool when guitool unavailable

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 25, 2019, 03:02 UTC
Message-ID
<xmqq7ebizt1c.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<e975fe4a8b206d8e40f9c4d7cd278fdb5c7358f0.1556142510.git.liu.denton@gmail.com>
Denton Liu <liu.denton@gmail.com> writes:
Show 5 quoted lines
> In git-difftool, if the tool is called with --gui but `diff.guitool` is
> not set, it falls back to `diff.tool`. Make git-mergetool also fallback
> from `merge.guitool` to `merge.tool` if the former is undefined.
>
> If git-difftool were to use `get_configured_mergetool`, it would also

I agree that the precedence order below makes sense, but I am a bit confused by "were to use" here. Do you mean you'll make the change to make difftool to look at mergetool configuraiton in a later step in the series? Or is there a way for the user to say "I want my difftool to also pay attention to the mergetool configurations" (and another "I do not want that" option)? I'll come back to this later.

Show 26 quoted lines
> get the fallback behaviour in the following precedence:
>
> 1. diff.guitool
> 2. merge.guitool
> 3. diff.tool
> 4. merge.tool
>
> Signed-off-by: Denton Liu <liu.denton@gmail.com>
> ---
>  Documentation/git-mergetool.txt |  4 +++-
>  git-mergetool--lib.sh           | 27 ++++++++++++++++++---------
>  t/t7610-mergetool.sh            | 19 +++++++++++++++++++
>  3 files changed, 40 insertions(+), 10 deletions(-)
>
> diff --git a/Documentation/git-mergetool.txt b/Documentation/git-mergetool.txt
> index 0c7975a050..6b14702e78 100644
> --- a/Documentation/git-mergetool.txt
> +++ b/Documentation/git-mergetool.txt
> @@ -83,7 +83,9 @@ success of the resolution after the custom tool has exited.
>  --gui::
>  	When 'git-mergetool' is invoked with the `-g` or `--gui` option
>  	the default merge tool will be read from the configured
> -	`merge.guitool` variable instead of `merge.tool`.
> +	`merge.guitool` variable instead of `merge.tool`. If
> +	`merge.guitool` is not set, we will fallback to the tool
> +	configured under `merge.tool`.

Makes sense. So "mergetool --gui" from the command line would look at guitool and then tool in the merge section.

Show 30 quoted lines
> diff --git a/git-mergetool--lib.sh b/git-mergetool--lib.sh
> index 68ff26a0f7..ada16acffc 100644
> --- a/git-mergetool--lib.sh
> +++ b/git-mergetool--lib.sh
> @@ -350,20 +350,29 @@ guess_merge_tool () {
>  }
>  
>  get_configured_merge_tool () {
> -	# If first argument is true, find the guitool instead
> -	if test "$1" = true
> +	is_gui="$1"
> +	sections="merge"
> +	keys="tool"
> +
> +	if diff_mode
>  	then
> -		gui_prefix=gui
> +		sections="diff $sections"
>  	fi
>  
> -	# Diff mode first tries diff.(gui)tool and falls back to merge.(gui)tool.
> -	# Merge mode only checks merge.(gui)tool
> -	if diff_mode
> +	if "$is_gui" = true
>  	then
> -		merge_tool=$(git config diff.${gui_prefix}tool || git config merge.${gui_prefix}tool)
> -	else
> -		merge_tool=$(git config merge.${gui_prefix}tool)
> +		keys="guitool $keys"
>  	fi

OK, so $sections is "diff merge" for difftool and just "merge" for mergetool. $tool is "guitool tool" under "--gui", and just "tool" otherwise.

Show 8 quoted lines
> +	IFS=' '
> +	for key in $keys
> +	do
> +		for section in $sections
> +		do
> +			merge_tool=$(git config $section.$key) && break 2
> +		done
> +	done

And you do up to four iterations to cover the combination in the precedence order. Which makes sense.

I am not sure about the wisdom of setting IFS here, though.

As far as I can see, both $keys and $sections do not take any arbitrary values, but just the two (for each) values you know that do not have any funny characters, so I am not sure what you are trying to achieve by that (i.e. benefit is unclear).

As long as the get_configured_merge_tool function is called always for string_emitted_to_stdout=$(that function), the updated setting will not leak to the outside world so there is no risk to break its callers, but it is not immediately obvious if helper functions called in the remainder of this function are OK with the modified value of IFS (i.e. safety is not obvious).

Now for the promised "come back to this later", I think you meant "the get_configured_merge_tool function is already prepared to be used from difftool in this step and when difftool starts to call it here is what happens". But I wonder if it makes the evolution of the topic easier to follow if you defer it to a later step when you actually make difftool to start calling it? In other words, in this step, your get_configured_merge_tool would look like

	sections=merge
	case "$1" in
	true)
		keys="guitool tool" ;;
	*)
		keys="tool" ;;
	esac
	for key in $keys
	do
		for section in $sections
		do
			merge_tool=$(git config ...) && break 2
		done
	done
	...

and then in a later step (6/6?), the unconditional setting of sections to 'merge' would be updated so that in diff_mode, you'll iterate over two sections.

I dunno.
Previous: Denton LiuNext: Denton Liu
Message 27 of 48 in “difftool and mergetool improvements”
  1. 0/5 difftool and mergetool improvementsDenton Liu, Apr 22, 2019
  2. 1/5 t7610: add mergetool --gui testsDenton Liu, Apr 22, 2019
  3. 2/5 mergetool: use get_merge_tool functionDenton Liu, Apr 22, 2019
  4. Eric SunshineApr 22, 2019
  5. Denton LiuApr 22, 2019
  6. 3/5 mergetool: fallback to tool when guitool unavailableDenton Liu, Apr 22, 2019
  7. 4/5 difftool: make --gui, --tool and --extcmd exclusiveDenton Liu, Apr 22, 2019
  8. Eric SunshineApr 22, 2019
  9. 5/5 difftool: fallback on merge.guitoolDenton Liu, Apr 22, 2019
  10. Jeff HostetlerApr 22, 2019
  11. Denton LiuApr 22, 2019
  12. 0/5 difftool and mergetool improvementsDenton Liu, Apr 23, 2019
  13. 1/5 t7610: add mergetool --gui testsDenton Liu, Apr 23, 2019
  14. Junio C HamanoApr 24, 2019
  15. 2/5 mergetool: use get_merge_tool_guessed functionDenton Liu, Apr 23, 2019
  16. Junio C HamanoApr 24, 2019
  17. 3/5 mergetool: fallback to tool when guitool unavailableDenton Liu, Apr 23, 2019
  18. 4/5 difftool: make --gui, --tool and --extcmd mutually exclusiveDenton Liu, Apr 23, 2019
  19. 5/5 difftool: fallback on merge.guitoolDenton Liu, Apr 23, 2019
  20. 0/6 difftool and mergetool improvementsDenton Liu, Apr 24, 2019
  21. 1/6 t7610: unsuppress outputDenton Liu, Apr 24, 2019
  22. Junio C HamanoApr 25, 2019
  23. 2/6 t7610: add mergetool --gui testsDenton Liu, Apr 24, 2019
  24. 3/6 mergetool: use get_merge_tool functionDenton Liu, Apr 24, 2019
  25. Junio C HamanoApr 25, 2019
  26. 4/6 mergetool: fallback to tool when guitool unavailableDenton Liu, Apr 24, 2019
  27. Junio C HamanoApr 25, 2019
  28. Denton LiuApr 25, 2019
  29. 5/6 difftool: make --gui, --tool and --extcmd mutually exclusiveDenton Liu, Apr 24, 2019
  30. 6/6 difftool: fallback on merge.guitoolDenton Liu, Apr 24, 2019
  31. Junio C HamanoApr 25, 2019
  32. 0/6 difftool and mergetool improvementsDenton Liu, Apr 25, 2019
  33. 1/6 t7610: unsuppress outputDenton Liu, Apr 25, 2019
  34. 2/6 t7610: add mergetool --gui testsDenton Liu, Apr 25, 2019
  35. 3/6 mergetool: use get_merge_tool functionDenton Liu, Apr 25, 2019
  36. David AguilarApr 28, 2019
  37. 4/6 mergetool: fallback to tool when guitool unavailableDenton Liu, Apr 25, 2019
  38. David AguilarApr 28, 2019
  39. 5/6 difftool: make --gui, --tool and --extcmd mutually exclusiveDenton Liu, Apr 25, 2019
  40. 6/6 difftool: fallback on merge.guitoolDenton Liu, Apr 25, 2019
  41. 0/7 difftool and mergetool improvementsDenton Liu, Apr 29, 2019
  42. 1/7 t7610: unsuppress outputDenton Liu, Apr 29, 2019
  43. 2/7 t7610: add mergetool --gui testsDenton Liu, Apr 29, 2019
  44. 3/7 mergetool: use get_merge_tool functionDenton Liu, Apr 29, 2019
  45. 4/7 mergetool--lib: create gui_mode functionDenton Liu, Apr 29, 2019
  46. 5/7 mergetool: fallback to tool when guitool unavailableDenton Liu, Apr 29, 2019
  47. 6/7 difftool: make --gui, --tool and --extcmd mutually exclusiveDenton Liu, Apr 29, 2019
  48. 7/7 difftool: fallback on merge.guitoolDenton Liu, Apr 29, 2019

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.