threads / patch / 8461

patchgit-mergetool: Make default smarter by considering user's desktop environment and editor

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

## tl;dr

6 messages between Jun 6, 2007 and Jun 6, 2007. Diffs are folded; open one to read it.

replies: 5people: 4as markdown or json

Josh Triplett· Jun 6, 2007, 04:28 UTC · lore

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(-)
Show changes to git-mergetool.sh +13 −1
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· Jun 6, 2007, 08:08 UTC · re: Josh Triplett · lore

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

Josh Triplett <josh@freedesktop.org> writes:
Show 5 quoted lines
> 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· Jun 6, 2007, 08:52 UTC · re: Junio C Hamano · lore

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

Junio C Hamano <junkio@cox.net> writes:
Show 18 quoted lines
> 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· Jun 6, 2007, 09:05 UTC · re: David Kastrup · lore

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

David Kastrup <dak@gnu.org> writes:
Show 25 quoted lines
> 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· Jun 6, 2007, 09:40 UTC · re: Junio C Hamano · lore

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

Junio C Hamano <gitster@pobox.com> writes:
Show 30 quoted lines
> 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· Jun 6, 2007, 21:20 UTC · re: David Kastrup · lore

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

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

← back to recent threads