简体   繁体   中英

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.

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM