Thomas Arendsen Hein avatar Thomas Arendsen Hein committed 849f011

Remember path to 'hg' executable and pass to external tools and hooks as $HG.

Comments (0)

Files changed (4)

mercurial/cmdutil.py

 class ParseError(Exception):
     """Exception raised on errors in parsing the command line."""
 
-def runcatch(ui, args):
+def runcatch(ui, args, argv0=None):
     def catchterm(*args):
         raise util.SignalInterrupt
 
             if '--debugger' in args:
                 pdb.set_trace()
             try:
-                return dispatch(ui, args)
+                return dispatch(ui, args, argv0=argv0)
             finally:
                 ui.flush()
         except:
             return args[args.index(opt) + 1]
     return None
 
-def dispatch(ui, args):
+def dispatch(ui, args, argv0=None):
+    # remember how to call 'hg' before changing the working dir
+    util.set_hgexecutable(argv0)
+
     # check for cwd first
     cwd = earlygetopt(['--cwd'], args)
     if cwd:

mercurial/commands.py

           " debugindex debugindexdot debugdate debuginstall")
 optionalrepo = ("paths serve showconfig")
 
-def dispatch(args):
+def dispatch(args, argv0=None):
     try:
         u = ui.ui(traceback='--traceback' in args)
     except util.Abort, inst:
         sys.stderr.write(_("abort: %s\n") % inst)
         return -1
-    return cmdutil.runcatch(u, args)
+    return cmdutil.runcatch(u, args, argv0=argv0)
 
 def run():
-    sys.exit(dispatch(sys.argv[1:]))
+    sys.exit(dispatch(sys.argv[1:], argv0=sys.argv[0]))

mercurial/help.py

 
     'environment|env|Environment Variables':
     r'''
+HG::
+    Path to the 'hg' executable, automatically passed when running hooks
+    or external tools. Falls back to 'hg' if unset and the value can't be
+    autodetected, e.g. when Mercurial is run as a Python module.
+
 HGEDITOR::
     This is the name of the editor to use when committing. Defaults to the
     value of EDITOR.

mercurial/util.py

 
     return (roots, match, (inc or exc or anypats) and True)
 
+_hgexecutable = None
+
+def set_hgexecutable(path):
+    """remember location of the 'hg' executable if easily possible
+
+    path might be None or empty if hg was loaded as a module,
+    fall back to 'hg' in this case.
+    """
+    global _hgexecutable
+    _hgexecutable = path and os.path.abspath(path) or 'hg'
+
 def system(cmd, environ={}, cwd=None, onerr=None, errprefix=None):
     '''enhanced shell command execution.
     run with environment maybe modified, maybe in different dir.
     try:
         for k, v in environ.iteritems():
             os.environ[k] = py2shell(v)
+        if 'HG' not in os.environ:
+            os.environ['HG'] = _hgexecutable
         if cwd is not None and oldcwd != cwd:
             os.chdir(cwd)
         rc = os.system(cmd)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.