From Eigen
Revision as of 10:06, 24 July 2020 by Ggael (Talk | contribs)

Jump to: navigation, search

Mercurial, also known as hg, is the source-control management (SCM) tool that we use for Eigen.

Installing Mercurial and finding documentation

  • If you are using Windows or Mac, download it from [website].
  • If you are using Unix/Linux, just install Mercurial from your package manager.

If you want a graphical user interface, install Tortoise-Hg, but below in this page we will only be referring to the command-line interface.

Below in this page we explain some usual commands, but Mercurial has lots of documentation available on the Web:

Basic usage

Getting the source

Initial checkout:

 $ hg clone

or if you have a write access to the repository you have two options:

 $ hg clone ssh://        # See [1] to setup ssh
 $ hg clone   # password login

After that, to update your checkout (get the new changes made to the repository):

 $ hg pull -u       # hg pull brings the changes into your store, and -u updates
                    # your working directory to the latest revision.
                    # 'hg pull -u' is the same as 'hg pull && hg update'.

Configuring Mercurial

Before using Mercurial to make Eigen changes/patches, please make sure that you have configured it as described in this section. To do so, edit (or create) your mercurial config file.

  • On Unix-like systems, the global config file is ~/.hgrc
  • On Windows systems, the global config file is Mercurial.ini in your home directory

Note that if you need a different mercurial config for another project, you can also use a per-clone config file: .hg/hgrc inside the clone directory. We recommend using the global config file unless you really know what you are doing.

Put these lines in the Mercurial config file:

git = 1
showfunc = 1
unified = 8

commit = -v

username = Your Real Name <>
merge = internal:merge
editor = YourFavoriteEditorExecutable

In the above config file sample, note that some lines are placeholders that you must replace with correct values:

  • Enter your real name and email address. Will be used to credit you, and contact you in case of a problem.
  • Enter your choice of text editor. Will be used to write commit messages.

Note to Windows users

In order to deal with Windows end-of-line encoding, please follow these instructions.

In the end you should be adding something like this to your config file:

** = cleverencode:
** = cleverdecode:
eol = crlf
pretxncommit.crlf = python:hgext.win32text.forbidcrlf


By default, what you now see is the development branch, called the 'default branch'.

 $ hg branch        # show the name the current branch
 $ hg branches      # show the list of branches
 $ hg up 2.0        # switch to 2.0 branch

Local commit

First of all, make sure you have put any new files under revision control:

 $ hg add <filename> # Put a file under revision control

Then you can examine before committing:

 $ hg status        # see what files are affected
 $ hg diff          # show differences that are to be committed

To proceed with the local commit:

 $ hg commit        # take care, this will commit changes for the whole repository
                    # even if you are in a sub-directory (this is not as SVN does)
 $ hg commit .      # if you want to commit only the local directory

This will open the text editor that you specified in your config file. This will also use the identity that you specified in your config file.

Undoing uncommitted changes

You can undo all uncommitted changes by doing:

 $ hg revert somefile   # undo uncommitted changes to this file
 $ hg revert -a         # undo all uncommitted changes to all files. Careful!

Undoing a local commit

In case you want to undo a local commit, e.g. to deal with typos in the commit log or similar, do:

 $ hg rollback      # roll back the last transaction

Notice that this will only undo the committing action. See previous section for undoing the changes to your working copy.

Only use this technique for local commits. Otherwise, it doesn't make sense, since it cannot be guaranteed that other users already pulled in public changes.

Generating a patch

Once you have made a local commit, you can generate a patch from it:

 $ hg export tip > somefile

Here, 'tip' means the latest revision. Try "hg help export" to see other options. This generates a file that you can then attach to a bug report or to an e-mail to the Eigen mailing list. If you have properly configured your identity as above before committing, then importing your patch will properly credit you.

Pushing the local changes to the central repository

Once you have made a local commit, you can push it to the central repository.

 $ hg out           # to check what you are actually pushing
 $ hg push          # push your commits. No need to give the repository if this is the one cloned

