Version 16 (modified by Luke Plant, 7 years ago) (diff)


Mercurial Branches

This page documents how to use Mercurial, rather than Subversion, to hack on Django. See also DjangoBranches.

Core developers

For people who are core committers, and want to use Mercurial rather than Subversion as their client:

  1. Install the hgsubversion extension (and understand basically how it works).
  2. Clone the Subversion repository (do this using hg version 1.5 or higher, or else the changeset hashes generated by hgsubversion will be incompatible with those from repos generated using newer hg versions, including the official Django hg mirror).:
    hg clone svn+ django
    This will take a Good While (lots of hours), and in some cases can take a Very Long Time for just one changeset (I gave up on r11505).
    1. Clone the official Django hg mirror in the normal way, using hg 1.5 or greater.
    2. Edit .hg/hgrc to say:
      default = svn+
    3. Do hg svn rebuildmeta. This works because the official Django mirror was created by hgsubversion, and is updated only by hgsubversion.
  1. Use normal hg commands to make commits, switch branches, push back to the subversion repository etc. Remember to use hg rebase --svn, and not hg merge.


  • Use the bookmarks extension for git-style local feature branches, or named branches for feature branches that you need other people to see.
  • Alternatively, use the queues extension to manage long lived patches.
  • To collapse several commits into a single commit before pushing back to svn, use the histedit extension
  • To backport a changeset from trunk, use the transplant extension. Or use this handy script which will generate the commit message for you:
    function usage {
        echo "Usage: hg_backport <branch> <hgrevision>";
    if [ $# -ne 2 ]
            exit 1;
    BRANCH="releases/$SHORTBRANCH" # we only backport to release branches.
    hg st -a -r -m | grep '' > /dev/null && { echo "Working directory not clean - exiting" > /dev/stderr; exit 1; }
    SVNREV=`hg svn info -r $HGREV | egrep '^Revision' | cut -f 2 -d ' '`
    if [ "x$SVNREV" = "x" ]
            echo "Can't find svn rev";
            exit 1;
    echo "Backporting Subversion revision $SVNREV"
    hg update $BRANCH || exit 1;
    # Make a commit message first in case the transplant fails.
    hg log -r $HGREV --template "[$SHORTBRANCH] {desc}\n\nBackport of [$SVNREV] from trunk\n" > hg-commit-message.txt || exit 1
    hg transplant $HGREV || exit 1;
    # Modify the commit message
    hg rollback > /dev/null || exit 1
    hg commit -l hg-commit-message.txt || exit 1
    echo "Backport committed."
    The command line is like:
    hg_backport 1.2.X 13643
    where 1.2.X is a directory under 'releases/' in the Subversion repo, and 13643 is a hg revision ID. The change is already committed to the local repo, but not 'pushed', so you can still rollback if changes need to be made.

An alternate approach

Another way of doing the same thing: Use the --filter option to the transplant extension. This requires the use of a Python script. Put the following in, and stick it in a tools directory somewhere:

import os
import sys

msg = sys.argv[1]
patch = sys.argv[2]

branch = os.environ.get('DJANGO_BRANCH',None)
if branch is None:
    branch = raw_input('Backport branch: ')
    print "Backport branch:", branch

msg_file = open(msg, 'r')
outlines = []
firstline = True
for raw_line in msg_file:
    line = raw_line.strip()
    if firstline:
        if line.startswith('#'):
            firstline = False
            print 'MERGING: %s' % line
            outlines.append('[%s] %s' % (branch, line))

svn_revision = os.environ.get('SVNMERGE_REVISION',None)
if svn_revision is None:
    svn_revision = raw_input('SVN Revision: ')
    print "SVN Revision:", svn_revision
outlines.append('Backport of r%s from trunk.' % svn_revision)

# And write the updated message file.
msg_file = open(msg,'w')
for line in outlines:
    print >> msg_file, line

# In case of a clash, write the filename of the message file
print "Log message written to",msg

Then, use the following shell script to do the actual backport:

export SVNMERGE_REVISION=`hg svn info -r $1 | egrep '^Revision' | cut -f 2 -d ' '`
export DJANGO_BRANCH=`hg branch | cut -f 2 -d '/'`
hg transplant $1 --filter /path/to/

If you call this script 'hg_backport', then

The command line is like:

hg_backport 13643

Any hg revision ID can be used as the argument; since the most common usage pattern is to backport the most recent svn commit on trunk, you can use 'tip':

hg_backport tip


If you make two commits, and only want to push the first, you will have some trouble, since the HgSubversion extensions doesn't support specifying revisions with the hg push command when pushing to a Subversion repository. Nice solutions to this welcome!

Back to Top