Navigation

ADS specializes in using Ruby on Rails to build advanced, scalable, database-backed web sites for organizations of all sizes. Find out more at our website.

Atlantic Dominion Solutions

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?

Share this post

Random Posts

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

5 Responses to “Rails, Git, and Empty Directories”

On May 11th, 2008 at 7:58 pm bryanl said:

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

On May 11th, 2008 at 8:23 pm Randal L. Schwartz said:

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?

On May 11th, 2008 at 8:58 pm Chris Kaukis said:

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.

On May 14th, 2008 at 7:41 am Patrick Reagan said:

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.

On May 14th, 2008 at 11:31 am Chris Kaukis said:

Very cool Patrick! Thanks!

Leave a Reply