# [PATCH] git-mergetool: Make default smarter by considering user's desktop environment and editor

6 messages from 2007-06-06 to 2007-06-06. Participants: Josh Triplett, Junio C Hamano, David Kastrup, Theodore Tso.
Thread: https://gitlist.dev/t/8461

## Josh Triplett, 2007-06-06 04:28

Subject: [PATCH] git-mergetool: Make default smarter by considering user's desktop environment and editor
Message-ID: <466637E2.5040303@freedesktop.org>
URL: https://gitlist.dev/e/466637E2.5040303%40freedesktop.org

```
Make git-mergetool prefer meld under GNOME, and kdiff3 under KDE.  When
considering emerge and vimdiff, check $VISUAL and $EDITOR to see which the
user might prefer.

Signed-off-by: Josh Triplett <josh@freedesktop.org>
---

Arguably, kdiff3 should now move down next to meld, below the other
non-graphical tools, so that if the user doesn't run KDE or GNOME they get a
non-KDE, non-GNOME tool.  This patch doesn't do that, though.

 git-mergetool.sh |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)

diff --git a/git-mergetool.sh b/git-mergetool.sh
index bb21b03..2053d43 100755
--- a/git-mergetool.sh
+++ b/git-mergetool.sh
@@ -304,7 +304,13 @@ if test -z "$merge_tool"; then
 fi
 
 if test -z "$merge_tool" ; then
-    if type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then
+    # Try tools that match the user's desktop environment
+    if test x"$KDE_FULL_SESSION" = x"true" && type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then
+        merge_tool="kdiff3";
+    elif test x"$GNOME_DESKTOP_SESSION_ID" != x"" && type meld >/dev/null 2>&1 && test -n "$DISPLAY"; then
+        merge_tool=meld
+    # Try any graphical tool
+    elif type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then
 	merge_tool="kdiff3";
     elif type tkdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
 	merge_tool=tkdiff
@@ -312,6 +318,12 @@ if test -z "$merge_tool" ; then
 	merge_tool=xxdiff
     elif type meld >/dev/null 2>&1 && test -n "$DISPLAY"; then
 	merge_tool=meld
+    # Try tools specific to the user's editor
+    elif echo "${VISUAL:-$EDITOR}" | grep '^emacs' > /dev/null 2>&1 && type emacs >/dev/null 2>&1; then
+	merge_tool=emerge
+    elif echo "${VISUAL:-$EDITOR}" | grep '^vim' > /dev/null 2>&1 && type vimdiff >/dev/null 2>&1; then
+	merge_tool=vimdiff
+    # Try other tools
     elif type opendiff >/dev/null 2>&1; then
 	merge_tool=opendiff
     elif type emacs >/dev/null 2>&1; then
-- 
1.5.2.1



```

## Junio C Hamano, 2007-06-06 08:08

Subject: Re: [PATCH] git-mergetool: Make default smarter by considering user's desktop environment and editor
Message-ID: <7vabvd7bfg.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vabvd7bfg.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <466637E2.5040303@freedesktop.org>

```
Josh Triplett <josh@freedesktop.org> writes:

> Make git-mergetool prefer meld under GNOME, and kdiff3 under KDE.  When
> considering emerge and vimdiff, check $VISUAL and $EDITOR to see which the
> user might prefer.
>
> Signed-off-by: Josh Triplett <josh@freedesktop.org>

The basic idea is sound.  However...

 (1) I wonder if we can get rid of the horribly long if .. elif
     chain by using shell function and then iterate a list of them;

 (2) echo "${VISUAL-$EDITOR}" | grep '^emacs'???

     Some people may have explicit path (/home/me/bin/emacs),
     and/or runs a variant of emacs called 'xemacs'.  Same for
     vim.

Something like...

        test_xstuff () {
                test -n "$DISPLAY" && type "$1" >/dev/null 2>&1
        test_kdiff3 () {
                test_xstuff kdiff3
        }
        test_tkdiff () {
                test_xstuff tkdiff
        }
        test_estuff() {
                type "$1" >/dev/null 2>&1 &&
                case "${VISUAL-$EDITOR}" in *"$1"*) : ;; *) false ;; esac
        }
        test_emerge () {
                test_estuff emacs
        }
        test_vimdiff () {
                test_estuff vim
        }

        choose_merge_tool () {
                for t in "$@"
                do
                        if test_$t
                        then
                                echo "$t"
                                break
                        fi
                done
        }

        if test -z "$merge_tool"
        then
                merge_tool_candidates='kdiff3 tkdiff xxdiff meld opendiff ...'
                if test -n "$GNOME_DESCTOP_SESSION_ID"
                then
                        merge_tool_candidates="meld $merge_tool_candidates"
                elif test -n "$KDE_FULL_SESSION"
                then
                        merge_tool_candidates="kdiff3 $merge_tool_candidates"
                elif
                        ...
                fi
                merge_tool=$(choose_merge_tool $merge_tool_candidates)
        fi

```

