Thursday, August 21, 2008

rorgitignore : .gitignore files specific for Ruby on Rails

rorgitignore : .gitignore files specific for Ruby on Rails

What?

It's a miniscript that finds empty directories below current directory and adds empty .gitignore files to them.

It also adds a main .gitignore file at the current directory with Rails project typical contents.

Why?

Because git tracks contents, not files, it doesn't save any empty directories, since there is no content to track.

This means that when you clone your project from a git repository, it is missing the log, tmp, lib and other directories.

This small script fixes that, so git adds even empty directories.

How?

Well, it adds empty .gitignore files to any empty directories below the current one.

Additionally it creates a main .gitignore file that includes tmp, log, tmp, config/database.yml and db/*.sqlite3 (all these usually contain dynamic discardable content that we don't commonly add to a SCM repository).

Where's the script?

Here it is:

#!/usr/bin/env bash

echo "Creating .gitignore files in empty directories ..."
for URL in `find . -type d | grep -v \.git`; do
    if [ `ls $URL | wc -l` -eq 0 ]; then
        echo "touch $URL/.gitignore"
        echo "git add -f $URL/.gitignore"
    fi
done | bash -x
echo ".gitignore files in empty directories created."
echo

echo "Creating main .gitignore file ..."
/bin/rm -f .gitignore
echo "config/database.yml" >> .gitignore
echo "db/*.sqlite3" >> .gitignore
echo "log/*.log" >> .gitignore
echo "log/*.pid" >> .gitignore
echo "tmp/**/*" >> .gitignore
echo "Created main .gitignore file."
echo


echo ".gitignore files created"
echo "=============================================================="
find . -name .gitignore
echo "=============================================================="
echo

echo "Content of ./.gitignore"
echo "=============================================================="
cat ./.gitignore
echo "=============================================================="
echo

Remember to use git add -f .

Bye!

See you next time.

Update Sep 8, 2008

Here's a simpler script for those that just want the good part, this just outputs the commands to add an empty .gitignore file to all empty directories in a git project and git add -f'em to the repo.

For this you're supposed to already have init'ed the git repo. This justs generates the commands and pipes them to bash.

for DIR in `find . -type d | sed -re 's/\.\///g' | grep -v '^\.git'`; do
    [ `ls -a $DIR | wc -l` -le 2 ] && \
    echo Creating and git-adding $DIR/.gitignore && \
    touch $DIR/.gitignore && \
    git add -f $DIR/.gitignore
done