Using Patterns to Identify Files

In addition to working with file and directory names, Mercurial lets you use patterns to identify files. Mercurial’s pattern handling is expressive.

On Unix-like systems (Linux, Mac OS, etc.), the job of matching filenames to patterns normally falls to the shell. On these systems, you must explicitly tell Mercurial that a name is a pattern. On Windows, the shell does not expand patterns, so Mercurial will automatically identify names that are patterns, and expand them for you.

To provide a pattern in place of a regular name on the command line, the mechanism is simple:

syntax:patternbody

That is, a pattern is identified by a short text string that says what kind of pattern this is, followed by a colon, followed by the actual pattern.

Mercurial supports two kinds of pattern syntax. The most frequently used is called glob; this is the same kind of pattern matching used by the Unix shell, and should be familiar to Windows command prompt users, too.

When Mercurial does automatic pattern matching on Windows, it uses glob syntax. You can thus omit the glob: prefix on Windows, but it’s safe to use it, too.

The re syntax is more powerful; it lets you specify patterns using regular expressions, also known as regexps.

By the way, in the examples that follow, notice that I’m careful to wrap all of my patterns in quote characters, so that they won’t get expanded by the shell before Mercurial sees them.

Shell-Style Glob Patterns

This is an overview of the kinds of patterns ...

Get Mercurial: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.