[PATCH 2 of 2 RFC] merge-tools: move gui check into the ui object, and make it platform-specific

Dan Villiom Podlaski Christiansen danchr at gmail.com
Wed Mar 23 16:24:30 CDT 2011


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1300915163 -3600
# Node ID 52010b7def45ae910491aba4331f88b1502785e8
# Parent  ca4aae97f719b657182e7a426cb4d62ada9bc7a7
merge-tools: move gui check into the ui object, and make it platform-specific

Merge tool configurations can now have three possible values:

- 'true' implies the default GUI for the platform.
- 'mac' implies a Mac native GUI.
- 'windows', similarly, implies a Windows native GUI.
- 'x11' means X11.

The logic for determining the availability of the environments remains
the same.

I haven't updated mergetools.hgrc or the documentation -- I'll do that
if the approach is sound.

diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
--- a/mercurial/filemerge.py
+++ b/mercurial/filemerge.py
@@ -39,6 +39,7 @@ def _findtool(ui, tool):
 def _picktool(repo, ui, path, binary, symlink):
     def check(tool, pat, symlink, binary):
         tmsg = tool
+        gui = _toolstr(ui, tool, "gui")
         if pat:
             tmsg += " specified for " + pat
         if not _findtool(ui, tool):
@@ -50,8 +51,9 @@ def _picktool(repo, ui, path, binary, sy
             ui.warn(_("tool %s can't handle symlinks\n") % tmsg)
         elif binary and not _toolbool(ui, tool, "binary"):
             ui.warn(_("tool %s can't handle binary\n") % tmsg)
-        elif not util.gui() and _toolbool(ui, tool, "gui"):
-            ui.warn(_("tool %s requires a GUI\n") % tmsg)
+        elif not ui.gui(_toolstr(ui, tool, "gui")):
+            guiname = util.parsegui(_toolstr(ui, tool, "gui")) or _('unknown')
+            ui.warn(_("tool %s requires a %s GUI\n") % (tmsg, guiname))
         else:
             return True
         return False
diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -7,7 +7,7 @@
 
 from i18n import _
 import errno, getpass, os, socket, sys, tempfile, traceback
-import config, util, error
+import config, util, error, osutil
 
 class ui(object):
     def __init__(self, src=None):
@@ -634,3 +634,22 @@ class ui(object):
         ui.write(ui.label(s, 'label')).
         '''
         return msg
+
+    def gui(self, environment=None):
+        '''Is the given GUI environment currently available?
+
+        If no environment is given, assume an OS-specific default.
+        '''
+
+        environment = util.parsegui(environment)
+
+        if environment == 'mac':
+            return (sys.environment == 'darwin'
+                    and 'SSH_CONNECTION' not in os.environ
+                    and osutil.isgui())
+        elif environment == 'x11':
+            return 'DISPLAY' in os.environ
+        elif environment == 'windows':
+            return os.name == 'nt'
+        else:
+            return False
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -767,17 +767,31 @@ def splitpath(path):
     function if need.'''
     return path.split(os.sep)
 
-def gui():
-    '''Are we running in a GUI?'''
-    if sys.platform == 'darwin':
-        if 'SSH_CONNECTION' in os.environ:
-            # handle SSH access to a box where the user is logged in
-            return False
-        else:
-            # check if a CoreGraphics session is available
-            return osutil.isgui()
+def defaultgui():
+    '''Get the default GUI environment for the platform.'''
+    if os.name == 'nt':
+        return 'win'
+    elif sys.platform == 'darwin':
+        return 'mac'
     else:
-        return os.name == "nt" or os.environ.get("DISPLAY")
+        # assume os.name == 'posix':
+        return 'x11'
+
+def parsegui(gui):
+    '''Convert the given GUI description into a recognized GUI name.'''
+
+    boolvalue = parsebool(gui)
+
+    if gui is None or boolvalue is True:
+        return defaultgui()
+    elif gui in ['aqua', 'mac', 'macosx']:
+        return 'mac'
+    elif gui in ['nt', 'win', 'win32', 'windows']:
+        return 'windows'
+    elif gui in ['posix', 'unix', 'x11']:
+        return 'x11'
+    else:
+        return None
 
 def mktempcopy(name, emptyok=False, createmode=None):
     """Create a temporary file with the same contents from name


More information about the Mercurial-devel mailing list