Subject: [RFC] Storing empty directories in Git

From: Øyvind A. Holm <[email-masked]>
Date: Sun, 30 May 2010 16:29:11 +0200

I sometimes need to store empty directories in Git, and instead of 
putting empty .gitignore files everywhere as has been suggested, I 
created two small shell scripts (git-store-dirs and git-restore-dirs) to 
store all empty directories in a file named ".emptydirs" at the top of 
the working tree. The directories are stored as null-separated strings 
to avoid problems with wacky things like \n or semicolons in the 
directory names.

Is this a viable method of fixing the emptydir problem? A default file 
name like ".gitdirectories" could be used, or maybe it’s possible to 
store a file like this inside the .git/ area if we can come up with a 
way to store this metadata properly.

The scripts are stored at http://gist.github.com/419015 , but I also 
include them here:

### git-store-dirs {{{

#!/bin/sh

# git-store-dirs
# Store a list of all directories to $dirfile to be able to restore empty 
# directories. The list is \0-separated in case there are some directory names 
# containing (ugh) \n. Use git-restore-dirs(1) to recreate directories.

dirfile=.emptydirs
test -d .git/. || { echo $0: Has to be run from the toplevel of the working tree >&2; exit 1; }
find -type d -empty -print0 | grep -E -v -z '/\.git(/|$)' | LC_ALL=C sort -z >$dirfile
git add $dirfile

### }}}
### git-restore-dirs {{{

#!/bin/sh

# git-restore-dirs
# Restore empty directories created by git-store-dirs(1)

dirfile=.emptydirs
test -d .git/. || { echo $0: Has to be run from the toplevel of the working tree >&2; exit 1; }
test -e $dirfile || { echo $0: No $dirfile file found >&2; exit 1; }
xargs -0 mkdir -p <$dirfile

### }}}

Cheers,
Øyvind

+-| Øyvind A. Holm <[email-masked]> - N 60.39548° E 5.31735° |-+
| OpenPGP: 0xFB0CBEE894A506E5 - http://www.sunbase.org/pubkey.asc |
| Fingerprint: A006 05D6 E676 B319 55E2  E77E FB0C BEE8 94A5 06E5 |
+------------| 06a9d9be-6bf2-11df-8623-90e6ba3022ac |-------------+