← All posts

FreeBSD. Moving from Subversion to Mercurial + Trac

Mercurial logo

The other day, vasa_c and I finally moved to Mercurial. We ran into difficulties transferring revisions from Subversion to Mercurial, as well as configuring Trac.

We’ll be working as the user and group www.
The path to the svn data files is “/var/svn”, to hg data “/var/hg”, and to trac data “/var/trac”.
The project name in this example is “EXAMPLE”.
We’ll run hg serve on port 8010.

Installing and configuring Mercurial

Let’s install Mercurial itself:


cd /usr/ports/devel/mercurial
make install clean

Create a directory to store Mercurial data and repositories:


mkdir -p /var/hg/repos/EXAMPLE
chown -R www:www /var/hg

Let’s initialize the project:


cd /var/hg/repos/EXAMPLE
hg init

Edit the file “/var/hg/repos/EXAMPLE/.hg/hgrc”:


[web]
# стиль gitweb
style = gitweb
# при пушенге не использовать SSL
push_ssl = false
# разрешить всем пушинг, по поводу авторизации см. ниже
allow_push = *

Copy the http-auth configuration from “/var/svn”:


cp /var/svn/svn-auth-file /var/hg/hg-auth-file

Or create it yourself:


htpasswd -cb /var/hg/hg-auth-file USER1 PASSWORD1
htpasswd -b /var/hg/hg-auth-file USER2 PASSWORD2

Client-side settings

Let’s add our name for hg and our authentication credentials by editing hgrc:


emacs /home/USER1/.hgrc

You can also specify settings for an individual project, for example in “/home/USER1/projects/EXAMPLE/.hg/hgrc”, where “/home/USER1/projects/EXAMPLE” is your working copy of the EXAMPLE project.

Add the following contents:


[ui]
username = USER1

[auth]
EXAMPLE.prefix = http://hg.example.com/
EXAMPLE.username = USER1
EXAMPLE.password = PASSWORD1

You can read more about hgrc in the article Configuration files for Mercurial

Configuring hgweb

Let’s return to the server and edit hgweb.config:


emacs /var/hg/repos/hgweb.config

Make it look like this:


[collections]
# путь который вырезается из строки запроса (http) = путь до файлов репозитория Mercurial
/var/hg/repos = /var/hg/repos

Starting hg serve

To start it, you’ll need an initialization script, which you can download here, then copy to the “/usr/local/etc/rc.d/” directory:


cd /usr/local/etc/rc.d/
wget -O hg /files/hg.txt
chmod +x hg

Now let’s create a pidfile:


mkdir -p /var/run/hgserve/
touch /var/run/hgserve/hgserve.8010
chown -R www:www /var/run/hgserve/

Start it:


/usr/local/etc/rc.d/hg start

To have hgserve start automatically when the system boots, add the following to /etc/rc.conf:


hgserve_enable="YES"

Configuring Nginx

Let’s add an upstream:


upstream hg {
    server 127.0.0.1:8010;
}

And add a host:


server {
    listen 80;
    server_name hg.example.com;
    location / {
        auth_basic "Password, please!";
        auth_basic_user_file /var/hg/hg-auth-file;
        proxy_pass http://hg;
    }
}

Restart nginx:


nginx -s reload

Importing data from Subversion

To import data from Subversion, we’ll need to install hgsvn (if you don’t have Subversion, skip the steps related to it):


cd /usr/ports/devel/hgsvn
make install clean
rehash

Import data from Subversion, such as revisions and their changelogs:


cd /var/hg/repos
hgimportsvn file:///var/svn/repos/EXAMPLE
cd EXAMPLE
hgpullsvn

You can read a little more about hgsvn in the article Converting from Subversion to Mercurial.

Configuring Trac

Let’s assume that we already have Trac installed, configured, and running, so I’ll describe only the changes.

For Mercurial support in Trac, we’ll need to install the mercurial-plugin plugin:


svn co http://svn.edgewall.org/repos/trac/plugins/0.11/mercurial-plugin mercurial-plugin
cd mercurial-plugin
python setup.py bdist_egg
easy_install dist/TracMercurial-0.11.0.[x]-py2.[x].egg

Open your project’s configuration file:


emacs /var/trac/EXAMPLE/conf/trac.ini

And change the corresponding sections and variables to look like this:


[trac]
repository_type = hg
repository_dir = /var/hg/repos/EXAMPLE

[components]
tracext.hg.* = enabled

[hg]
# -- Show revision number in addition to the changeset hash
show_rev = yes
# -- Changeset hash format
node_format = short
# hex:   Show the full SHA1 hash 
# short: Show a shortened hash for the changesets

After installing the plugin and configuring the project, don’t forget to restart Tracd or whichever other web server you use to access Trac.

Now, if question marks appear in your Timeline instead of Cyrillic revision comments, you need to set HGENCODING=“utf-8”:


HGENCODING="utf-8"
export HGENCODING

You can view the complete initialization script for tracd here (it sets HGENCODING).

You can investigate the problem of garbled characters in displayed changelog messages in more detail by looking at the Mercurial source code, specifically the “tolocal” function in the file “/usr/local/lib/python2.6/site-packages/mercurial/encoding.py”.

A few articles about Mercurial and Trac:

Mercurial. Automatically updating a project using hooks
vasa_c’s articles about Mercurial
Installing and configuring Trac on FreeBSD
Moving Trac to Nginx and tracd

Send me links to other articles and I’ll publish them here.

Comments are closed. Read the original discussion in Russian →