Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial for Beginners: The Definitive Practical Guide

How do you configure it to ignore files?

Ignore is configured in a normal text file called .hgignore in the root of your repository. Add it just like a normal file with:

hg add .hgignore

There are two syntax options available for file matching, glob and regexp. glob is unix-like filename expansion and regexp is regular expressions. You activate each by adding syntax: glob or syntax: regexp on a line by itself. All lines following that will use that syntax, until the next syntax marker. You can have as many syntax markers as you want. The default syntax is regexp, so if you only use regexp you don't need any syntax marker.

You can add comments with #

Example:

# python temporary files
syntax: glob
*.pyc

#editor autosaves
*~

# temporary data
syntax: regexp
temp

Ignore only applies to unmanaged files (i.e. files that are not already checked in). To ignore files that are under version control, you can use the switches -I and -X.


How do you see what's uncommitted, or the status of your current codebase?

To see a list of files that have been changed:

$ hg status

This will print each file that has been changed along with its status, which can include:

  • M - Modified. The file has been changed and the changes have not been committed.
  • A - Added. The file was not tracked before, but if you commit Mercurial will begin tracking it.
  • R - Removed. The file was tracked before, but if you commit Mercurial will cease tracking it in this and future commits.
  • ? - Unknown. The file is not currently tracked by Mercurial. Committing will have no effect on it unless you use hg add to add it.
  • ! - Missing. The file was tracked but Mercurial cannot find it in the working copy.

To see the changes that have actually been made to the files:

$ hg diff

How do you create a new project/repository?

$ hg init my-repository

How do I interface with Subversion?

There are three ways:


The convert extension will clone an existing Subversion repository into a Mercurial one. It comes with Mercurial. It works roughly like this:

hg convert <Subversion URL or directory> <path to new Mercurial repository>

For example this will grab the trunk of the SixApart memcached repository.

hg convert http://code.sixapart.com/svn/memcached/trunk

The extension can incrementally bring in new revisions from a Subversion repository into the Mercurial one (a little like pull). However it does not support taking Mercurial revisions and sending them back to Subversion (no push). [XXX: Correct this if it is wrong].


The hgsubversion extension. It is in many ways the most sophisticated solution as it uses the Subversion API to communicate with the Subversion repository. It aims to become the hg-svn bridge. It allow full round-tripping of revisions (full clone, pull, and push), However as of this writing [XXX: Amend this if/when it becomes incorrect] it is still in development and there are not yet official releases. As a consequence it works with only the most up-to-date Mercurial (1.3 as of this writing).

  • It maps tags and branches (preceding all tags with tags/ to distinguish them from equivalently named branches).
  • It maintains a special branch closed-branches for closing off branches which are removed in Subversion.
  • It requires that the Subversion repository be laid out according to the convention of trunk/branches/tags.
  • The command set is typically hg svn <subcommand> though it aims at being integrated to the point that you don't need the 'svn' part (i.e. it wants to treat a Subversion clone as much as possible like any other Mercurial repository).;

It works like this:

clone:

hg svnclone <Subversion URL> 

OR (only for svn:// URLs)

hg clone <svn:// URL>

pull:

hg svn pull

push:

hg svn push

incoming:

hg svn incoming

outgoing:

hg svn outgoing

Checking out an entire repository:

hg svnclone http://code.sixapart.com/svn/memcached

The hgsvn utility (bitbucket tree). Up until recently this only let you clone and pull a Subversion repository, but as of hgsvn 0.1.7 it supports push. [I do not know how well it does push. Anyone with more experience should update this.] It has the following notable features:

  • It generates a Mercurial tag for every SVN tag.
  • It puts a local tag on every changeset to mark its SVN revision.
  • It puts every Mercurial revision on a named branch named after its SVN branch. For example branches/some-feature would be like hg branch some-feature. It puts the trunk on trunk (i.e. nothing is on the Mercurial default branch, unless the user explicitly switches to it.)
  • It will try to identify branches and tags, and create them but if it can't it just skips them. This is handy when the Subversion repository is not following the conventional trunk/branches/tags layout.

It works like this:

clone:

hgimportsvn <Subversion URL>

pull:

hgpullsvn

push:

hgpushsvn

incoming:

hgpullsvn -n

outgoing:

hgpushsvn -n

Checking out an entire repository:

hgimportsvn http://code.sixapart.com/svn/memcached

Checking out just the trunk:

hgimportsvn http://code.sixapart.com/svn/memcached/trunk

How do you compare two revisions of a file, or your current file and a previous revision?

Both use hg diff. When hg diff is used all changes in the working copy and the tip (the latest commit) is displayed.

For "How do you compare two revisions of a file?"

$ hg diff -r{rev1} -r{rev2} {file.code}

The above command will show different between rev1 and rev2 of "file.code".

For "How do you compare your current file and a previous revision?"

$ hg diff {file.code}

The above command will show different between the current version of "file.code" and the lastest revision (the lastest commited).

:D