Mercurial SCM
Mercurial is a fast, scalable, and distributed revision control (version control) system designed for efficient handling of projects of any size. It is primarily implemented in Python, with performance-critical parts in C and Rust. The library is actively maintained with regular releases, with version 7.2.1 being the latest stable release as of April 1, 2026.
Warnings
- gotcha Direct use of Mercurial's internal Python API (e.g., `from mercurial import ...`) is primarily intended for writing extensions and is considered unstable. Its license is GPL-2.0-or-later. For stable, permissive-licensed programmatic interaction, consider `hgapi` which wraps the command-line interface, or `hglib` for the command server.
- breaking Mercurial has adjusted its Python version support over time. Version 6.9 dropped support for Python 3.6 and 3.7. Version 7.2.0 was likely the last to support Python 3.9, with future releases focusing on newer Python versions (e.g., 3.14, 3.15).
- breaking Mercurial 7.0 introduced compliance with PEP 517 for packaging. This means `setup.py` can no longer be called directly for building; instead, PyPA's `build` package should be used. This primarily affects maintainers who build Mercurial from source or create custom distributions.
- breaking The 7.2.1 release included a 'large import-cycle-breaking series' due to Python typing efforts. This refactoring, involving new files, may make merges more difficult for existing extension maintainers and could break older extensions that relied on specific internal import paths or structures.
Install
-
pip install mercurial
Imports
- ui, hg
from mercurial import ui, hg
Quickstart
import os
import shutil
from mercurial import ui, hg
# Create a dummy directory for the repository
repo_path = 'my_test_repo'
if os.path.exists(repo_path):
shutil.rmtree(repo_path)
os.makedirs(repo_path)
# Initialize a new Mercurial repository
repo = hg.repository(ui.ui(), repo_path, create=True)
print(f"Repository initialized at: {repo_path}")
# Create a file
file_path = os.path.join(repo_path, 'hello.txt')
with open(file_path, 'w') as f:
f.write('Hello, Mercurial!')
print(f"Created file: {file_path}")
# Add and commit the file
# Note: Mercurial's internal API for add/commit can be complex.
# This example uses `add` and then `commit` via the repo object.
# For simpler CLI-like interaction, consider `hglib` or `hgapi`.
# The 'add' command is implicitly handled by `commit` for untracked files in newer versions/contexts,
# but explicitly adding first is generally safer for robust scripts.
repo.add([file_path])
repo.commit(text='Initial commit', user='Test User <test@example.com>')
print("File committed.")
# Verify the commit (optional: run hg log in the directory to confirm)
# You would typically interact with changesets, manifests, etc. for more complex verification.
logs = repo.changelog.read() # Read changelog to get revision info
if logs:
latest_rev = repo.changelog.tip()
print(f"Latest revision: {latest_rev.node()[:12]}")
print(f"Commit message: {latest_rev.description()}")
# Clean up the test repository (optional)
shutil.rmtree(repo_path)
print(f"Cleaned up {repo_path}")