Online Book Reader

Home Category

Mercurial_ The Definitive Guide - Bryan O'Sullivan [52]

By Root 1004 0
and its subdirectories.” Mercurial traverses the files and subdirectories in a directory in alphabetical order. When it encounters a subdirectory, it will traverse that subdirectory before continuing with the current directory.

$ hg status src

? src/main.py

? src/watcher/_watcher.c

? src/watcher/watcher.py

? src/xyzzy.txt

Running Commands Without Any Filenames

Mercurial’s commands that work with filenames have useful default behaviors when you invoke them without providing any filenames or patterns. What kind of behavior you should expect depends on what the command does. Here are a few rules of thumb you can use to predict what a command is likely to do if you don’t give it any names to work with.

Most commands will operate on the entire working directory. This is what the hg add command does, for example.

If the command has effects that are difficult or impossible to reverse, it will force you to explicitly provide at least one name or pattern (see below). This protects you from accidentally deleting files by running hg remove with no arguments, for example.

It’s easy to work around these default behaviors if they don’t suit you. If a command normally operates on the whole working directory, you can invoke it on just the current directory and its subdirectories by giving it the name “.”.

$ cd src

$ hg add -n

adding ../MANIFEST.in

adding ../examples/performant.py

adding ../setup.py

adding main.py

adding watcher/_watcher.c

adding watcher/watcher.py

adding xyzzy.txt

$ hg add -n .

adding main.py

adding watcher/_watcher.c

adding watcher/watcher.py

adding xyzzy.txt

Along the same lines, some commands normally print filenames relative to the root of the repository, even if you’re invoking them from a subdirectory. Such a command will print filenames relative to your subdirectory if you give it explicit names. Here, we’re going to run hg status from a subdirectory, and get it to operate on the entire working directory while printing filenames relative to our subdirectory, by passing it the output of the hg root command.

$ hg status

A COPYING

A README

A examples/simple.py

? MANIFEST.in

? examples/performant.py

? setup.py

? src/main.py

? src/watcher/_watcher.c

? src/watcher/watcher.py

? src/xyzzy.txt

$ hg status `hg root`

A ../COPYING

A ../README

A ../examples/simple.py

? ../MANIFEST.in

? ../examples/performant.py

? ../setup.py

? main.py

? watcher/_watcher.c

? watcher/watcher.py

? xyzzy.txt

Telling You What’s Going On

The hg add example in the preceding section illustrates something else that’s helpful about Mercurial commands. If a command operates on a file that you didn’t name explicitly on the command line, it will usually print the name of the file, so that you will not be surprised what’s going on.

The principle here is of least surprise. If you’ve exactly named a file on the command line, there’s no point in repeating it back to you. If Mercurial is acting on a file implicitly, e.g., because you provided no names, or a directory, or a pattern (see below), it is safest to tell you what files it’s operating on.

For commands that behave this way, you can silence them using the -q option. You can also get them to print the name of every file, even those you’ve named explicitly, using the -v option.

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

Return Main Page Previous Page Next Page

®Online Book Reader