diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py index 8c3d449ecc63090d18d3f7d8cc1afbfe0cefa13c_bWVyY3VyaWFsL2NtZHV0aWwucHk=..849f011dbf7968647d0793d4e201434271c38fef_bWVyY3VyaWFsL2NtZHV0aWwucHk= 100644 --- a/mercurial/cmdutil.py +++ b/mercurial/cmdutil.py @@ -20,7 +20,7 @@ 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 @@ 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 @@ 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 index 8c3d449ecc63090d18d3f7d8cc1afbfe0cefa13c_bWVyY3VyaWFsL2NvbW1hbmRzLnB5..849f011dbf7968647d0793d4e201434271c38fef_bWVyY3VyaWFsL2NvbW1hbmRzLnB5 100644 --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -3090,9 +3090,9 @@ " 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 @@ -3094,8 +3094,8 @@ 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(): @@ -3100,3 +3100,3 @@ 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 index 8c3d449ecc63090d18d3f7d8cc1afbfe0cefa13c_bWVyY3VyaWFsL2hlbHAucHk=..849f011dbf7968647d0793d4e201434271c38fef_bWVyY3VyaWFsL2hlbHAucHk= 100644 --- a/mercurial/help.py +++ b/mercurial/help.py @@ -37,6 +37,11 @@ '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 index 8c3d449ecc63090d18d3f7d8cc1afbfe0cefa13c_bWVyY3VyaWFsL3V0aWwucHk=..849f011dbf7968647d0793d4e201434271c38fef_bWVyY3VyaWFsL3V0aWwucHk= 100644 --- a/mercurial/util.py +++ b/mercurial/util.py @@ -537,6 +537,17 @@ 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 @@ 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)