You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使自定义Mercurial合并工具mymergetool.py与Hg同进程运行?

How to Run Custom Mercurial Merge Tool in the Same Process with python: Prefix

Got it, let's break down how to make your mymergetool.py run in the same Mercurial process using that python: syntax—super useful for Windows environments where standalone python.exe might not be available (like embedded Hg apps).

Step 1: Adjust Your Merge Tool Script Structure

Mercurial can execute Python scripts directly in its own process when you use the python: prefix, but your script needs to handle merge parameters just like it would when run externally.

First, make sure your script captures the standard merge tool arguments (base, local, other, output) via sys.argv. If you already have a core merge function, keep that intact and add a simple entry point that works for both direct execution and Hg's in-process call:

import sys

def your_merge_logic(base_path, local_path, other_path, output_path):
    # Your existing merge logic goes here
    print(f"Merging files: base={base_path}, local={local_path}, other={other_path} -> output={output_path}")

def main(args):
    if len(args) != 4:
        print("Usage: mymergetool.py <base> <local> <other> <output>", file=sys.stderr)
        sys.exit(1)
    your_merge_logic(*args)

if __name__ == "__main__":
    main(sys.argv[1:])

Mercurial will pass the configured arguments to the script via sys.argv, so this structure works seamlessly for both in-process and external execution.

Step 2: Configure Mercurial for In-Process Execution

Update your .hgrc (Linux/macOS) or Mercurial.ini (Windows) with this merge tool configuration:

[merge-tools]
MyMergeTool.executable = python:mymergetool.py
MyMergeTool.args = $base $local $other $output
MyMergeTool.priority = 100
MyMergeTool.premerge = True  # Set to False if your tool doesn't need pre-merge checks
  • The python: prefix is the magic here—it tells Mercurial to load and run the script in its own process instead of spawning a new one.
  • $base, $local, $other, $output are Mercurial variables that map to the respective file paths required for the merge.

Step 3: Ensure Mercurial Can Locate Your Script

Mercurial's embedded Python interpreter needs to find mymergetool.py. Use one of these reliable methods:

  • Drop it in a Hg-accessible folder: Place the script in your user-specific Mercurial extensions directory (e.g., ~/.hgext on Linux/macOS, %USERPROFILE%\.hgext on Windows).
  • Use an absolute path: Specify the full path to your script in the config, like python:C:\tools\mymergetool.py (Windows) or python:/home/you/tools/mymergetool.py (Linux).
  • Add the script's directory to PYTHONPATH: Set the PYTHONPATH environment variable to include the folder holding mymergetool.py—this lets Hg's Python runtime find it automatically.

Step 4: Verify It's Running in the Same Process

To confirm your tool is sharing Mercurial's process, add a quick check to your script:

import os
print(f"Merge tool PID: {os.getpid()}")

Run a Hg merge command, note the PID output, then cross-check with the Hg process's PID (use Task Manager on Windows, ps on Linux). If they match, you're all set!

Key Notes for Windows Embedded Environments

For apps that embed Mercurial without a standalone python.exe, this method shines because it uses Hg's own embedded Python runtime. Just double-check that your script is compatible with the Python version Hg uses (run hg --version to see the embedded Python version).

内容的提问来源于stack exchange,提问作者Tom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:41:53