ppkarwasz commented on PR #427: URL: https://github.com/apache/commons-codec/pull/427#issuecomment-4137714904
> Should all this git related code be in a new GitDigest class instead? `gitBlob` fits naturally in `DigestUtils`: it follows the same pattern as the existing digest methods (wrap content with a header, hash it). `gitTree` is more specialised: it only accepts a directory and recursively computes hashes for the entire tree, which is arguably outside `DigestUtils`'s scope. I'm open to moving both to a new `GitDigest` class. > Curious: isn't all this in jgit? JGit does provide the building blocks via [`ObjectInserter`](https://javadoc.io/doc/org.eclipse.jgit/org.eclipse.jgit/latest/org.eclipse.jgit/org/eclipse/jgit/lib/ObjectInserter.html), but it has two significant limitations that make it unsuitable here: 1. `ObjectInserter` is hardcoded to SHA-1. 2. The caller is responsible for walking the directory, sorting entries, and building each sub-tree formatter correctly. The proposed `gitTree` method handles all of that automatically. For reference, here is the equivalent JGit code for a two-file tree: ```java final byte[] aBytes = ...; // a.txt final byte[] bBytes = ...; // nested/b.txt try (ObjectInserter inserter = new ObjectInserter.Formatter()) { final ObjectId aBlob = inserter.idFor(OBJ_BLOB, aBytes); final ObjectId bBlob = inserter.idFor(OBJ_BLOB, bBytes); final TreeFormatter nestedTreeFormatter = new TreeFormatter(); nestedTreeFormatter.append("b.txt", FileMode.REGULAR_FILE, bBlob); final ObjectId nestedTree = inserter.idFor(nestedTreeFormatter); final TreeFormatter rootTreeFormatter = new TreeFormatter(); rootTreeFormatter.append("a.txt", FileMode.REGULAR_FILE, aBlob); rootTreeFormatter.append("nested", FileMode.TREE, nestedTree); return inserter.idFor(rootTreeFormatter).name(); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
