From The Blog

Rails, Git, and Empty Directories

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...

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?

Other Posts That Might Interest You

  1. Windows Gotchas
  2. SSH into EC2 without Mindterm (RightScale)
  3. Get JRuby onto the Rails on Mac OS X

Tags: 

  • Jonny Dee
    've been facing the issue with empty directories, too. The problem with using placeholder files is that you need to create them, and delete them, if they are not necessary anymore (because later on there were added sub-directories or files. With big source trees managing these placeholder files can be cumbersome and error prone.

    This is why I decided to write an open source tool which can manage the creation/deletion of such placeholder files automatically. It is licensed under GPLv3 and it is written for .NET platform and runs under Mono (.NET for Linux) and Windows.

    Just have a look at: http://code.google.com/p/markemptydirs

    Best regards and have fun with it :)

    Jonny Dee
  • annoymous
    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.
  • Very cool Patrick! Thanks!
  • 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.
  • 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.
  • 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?
  • FWIW, I believe Rails Edge has support to automatically create directories when needed. This problem should go away soon.
blog comments powered by Disqus