javagitjgitgit-ls-files

How do I get the list of SHA values of all the files in a folder of my git repo using JGit library


I believe that I can get the SHA values of all the files in a folder through command line by using git ls-files -s <file name> command. I want to know if there is an equivalent way in JGit Java library?

PS: I don't want to calculate SHA of files myself and there can be many files inside the folder so I need to get SHAs which are already present in git objects. Also, I can't do Runtime.getRuntime().exec("git ls-files -s") as the host machine will not identify git itself.


Solution

  • To see the object IDs of the index, you can use the DirCache as it is called in JGit. It allows to iterate over its entries.

    The following example prints the object ID of each file in the index:

    DirCache index = repository.lockDirCache();
    for (int i = 0; i < index.getEntryCount(); i++) {
      DirCacheEntry entry = index.getEntry(i);
      System.out.println(entry.getObjectId().getName() + " " + entry.getPathString());
    }