Scripting Mercurial

Dan Fuchs posted a short article about scripting Mercurial to see what files an incoming changeset modifies.

I am always amazed at how easy Mercurial (and other free software tools) make this sort of thing.

For example, in my ~/bin/ directory I have a small shell script:

#!/usr/bin/ksh -p

function fmtlog
{
        awk -F^L '{
                printf "%12s | %12s | %-18s |",$1,$2,$3;
                for (k = 4; k <= NF; k++) {
                        printf " %s", $k;
                }
                printf "\n";
        }'
}

tmpl='{date|age}^L{node|short}^L{author|user}^L{desc|firstline}\n'
hg in --template "${tmpl}" "$@" | grep '^L.*^L.*^L' | fmtlog

This is a tiny and pretty naive wrapper around “hg incoming” but it shows a nice one-line summary of each incoming changeset:

$ cd /ws/mercurial/gker
$ inc ../crew
      3 days | 7dfac37cfabf | mpm          | dirstate: improve performance for building _dirs
      3 days | 892d27fb04a5 | mpm          | osutil: fix some braindamage
      3 days | 0d513661d6c2 | mpm          | listdir: add support for aborting if a certain path is found
    21 hours | aafe12bd7174 | msommerville | hgk: Display branch name for each head (issue 740)
$

It’s trivial to write a similar “out” wrapper for outgoing changes, and it’s also possible to use the standard options of “hg incoming” or “hg outgoing”, like “–limit”:

$ inc --limit 2 ../crew
      3 days | 7dfac37cfabf | mpm          | dirstate: improve performance for building _dirs
      3 days | 892d27fb04a5 | mpm          | osutil: fix some braindamage
$

or to pull all the changes from a remote network repositor, look at a quick summary, and save the incoming changesets in a local bundle:

$ inc --bundle /tmp/crew.hg http://hg.intevation.org/mercurial/crew
      3 days | 7dfac37cfabf | mpm          | dirstate: improve performance for building _dirs
      3 days | 892d27fb04a5 | mpm          | osutil: fix some braindamage
      3 days | 0d513661d6c2 | mpm          | listdir: add support for aborting if a certain path is found
    21 hours | aafe12bd7174 | msommerville | hgk: Display branch name for each head (issue 740)
$

Then we can pull from the local bundle, instead of going through a network round-trip once again:

$ hg pull /tmp/crew.hg && rm -f /tmp/crew.hg
pulling from /tmp/crew.hg
searching for changes
adding changesets
adding manifests
adding file changes
added 4 changesets with 6 changes to 4 files
(run 'hg update' to get a working copy)
$ inc http://hg.intevation.org/mercurial/crew
$

It’s little details like this one, and the feeling that whenever an extension is needed, everything is open, easy to modify, integrate, and adapt, that make UNIX a pleasant working environment.

Advertisements