Monday, November 3, 2014

Git and Empty Directories

Hello people!

I am not much of a blogger, I'm just getting started with it! But I wanted to share my learning experience  with the world and create my first opensource project!

How to Stage empty directories?


Anyone who works with git know the trouble I'm talking about. Git doesn't index directories (folder for windows users). It tracks content and directories are not exactly contents. Some might say why should i track empty directories any ways! I don't argue with them if they don't need them. But for me using gits caused me a lot of clathpath errors. It took me a hour of google to see that git will never index my directories until i put something in it! So the answer to the above question is you can't!

How to Trick git in to Staging empty Directories?

You can't tell git to index directories. There is just no way to do that! The only solution is to put something you don't even care about inside the directories. Then add those files! Job done!

Talking about putting something in it, what should i put? Well,  here is a convention programmers came up with. Put .gitkeep file in it! Other files are also alternative such as README and .gitignore! README is fine but .gitignore is not a good option because it is actually checked by git, so the more u have the more it could cause lags. Therefore I am sticking with the .gitkeep file.

So, Here is the problem I have a couple of hundred directories that i am not sure if empty or not! i can't just put .gitkeep everywhere! Ow' crap! that is a trouble that has just the simplest solution. 

Put your programming skill to use! I wrote a small programm to put .gitkeep file in every directory starting from the root of the project, that way i don't have to be bugged about any of the troubles!


Here is the code for the solution, just copy, past, change the root path then compile and run! Or go to git hub, fork modify it to be better and create a pull request! (Consider it my first opensource project, i know that is dumb)!

package gitkeep;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Walker {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String rootFolder = "E:/Projects/Ongoing/Grails/AdeStyles/";

  File root = new File(rootFolder);

  System.out.println(root.isDirectory());
  walkTree(root);
  // root.listFiles();

 }

 public static void walkTree(File root) {
  if (root.isFile()) {
   return;
  } else if (root.isDirectory()) {
   // put an empty .keep file here
   File gitKeep = new File(root.getAbsolutePath()+"/.gitKeep");
   try {
    FileWriter w = new FileWriter(gitKeep);
    w.write("#This file is not ment to do anything!" 
                                         + " It is used to make empty directories"
             + " stay in the git repository so that classpath"
                                         + " error doesn' occur!");
    w.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println(root.getName());
   for(File file:root.listFiles()){
    walkTree(file);
   }
  } else {
   return;
  }
 }

}


Here, is a link to the github! amexboy/gitkeeper Please fork and make it better! And let us build a better world for our selves!

No comments:

Post a Comment