Introduction

Mercurial as well as other well-known version control systems cannot version directories. A "workaround" for this issue is to use placeholder files which are place into empty directories. These placeholder files can then be committed into the repository and will make sure that, upon checkout, the directory tree is entirely reconstructed. This solution is also suggested in the FAQ. Note however, that using that workaround might not be a good idea. Creating missing directories during the build process might often be a better solution.

Sometimes a solution where the missing directories are created by some magic is not practicable and people will face the problem of managing such placeholder files. In particular, the problem with using placeholder files is that you need to create them, and delete them, if they are not necessary anymore (because there were added sub-directories or files). With big source trees managing these placeholder files can be cumbersome and error prone.

I had been confronted with such a situation several days ago and it wasn't the first time. This is why I decided to write an open source tool which can manage the creation/deletion of such placeholder files automatically. It creates placeholder files in all empty directories. If later on new files or directories are put into such directories, the placeholder files are not necessary anymore and, thus, are removed automatically.

This tool is licensed under the GPLv3 and runs under Mono (Linux) as well as under Microsoft .NET platform (Windows). It can be downloaded here: http://code.google.com/p/markemptydirs/downloads/list

Use Cases

In the following sections some typical use cases are described and how the tool can help you getting you work done. For clarity, I use directory names with upper case letters and file names with lowercase letters in the following examples.

Create placeholder files

Within a directory PROJECT find all "leaf" directories that do not contain any files or sub-directories, and create a placeholder file .emptydir within each such "leaf" directory.

For example, let's assume the following source tree:

[PROJECT]
        |
        *--[DIR1]
        |       |
        |       *--[DIR1-1]
        |
        *--[DIR2]
        |       |
        |       *--file1
        |
        *--[DIR3]

A tree with the corresponding placeholder files will then look like this:

[PROJECT]
        |
        *--[DIR1]
        |       |
        |       *--[DIR1-1]
        |                 |
        |                 *--.emptydir
        |
        *--[DIR2]
        |       |
        |       *--file1
        |
        *--[DIR3]
                |
                *--.emptydir

Using the MarkEmptyDirs tool one would simply do:

$> MarkEmptyDirs PROJECT

Update placeholder files

Let's assume the PROJECT directory has undergone some changes. Directories and/or files have been added and/or deleted. This requires to synchronize the corresponding placeholder files. In particular, some now are not needed anymore and new ones may now be necessary.

For instance, have a look at the following tree:

[PROJECT]
        |
        *--[DIR1]
        |       |
        |       *--[DIR1-1]
        |                 |
        |                 *--.emptydir
        |                 |
        |                 *--newfile.txt
        |
        *--[DIR2]
        |
        *--[DIR3]
                |
                *--.emptydir

DIR1-1 now contains a new file newfile.txt and a previously created .emptydir file. The former now acts as a placholder and, thus, the latter is not needed anymore. DIR2 now is empty and requires a placeholder file.

Consequently, after updating the placeholder files the tree should look like this:

[PROJECT]
        |
        *--[DIR1]
        |       |
        |       *--[DIR1-1]
        |                 |
        |                 *--newfile.txt
        |
        *--[DIR2]
        |       |
        |       *--.emptydir
        |
        *--[DIR3]
                |
                *--.emptydir

Using the MarkEmptyDirs tool for synchronization one would simply do (again):

$> MarkEmptyDirs PROJECT

Delete placeholder files

There are situations where one wants a clean tree which does not consist any placeholder files. So all .emptydir files should be removed.

This can simply be achieved by using the optional --clean option:

$> MarkEmptyDirs --clean PROJECT

List placeholder files

In order to list all placeholder files just execute:

$> MarkEmptyDirs --list PROJECT

Note that this command is simply a shorthand for:

$> MarkEmptyDirs --short --dry-run --clean PROJECT