Apr8
Easily Track Empty Directories in Mercurial or Git
- posted by: taylor
- no comments
- post a comment
We’re currently in the process of switching from Subversion to Mercurial, and while the process has gone well so far, we’ve run into one issue that required a bit of investigating. It turns out that Mercurial and Git don’t track empty folders. To a certain extent, this makes sense, but when it comes to working with a CMS that creates empty folders which will eventually be filled, this can be troubling. Furthermore, our static framework provides a starting point for organizing images which is made up of multiple empty folders. We need these to be tracked to make sure everyone uses the same structure.
The Solution
Luckily, it’s relatively easy to get around this limitation by adding dummy files to your empty directories. Once these dummy files are added to the repository, their “empty” parent folders will also be tracked.
Looking around, it seems that a lot of people advocate adding .hgignore files to empty folders since those are commonly used anyway, but personally, I think that may cause some headaches down the road; if we’re not actually ignoring any files, they’re unnecessary, and may cause confusion. In addition, once those folders actually contain some files, it’d be nice to remove the blank files to keep things tidy.
For this reason, I’d suggest having your team standardize on a dummy file naming convention, such as .hidden or maybe something a bit more unique like .imulus or .unicorns. That way, we can later recursively delete these files without fear of deleting something important, like .hgignore files.
Taking it a Step Further
Once we figured out how to track empty directories, it was only natural we figure out a way to automate the process of adding blank files. Luckily, Ernesto Méndez has written a Python script to do it for us, and setting it up to use is very simple.
Getting Started
While his site provides a decent rundown on how to use the script, I’d encourage you to take the extra step of copying it to /usr/local/bin and making it executable. Once you do this, you’ll be able to run the script from inside any folder.
To begin, fire up Terminal and copy the script to /usr/local/bin. If you’d like, you can remove the .py extension as I have. You’ll be asked to enter your password.
sudo cp hg-touch.py /usr/local/bin/hg-touch
Next, we need to ensure that it is executable:
sudo chmod u+rwx /usr/local/bin/hg-touch
And that’s it. Now we can run the script from inside any folder and recursively add .unicorn files to every empty directory. Let’s take a look at some examples:
Creating Dummy Files
The following example will create .unicorn files in any empty directory found in the static-framework directory:
hg-touch -p static-framework .unicorn

Removing Dummy Files
Once we’ve added some real files to those directories, we can run the command to remove all the dummy files.
hg-touch -r -p static-framework .unicorn

Be extra careful when using this command to make sure you are removing files you truly know are no longer needed. You will not be prompted to confirm.
Wrapping Up
Although we’ve decided that Mercurial is best suited for us over Git, this script can just as easily be used for tracking empty directories in a Git repository as well. In fact, I’ve used it this way a few times already. You could even rename the script to git-touch if you’d prefer.
Although I haven’t found any real issues with the script, it would be nice to expand upon it and add some features. One that comes to mind is the ability to define a standard dummy file name, yet maintaining the option of overriding it. Surely this would be a relatively simple fix, and the source code is available should you be up to the challenge.