## David Kastrup, 2007-06-06 08:52

Subject: Re: [PATCH] git-mergetool: Make default smarter by considering user's desktop environment and editor
Message-ID: <86tztljwgk.fsf@lola.quinscape.zz>
URL: https://gitlist.dev/e/86tztljwgk.fsf%40lola.quinscape.zz
In-Reply-To: <7vabvd7bfg.fsf@assigned-by-dhcp.cox.net>

```
Junio C Hamano <junkio@cox.net> writes:

> Josh Triplett <josh@freedesktop.org> writes:
>
>> Make git-mergetool prefer meld under GNOME, and kdiff3 under KDE.  When
>> considering emerge and vimdiff, check $VISUAL and $EDITOR to see which the
>> user might prefer.
>>
>> Signed-off-by: Josh Triplett <josh@freedesktop.org>
>
> The basic idea is sound.  However...
>
>  (1) I wonder if we can get rid of the horribly long if .. elif
>      chain by using shell function and then iterate a list of them;
>
>  (2) echo "${VISUAL-$EDITOR}" | grep '^emacs'???
>
>      Some people may have explicit path (/home/me/bin/emacs),
>      and/or runs a variant of emacs called 'xemacs'.  Same for
>      vim.

Actually, a lot of people run as editor a program called "emacsclient"
or "gnuclient": this one connects to an existing Emacs session and
passes its arguments to it.  And of course, this is what git-mergetool
should then also do.

-- 
David Kastrup

```

## Junio C Hamano, 2007-06-06 09:05

Subject: Re: [PATCH] git-mergetool: Make default smarter by considering user's desktop environment and editor
Message-ID: <7vabvd5u7t.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vabvd5u7t.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <86tztljwgk.fsf@lola.quinscape.zz>

```
David Kastrup <dak@gnu.org> writes:

> Junio C Hamano <junkio@cox.net> writes:
>
>> Josh Triplett <josh@freedesktop.org> writes:
>>
>>> Make git-mergetool prefer meld under GNOME, and kdiff3 under KDE.  When
>>> considering emerge and vimdiff, check $VISUAL and $EDITOR to see which the
>>> user might prefer.
>>>
>>> Signed-off-by: Josh Triplett <josh@freedesktop.org>
>>
>> The basic idea is sound.  However...
>>
>>  (1) I wonder if we can get rid of the horribly long if .. elif
>>      chain by using shell function and then iterate a list of them;
>>
>>  (2) echo "${VISUAL-$EDITOR}" | grep '^emacs'???
>>
>>      Some people may have explicit path (/home/me/bin/emacs),
>>      and/or runs a variant of emacs called 'xemacs'.  Same for
>>      vim.
>
> Actually, a lot of people run as editor a program called "emacsclient"
> or "gnuclient": this one connects to an existing Emacs session and
> passes its arguments to it.  And of course, this is what git-mergetool
> should then also do.

I do that too, but that is covered by the prefix rule Josh has,
so it is Ok.

```

## David Kastrup, 2007-06-06 09:40

Subject: Re: [PATCH] git-mergetool: Make default smarter by considering user's desktop environment and editor
Message-ID: <86lkexjua3.fsf@lola.quinscape.zz>
URL: https://gitlist.dev/e/86lkexjua3.fsf%40lola.quinscape.zz
In-Reply-To: <7vabvd5u7t.fsf@assigned-by-dhcp.cox.net>

```
Junio C Hamano <gitster@pobox.com> writes:

> David Kastrup <dak@gnu.org> writes:
>
>> Junio C Hamano <junkio@cox.net> writes:
>>
>>> Josh Triplett <josh@freedesktop.org> writes:
>>>
>>>> Make git-mergetool prefer meld under GNOME, and kdiff3 under KDE.  When
>>>> considering emerge and vimdiff, check $VISUAL and $EDITOR to see which the
>>>> user might prefer.
>>>>
>>>> Signed-off-by: Josh Triplett <josh@freedesktop.org>
>>>
>>> The basic idea is sound.  However...
>>>
>>>  (1) I wonder if we can get rid of the horribly long if .. elif
>>>      chain by using shell function and then iterate a list of them;
>>>
>>>  (2) echo "${VISUAL-$EDITOR}" | grep '^emacs'???
>>>
>>>      Some people may have explicit path (/home/me/bin/emacs),
>>>      and/or runs a variant of emacs called 'xemacs'.  Same for
>>>      vim.
>>
>> Actually, a lot of people run as editor a program called "emacsclient"
>> or "gnuclient": this one connects to an existing Emacs session and
>> passes its arguments to it.  And of course, this is what git-mergetool
>> should then also do.
>
> I do that too, but that is covered by the prefix rule Josh has,
> so it is Ok.

Another note: in my usual Emacs session, EDITOR is set to
/usr/local/emacs-22/bin/emacsclient
which would not be covered...  So that should probably be

useemacs=0
for i in ${VISUAL-$EDITOR}
do
        case "`basename $i`" in [xX][eE]macs*|[eE]macs*|gnuclient*)
             useemacs=1
        esac
        break
done

This should work even when we are talking about something like
EDITOR="C:\My Programs\XEmacs\gnuclient.exe" -n "C:\My Programs\XEmacs\XEmacs"

And it does not require starting grep for something which the shell
can perfectly well handle on its own.  Yes, it does need basename.  I
don't see a good way to avoid that.

-- 
David Kastrup

```

## Theodore Tso, 2007-06-06 21:20

Subject: Re: [PATCH] git-mergetool: Make default smarter by considering user's desktop environment and editor
Message-ID: <20070606212031.GD9708@thunk.org>
URL: https://gitlist.dev/e/20070606212031.GD9708%40thunk.org
In-Reply-To: <86lkexjua3.fsf@lola.quinscape.zz>

```
This is what I've been experimenting in my tree; what do people think?

if test -z "$merge_tool" ; then
    if test -n "$DISPLAY"; then
	merge_tool_candidates="kdiff3 tkdiff xxdiff meld gvimdiff"
	if test "$GNOME_DESKTOP_SESSION_ID"x != ""x; then
	    merge_tool_candidates="meld $merge_tool_candidates"
	fi
	if test "$KDE_FULL_SESSION"x = "true"x; then
	    merge_tool_candidates="kdiff3 $merge_tool_candidates"
	fi
    fi
    if echo "${VISUAL:-$EDITOR}" | grep 'emacs' > /dev/null 2>&1; then
	merge_tool_candidates="$merge_tool_candidates emerge"
    fi
    if echo "${VISUAL:-$EDITOR}" | grep 'vim' > /dev/null 2>&1; then
	merge_tool_candidates="$merge_tool_candidates vimdiff"
    fi
    merge_tool_candidates="$merge_tool_candidates opendiff emerge vimdiff"
    echo "merge tool candidates: $merge_tool_candidates"
    for i in $merge_tool_candidates; do
	if test $i = emerge ; then
	    cmd=emacs
	else
	    cmd=$i
	fi
	if type $cmd > /dev/null 2>&1; then
	    echo merge_tool chosen is $i
	    merge_tool=$i
	    break
	fi
    done
fi

						- Ted

```
