# HG changeset patch # User Thomas Arendsen Hein # Date 1182622870 -7200 # Node ID 849f011dbf7968647d0793d4e201434271c38fef # Parent 8c3d449ecc63090d18d3f7d8cc1afbfe0cefa13c Remember path to 'hg' executable and pass to external tools and hooks as $HG. diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -20,7 +20,7 @@ class AmbiguousCommand(Exception): 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 @@ -34,7 +34,7 @@ def runcatch(ui, args): if '--debugger' in args: pdb.set_trace() try: - return dispatch(ui, args) + return dispatch(ui, args, argv0=argv0) finally: ui.flush() except: @@ -255,7 +255,10 @@ def earlygetopt(aliases, args): 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: diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -3090,13 +3090,13 @@ norepo = ("clone init version help debug " 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])) diff --git a/mercurial/help.py b/mercurial/help.py --- a/mercurial/help.py +++ b/mercurial/help.py @@ -37,6 +37,11 @@ helptable = { '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. diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -537,6 +537,17 @@ def _matcher(canonroot, cwd, names, inc, 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. @@ -562,6 +573,8 @@ def system(cmd, environ={}, cwd=None, on 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)