Rails, Git, and Empty Directories

May 11th, 2008 by Chris Kaukis - Tags: ,

I was getting a little fed up touching every single directory that was empty after I created a new Rails app and than pushing it to a git repo and having it not include the empty directories. So, I created a short quick script to help me out. I didn’t test it more than a couple times, but it seems to do the job. Anyway, here it is:

def touch_gitignore(path = '.')
  Dir[File.join(File.expand_path(path), '**')].each do |file|
    if File.directory?(file)
      touch_gitignore(file)
      if Dir[File.join(file, '*')].empty?
        `touch #{File.join(file, '.gitignore')}`
        puts 'touched: ' + file
      end
    end
  end  
end
 
ARGV.first ? touch_gitignore(ARGV.first) : touch_gitignore

Yes, I’m sure it can be improved. As I said, it was quick and dirty, out of frustration even.

As an example how to run it, if I named the file I pasted the code from above into ‘gitignore.rb’ then it would be something like this:

chris$ rails test
chris$ ruby gitignore.rb test

If you found this useful, how about a recommendation on working with rails?

You can leave a response, or trackback from your own site.

6 Comments

  1. bryanl says:

    FWIW, I believe Rails Edge has support to automatically create directories when needed. This problem should go away soon.

  2. I’m curious…since git is a “content” tracker, and there’s no content to an empty directory, why do you need git to track it anyway?

  3. Chris Kaukis says:

    Well, if you use the .gitignore file to exclude some files, when you commit a pretty young rails project, you can be left with things like empty tmp, lib, vendor, log directories. I was coming across cases where if I cloned a repo, I would have these missing directories and some generators and other scripts/plugins would complain. It’s not obvious right away these were missing.

  4. Chris –

    I liked your idea and thought – why not pull this into Sake? So I did. Make sure you have sake installed (sudo gem install sake) and install my task:

    sake -T http://pastie.caboo.se/196733.txt
    sake -i http://pastie.caboo.se/196733.txt

    Now from your project root, you can just run:

    sake git:ignore

    and it will automatically add the .gitignore files to empty directories. I also updated the code to handle cases where there were already dotfiles in the directory – the task will not throw ignore files there.

    Source is available on GitHub:

    http://github.com/vigetlabs/sake-tasks/tree/master

    p.

  5. Chris Kaukis says:

    Very cool Patrick! Thanks!

  6. annoymous says:

    You don’t need this program, git can track empty directories by putting a .gitignore in them. If you don’t want to track the contents of the directory, put a “*” in that .gitignore. Make sure to remove the subdir lines from your top level .gitignore, and check in the new ignore files in the subdir.

Leave a Reply

Collaborate.
Enable.
Succeed.

Contact

(888) 331-8520
4210 Beau James Court
Winter Park, Florida 32792 RSS Feed

Search

Popular Articles

Recent Articles