Copilot commented on code in PR #443:
URL: https://github.com/apache/commons-codec/pull/443#discussion_r3996759697


##########
src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java:
##########
@@ -100,13 +101,22 @@ static class DirectoryEntry implements 
Comparable<DirectoryEntry> {
             }
             this.name = name;
             this.type = Objects.requireNonNull(type, "type");
-            this.sortKey = type == FileMode.DIRECTORY ? name + "/" : name;
+            this.sortKey = (type == FileMode.DIRECTORY ? name + "/" : 
name).getBytes(StandardCharsets.UTF_8);

Review Comment:
   `String.getBytes(StandardCharsets.UTF_8)` replaces malformed UTF-16 (for 
example, a lone surrogate) with the encoder's replacement bytes. Consequently, 
two distinct names can produce the same `sortKey`, `compareTo` returns 0, and 
the `TreeSet` in `TreeIdBuilder.get()` silently drops one entry. Reject 
malformed names (or otherwise enforce a one-to-one sort key) before relying on 
this comparator so every accepted entry is retained.



##########
src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java:
##########
@@ -57,8 +57,9 @@ public class GitIdentifiers {
      *   <li>the raw object id of the referenced blob or sub-tree.</li>
      * </ul>
      *
-     * <p>Entries are ordered by {@link #compareTo} using Git's tree-sort 
rule: directory names are compared as if they ended with {@code '/'}, so that 
{@code foo/}
-     * sorts after {@code foobar}.</p>
+     * <p>Entries are ordered by {@link #compareTo} using Git's tree-sort 
rule: names are compared as unsigned UTF-8 bytes, and directory names are 
compared as if
+     * they ended with {@code '/'}, so that {@code foo/} sorts after {@code 
foobar}. Comparing the UTF-8 bytes rather than the Java {@link String} is what 
keeps
+     * the order Git's for names outside the Basic Multilingual Plane, whose 
UTF-16 code units do not sort in code point order.</p>

Review Comment:
   The phrase `the order Git's for names` is grammatically malformed; rephrase 
it so the Javadoc clearly explains that the byte comparison preserves Git's 
ordering.



-- 
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]

Reply via email to