Skip to main content

Mercurial Path Pattern Extension

Project description

Don’t repeat yourself defining [paths] over many repositories, specify the general rule once in ~/.hgrc.

path_pattern is a Mercurial extension used to define default remote path aliases. You may find it helpful if you maintain consistently layed out repository trees on a few machines.

It also provides shortcut hg cloneto «path-alias» (clone to address to which given path alias resolves).

Typical use case

Install the extension (sudo pip install mercurial_path_pattern or manually in the way described below).

Write in your ~/.hgrc:

path_pattern =

lagrange.local = ~/devel/{repo}
lagrange.remote =  ssh://{repo}
bbssh.local = ~/devel/public/{below}
bbssh.remote = ssh://{below:/=-}

Imagine ~/devel/personal/blog/drafts and ~/devel/public/pymods/acme are both some mercurial repositories. Then:

cd ~/devel/personal/blog/drafts
hg push lagrange
# Works, pushes to ssh://

cd ~/devel/public/pymods/acme
hg pull lagrange
# Works, pulls from ssh://
hg pull bbssh
# Works too, pulls from ssh://

Note: paths lagrange and bbssh need not be defined in any of those repositories (they may even lack .hg/hgrc at all).

For two repositories that’s not very useful, but once you have hundred of them, managing individual .hg/hgrc becomes a hassle (imaginge changing to everywhere, or maybe adding second remote alias for new development machine).

Path patterns have lower priority than per-repository paths, so in case you define lagrange path in some repo, it won’t be overwritten.

There is also:

hg cloneto lagrange
# Equivalent to
#   hg clone . ssh://ssh://
# but noticeably shorter

which works both for normal paths and paths derived from patterns, but is especially handy with patterns, for example:

cd ~/devel/libs
hg init xyz
cd xyz
hg cloneto lagrange


Extension mostly works behind the courtains, making standard commands like hg pull, hg push, and hg incoming aware of extra paths. In particular, hg paths includes generated paths and can be used to check whether they are correct.

You may also use:

hg list_path_patterns

to check which patterns you configured.


hg cloneto «alias»

command looks up alias among paths (both pattern-based, and normal) and issues clone to this path. It is equivalent to hg clone . «alias expansion»). In case alias is not defined, it fails.

Pattern syntax

Patterns are defined in [path_pattern] section of mercurial configuration file (typically they are kept in ~/.hgrc, but feel free to define them system-wide).

You may have as many patterns as you like. Example:

lagrange.local = ~/devel/{repo}
lagrange.remote =  ssh://{repo}
euler.local = ~/devel/{repo}
euler.remote =  ssh://{repo:/=.}/hg
wrk.local = ~/work/{what}
wrk.remote =  https://tim@devel-department.local/{what:/=__:\=__}
ugly.local = ~/(topic)/sources/{subpath}/repo
ugly.remote = ssh://hg{topic}@devel.local/{topic}/{subpath}

Every pattern is defined by the pair of keys - «alias».local and «alias».remote.

Local part should specify local path of the repository (absolute path, ~ and ~user are allowed). Some part(s) of the path may be marked with {brace} or (paren) markers ({brace} matches everything aggressively, (paren) is limited to single path item and does not cross / or \\ characters. Those parts will be extracted from matched path and available for substitution. Typically there will be single marker on the end, but more obscure patterns are possible (as ugly above illustrates).

Remote part defines appropriate remote address. This is typical Mercurial remote path, where {marker}’s can be used to refer to values extracted from local path. Simple substitutions are allowed, {sth:x=y} means take whatever was extracted as ``sth`` and replace any ``x`` with ``y`` and can be chained if necessary, {sth:x=y,v=z} means take whatever was extracted as ``sth`` and replace any ``x`` with ``y``, then replace any ``v`` with ``z``.

While processing patterns, the extension matches current repository root path against local part of the pattern, and if it matches, extracts parts marked with markers and fills remote part with them.

For example, with definitions above, if you happen to issue hg paths in repository ~/devel/python/libs/webby, the extension will:

  1. Find that lagrange.local matches and that {repo} is python/libs/webby. Filling lagrange.remote with that value generates ssh://, so finally it will create path alias lagrange=ssh://;

  2. Similarly discover that euler.local matches, and after copying {repo} and applying substitution define path euler=ssh://;

  3. Ignore remaining patterns as they do not match.

Local paths are matched to patterns with naive text matching, in particular / are treated as any other character. This may change in the future in case there is a true need.


Recommended way:

pip install mercurial_path_pattern

(prepend with sudo if necessary).

Manual way:

  • install mercurial_extension_utils

  • download from this repository and save it somewhere

  • activate extension by:

    path_pattern = /path/to/



Development, bug reports, enhancement suggestions

Development is tracked on BitBucket, see

Use BitBucket issue tracker for bug reports and enhancement suggestions.

Additional notes

Information about this extension is also available on Mercurial Wiki:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mercurial_path_pattern-0.8.0.tar.gz (7.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page