mojombo/grit — pull request #158 by jonjohn (2013-02-23 19:45:29+00:00)
Title: Index - remove empty directories

I ran into a problem while using grit where I couldn't get it to create a commit that deletes a directory. It seemed to me that using index.read_tree(repo.commits.first.id) as a starting point I should be able to use index.delete(file) to remove all files from a directory and that the directory would also be removed. Instead what was happening was that the commit would include an empty directory (tree) which isn't even valid. Even using index.delete(directory_name) specifically wouldn't remove the directory (due to the add method pruning trailing '/' and then the write_tree method later expecting a trailing '/' for directory removals).

I changed the Index class so that the write_tree method recursively merges the pre-existing tree (from read_tree) with whatever changes have been made using the add and delete methods in such a way that empty directories are pruned from the resulting tree hierarchy used for the commit. I also added a test to test_rubygit_index to test the functionality.