You may see this error message:

 abort: push creates new remote heads!
 (did you forget to merge? use push -f to force)

This means that one of your modified files has been modified meanwhile in the repository. You need to merge before you can push: see next section. Do not use push -f as mentioned in that message, as there may be a conflict to resolve manually.

Merge changes from another repository

 $ hg in <repo>     # check what's coming in
 $ hg pull <repo>   # pull changesets from <repo>
 $ hg merge         # merge the new tip from <repo> into our working directory
 $ hg parents       # see the revisions that have been merged into the working directory
 $ hg commit        # commit the result of the merge

After 'hg merge', you may see this error message:

 warning: conflicts during merge.
 merging <filename> failed!
 0 files updated, 0 files merged, 0 files removed, x files unresolved

In that case, read the section "Resolving Conflicts" below.

Resolving conflicts

This is very similar to SVN. After a 'hg merge' has found conflicts, the files are left with special markers at the locations of conflicts, like:

 <<<<<<< local
 Hello Sir
 Hello Madam
 >>>>>>> other

You have to edit these files manually to resolve the conflict. Once a file <filename> is fixed, do:

 hg resolve -m <filename>   # mark the file as resolved

Once all the files are marked as resolved, you can proceed with committing.

Viewing history

The basic command here is 'hg log'. If you want to see full information about a certain revision, you can do:

 $ hg log --style changelog -r 1000  # examine revision 1000
 $ hg log -u someuser  # see all commits made by someuser

But you can also enable a couple of extensions that provide fancier views. Edit (or create) your global .hgrc file (in your home directory) and add these lines:

hgext.graphlog =

You can now enjoy a graphical view of the commit history:

 $ hgk   # or try 'hg view' if that doesn't work

Or if you prefer a plain text output of the graph:

 $ hg glog

Finally, if you want something more powerful, have a look at the following external programs: hgtk and hgview. Hgtk allows many things, like viewing only as specific branch, viewing only merges, etc. Do:

 $ hgtk log

Hgview is simpler but still allows viewing a specific branch:

 $ hgview

Advanced usage

Porting a changeset to another branch

First of all, you should a separate local clone for each of the branches that you're interested in. If you want to transplant a changeset, say, from the default branch to the 2.0 branch, enter your clone of the 2.0 branch, make sure that you have pulled the changeset that you want to transplant, and do:

 $ hg export REV | hg import -

There also is a transplant extension.

Mercurial Queues

If you're feeling dangerous today, read about Mercurial Queues. This makes Mercurial about as dangerous as Git.

Pushing many local commits as a single one

If you want to do this, you want Mercurial Queues, see above. Specifically, the qfold command. If you're not very comfortable with Mercurial, don't bother.

Splitting a patch into smaller patches

The best way to do that is using the Crecord extension. It has an interactive interface to let you select line-by-line or hunk-by-hunk how to split your patch.

This extension also provides a 'hg qcrecord' command to interoperation with Mercurial Queues, making it even more powerful. We explain it there.

Rebasing changesets

Warning: rebasing is not something trivial, and can break a repository. Typically, if you push changes, then do some rebasing, this is a very bad thing. The remote repository might get corrupted.

The most obvious way is to use 'hg rebase', or 'hg update --rebase'. See respective documentation.

If you are such a power user that you know how to use Mercurial Queus (MQ, see above), this is another way to do almost automatic rebasing. Once your changeset is a MQ patch, you can pop it, update your hg repository to another changeset, and push it again.

Receiving commit notification

The Bitbucket web interface offers RSS and Atom feeds.

If you prefer email notification, subscribe to the eigen-commits mailing list. Instructions are the same as for the eigen mailing list, just replace "eigen" by "eigen-commits" everywhere.

Search and replace on whole directory

If you do a search-and-replace operation on a whole directory, you probably do not want it to affect Mercurial's own internal files! So you must avoid all .hg directories. You can do:

 $ find . -type f | grep -v \\.hg | xargs sed -i 's/oldstring/newstring/g'