Wednesday, January 6, 2010

Simple script to add a file or directory to Mercurial's .hgignore

If you use Mercurial as a Source Control Management (SCM) system (and if you don't, then you should seriously ask yourself why) then you have probably grown used to the rather repetitive editing of the .hgignore file, to add files / folders that should not be under version control.

The need is particularly acute if you use IDEs and / or application frameworks, as they both tend to have their own quirky way of arranging files, and most of the folders they use or generate should really not be under source control (especially if you are working with a number of co-workers, who may very well use different IDEs, platforms or even OSes).

The process is rather straightforward: you add a couple of lines (possibly preceded by a comment) to .hgignore, usually specifying the folders to avoid using a RegEx.

Having grown tired of typing this up every time, I decided that a simple shell script was in order[*] to save myself some typing (as Martin Fowler said: "I'm a lazy person: I'm prepared to work pretty hard to save myself work").

The only quirk is that there is a minor difference in the RegEx you want, depending on whether you are trying to ignore a directory or a file (possibly a file pattern, such as all .class files, where you would use something like ".*\.class$" for your RegEx).
The script handles this very simplistically, checking whether the passed parameter corresponds to an existing directory, and, if not, treats it as a straightforward pattern that simply gets copied to .hgignore.

I am positive there may well be a number of corner cases where this all fails abysmally; but in that event, it's probably much simpler to directly edit .hgignore, than trying to ferret out the cleverest possible algorithm.

Here it is, in all its glorious banality: I post it here, just in case others may find it useful (if you do, and find yourself improving on it, please feel free to post back comments with your suggestions).
#!/bin/bash
#
# Simple script to add a folder to the list of those ignored by hg
# Usage: hgignore FOLDER [COMMENT]
#
# Created by M. Massenzio (c) 2010 AlertAvert.com

USAGE="Usage: hgignore FOLDER [COMMENT]"

if [ ! -e .hgignore ]; then
echo "Could not find .hgignore in the current directory:"
echo "are you sure this project is being tracked by HG (Mercurial)?"
echo ""
echo $USAGE
exit -1
fi

if [ ! -z "$2" ]; then
echo "# $2" >> .hgignore
fi

if [ -z "$1" ]; then
echo $USAGE
exit -2
fi

echo "syntax: regexp" >> .hgignore
if [ -d $1 ]; then
echo "^$1\$" >> .hgignore
echo "Added directory $1 to the list of ignored directories in .hgignore:"
else
echo "\\$1\$" >> .hgignore
echo "Added file pattern $1 to the list of ignored files in .hgignore:"
fi
echo "" >> .hgignore

echo "--- Contents of .hgignore ---"
cat .hgignore
echo "--- File ends ---"
[*] I do not even attempt to pretend I know the first thing about shell scripts: however, the above seemed trivial enough that I felt reasonably confident that it should do no harm, despite my n00b-ness.

Blogged with the Flock Browser

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.
    I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    If anyone wants to become a Java developer learn from Java EE Online Training from India.
    or learn thru Java EE Online Training from India .
    Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete