How to add symbolic name for Mercurial repository

When you clone a repository in Mercurial, it creates a file .hg/hgrc in the new clone repository. Typically, this has the following lines:

[paths]
default = https://joe@someserver.org/joe/foobar

That is, it has the login, the source location (directory or internet domain), the repository owner and finally the repository name. This assigns a symbolic name of default to this repository. After this any push or pull command is done with this repository.

If you created a new Mercurial repository and want to push it to another existing repository, then the command is:

$ hg push https://joe@someserver.org/joe/foobar

If you want to push or pull without having to specify the repository, then just create a .hg/hgrc file and add the above lines to it.

If you want to add other repositories to push-pull from, add them on separate lines with a different symbolic name, not default:

[paths]
default = https://joe@someserver.org/joe/foobar
bkp = https://joe@bkpserver.org/joe/foobar

You can push-pull from that repository using the symbolic name you have given it:

$ hg pull bkp

Tried with: Mercurial 2.0.2 and Ubuntu 12.04 LTS

About these ads

How to view files ignored by Mercurial

It is sometimes useful to see which files are being ignored in a Mercurial repository. For example, there might be some .hgignore rules that might be getting applied without your full knowledge. Such files will not be tracked or committed.

Seeing the list of ignored files in a local repository is easy:

$ hg status --ignored

Or if you prefer the short version:

$ hg st -i

Tried with: Mercurial 2.0.2 and Ubuntu 12.04 LTS

MercurialEclipse plugin for Eclipse

If you use Eclipse as your IDE and Mercurial as your version control system, then you might find the MercurialEclipse plugin useful. If you add, remove, rename or move source files using Eclipse, it is a pain to synchronize these changes to the underlying Mercurial repository. If you use the MercurialEclipse plugin, all can make all the changes you wish directly to the repository.

Install

Go to Help > Install New Software > Add and add http://mercurialeclipse.eclipselabs.org.codespot.com/hg.wiki/update_site/stable to Location and click OK. Pick the Stable release and proceed with the rest of the installation. Restart Eclipse after the plugin is installed.

Usage

The big problem I have with MercurialEclipse is that it will not work with existing repositories. You have to clone an existing repository using MercurialEclipse, so that it can create a project of that in your Workspace. This is the only way to begin using it. To do this, go to File > New > Project > Mercurial > Clone Existing Mercurial Repository. Provide the URL or local location and create the project.

To do any Mercurial operation, click on the relevant project, directory or source file in Project Explorer and choose Team. All the relevant Mercurial operations can be seen listed in this right-click menu.

The status of the files in the repository are indicated by icon overlays in Project Explorer. You can do almost all the Mercurial operations from inside Eclipse, including viewing the history log and graph, committing, changing branches and so on.

Tried with: MercurialEclipse 2.1.0, Eclipse 3.7.2 and Ubuntu 12.04 LTS

CDiff

If you use Mercurial or Git at the shell, cdiff is a useful tool to have around. By default, hg diff or git diff is displayed in the classic diff format. This displays the before-after changed lines one below the other. Personally, I find this very confusing. I very much prefer the side-by-side diff view used in GUI diff programs. cdiff is a great way to get this side-by-side diff view at the shell in full colors!

Install

cdiff is a Python module that can be installed from PyPI using this command:

$ sudo pip install cdiff

Note: Do not confuse this package with the colordiff package that is available in Ubuntu repositories. That is something else!

Usage

Just invoke cdiff in any subdirectory of a Mercurial or Git repository. To get side-by-side view use cdiff -s

Tried with: cdiff 0.9.2, Python 2.7.3 and Ubuntu 12.04 LTS

How to install Mercurial Keyring extension on Ubuntu

The Mercurial Keyring extension eliminates the pain of having to authenticate yourself with username and password when you need to push or pull from repositories. Enabling this extension on Windows is pretty easy with TortoiseHg.

To install and enable this extension on Ubuntu:

  • Newer distributions of Ubuntu have a package of this extension. You can try:
$ sudo apt-get install mercurial-keyring
  • If there is no package of this extension, then install the python-setuptools package if it is not already present. This provides the easy_install program which is needed for the next steps.

  • Install the keyring Python package using easy_install:

$ sudo easy_install keyring
  • Install the mercurial_keyring Python package:
$ sudo easy_install mercurial_keyring
  • Enable the mercurial_keyring package in Mercurial by modifying the configuration file as explained here.

Tried with: Mercurial 2.0.2 and Ubuntu 12.04.2 LTS

How to have a global hgignore for Mercurial

The hgignore file can be used to help Mercurial in deciding which files to ignore. If you find yourself repeating the same file patterns in the hgignore files of your Mercurial repositories, then you might want to create a global hgignore file.

First, create a .hgignore file in your home directory. Fill it up with patterns just like you did for your repository’s hgignore.

Next, add this entry to your global Mercurial configuration file:

[ui]
ignore = ~/.hgignore

Tried with: Mercurial 2.0.2 and Ubuntu 12.04 LTS

How to use a pager with output from Mercurial

The output of many commands in Mercurial can spill beyond the height of the terminal. An external pager can be applied to this output automatically by using the pager extension that ships with Mercurial.

In the Mercurial configuration file, first enable the plugin by using the word pager and next indicate the pager program you would like it to use. For example:

[pager]
pager = less

To use the pager only when the output is more than one page and to preserve the color output of Mercurial, so I use this:

[pager]
pager = less --quit-if-one-screen --RAW-CONTROL-CHARS --no-init

Tried with: Mercurial 2.0.2

Mercurial: Not trusting file from untrusted user or group

Mercurial gives this error for the hgrc (Mercurial configuration file) when the user running a Mercurial command is not the same as the owner of hgrc. This typically happens on servers, shares or distributed filesystems.

To fix this, add a trusted section to the configuration file to tell Mercurial to trust the user or group reported in the error.

For example, if Mercurial complains that it does not trust joe and you happen to trust him, add him to hgrc:

[trusted]
users = joe

Tried with: Mercurial 2.0.2 and Ubuntu 12.04.1 LTS

How to create aliases for Mercurial commands

Just like aliases in bash, you can create aliases for Mercurial commands. This functionality is available through the alias extension. Just create a new section in your Mercurial configuration file and add aliases.

For example, to create an alias to show log in graph format and another to show compact log:

[alias]
lg = log --graph
lc = log --style=compact

Even Mercurial commands can be replaced with an alias.

Tried with: Mercurial 2.0.2 and Ubuntu 12.04.1 LTS