From 8e8a5d2be59555227bafbebb73019151eb16551e Mon Sep 17 00:00:00 2001 From: simpleqt <2015900818@qq.com> Date: Sun, 15 Nov 2026 00:01:40 +0800 Subject: [PATCH 1/2] GitIdentifiers: record files as regular without POSIX permissions Files.isExecutable(Path) returns true for every readable file on the default Windows file system, so treeId(Path) recorded all regular files with mode 100755 instead of 100644 and produced tree identifiers that differ from Git's. Git records the executable bit only where the file system exposes POSIX permissions; with its default core.fileMode=false on Windows it stores regular files as 100644. The executable bit is now used only when the file has a POSIX attribute view. On a POSIX file system the view is present and Files.isExecutable stays authoritative, so the behaviour there is unchanged. Add a regression test. Existing GitIdentifiersTest.testTreeIdPath and testTreeIdSortsSupplementaryPlaneNamesLikeGit now pass on Windows. Assisted-By: DeepSeek dsv41 --- .../commons/codec/digest/GitIdentifiers.java | 40 +++++++++++++++---- .../codec/digest/GitIdentifiersTest.java | 18 +++++++++ 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java b/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java index d366f89f6f..634bdca61e 100644 --- a/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java +++ b/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java @@ -23,7 +23,13 @@ import java.nio.charset.StandardCharsets; import java.nio.file.DirectoryStream; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributeView; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.PosixFileAttributes; +import java.nio.file.attribute.PosixFilePermission; import java.security.MessageDigest; import java.util.HashMap; import java.util.Map; @@ -48,6 +54,12 @@ *
* *+ * The executable bit of a file is recorded only when the file system exposes POSIX permissions. File systems without them, + * such as the default Windows file system, record every non-directory, non-symbolic-link entry as a regular file, matching + * Git's behavior with its default {@code core.fileMode=false}. + *
+ * + ** This class is immutable and thread-safe. However, the {@link MessageDigest} instances passed to it generally won't be. *
* @@ -211,18 +223,30 @@ public enum FileMode { */ SYMBOLIC_LINK(new byte[] { '1', '2', '0', '0', '0', '0' }); - private static FileMode get(final Path path) { - // Symbolic links first - if (Files.isSymbolicLink(path)) { + private static FileMode get(final Path path) throws IOException { + // Read one attribute set, preferring POSIX where the file system provides it. The basic view always exists and + // classifies links and directories; only POSIX permissions carry the owner-execute bit that Git records. Git + // records the executable bit only where the file system exposes POSIX permissions; elsewhere, for example with + // Git for Windows and its default core.fileMode=false, regular files are stored as 100644 even though Windows + // reports every readable file as executable. + BasicFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); + final BasicFileAttributes attributes; + if (view == null) { + view = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); + attributes = view.readAttributes(); + if (attributes.isSymbolicLink()) { + return SYMBOLIC_LINK; + } + return attributes.isDirectory() ? DIRECTORY : REGULAR; + } + attributes = view.readAttributes(); + if (attributes.isSymbolicLink()) { return SYMBOLIC_LINK; } - if (Files.isDirectory(path)) { + if (attributes.isDirectory()) { return DIRECTORY; } - if (Files.isExecutable(path)) { - return EXECUTABLE; - } - return REGULAR; + return ((PosixFileAttributes) attributes).permissions().contains(PosixFilePermission.OWNER_EXECUTE) ? EXECUTABLE : REGULAR; } /** diff --git a/src/test/java/org/apache/commons/codec/digest/GitIdentifiersTest.java b/src/test/java/org/apache/commons/codec/digest/GitIdentifiersTest.java index 0f5ba67b39..de30da0680 100644 --- a/src/test/java/org/apache/commons/codec/digest/GitIdentifiersTest.java +++ b/src/test/java/org/apache/commons/codec/digest/GitIdentifiersTest.java @@ -395,6 +395,24 @@ void testTreeIdPath() throws Exception { GitIdentifiers.treeId(DigestUtils.getSha1Digest(), resourcePath("DigestUtilsTest"))); } + /** + * A file must be recorded with mode {@code 100644} unless the file system reports a POSIX executable bit for it. + * + *+ * {@link Files#isExecutable(Path)} alone is not enough: Windows reports every readable file as executable, which would encode ordinary files as + * {@code 100755}. Git on Windows defaults to {@code core.fileMode=false} and therefore stores regular files as {@code 100644}. + *
+ */ + @Test + void testTreeIdPathRecordsFilesWithoutPosixExecutableBitAsRegular(final @TempDir Path tempDir) throws Exception { + final byte[] content = "x".getBytes(StandardCharsets.UTF_8); + Files.write(tempDir.resolve("file.txt"), content); + final MessageDigest md = DigestUtils.getSha1Digest(); + final GitIdentifiers.TreeIdBuilder builder = GitIdentifiers.treeIdBuilder(md); + builder.addFile(GitIdentifiers.FileMode.REGULAR, "file.txt", content); + assertArrayEquals(builder.get(), GitIdentifiers.treeId(md, tempDir)); + } + @ParameterizedTest @MethodSource("virtualTreeProvider") void testTreeIdPathUnix(final String algorithm, final byte[] helloId, final byte[] linkId, final byte[] linkTxtId, From a2ae570eba1dcd26079c2f2570a7908d7b84073b Mon Sep 17 00:00:00 2001 From: simpleqt <2015900818@qq.com> Date: Sat, 21 Nov 2026 20:44:13 +0800 Subject: [PATCH 2/2] GitIdentifiers: classify once from the file attributes Address the review nit on FileMode.get: read one BasicFileAttributeView (POSIX preferred, Basic fallback), classify symbolic link and directory once, and use an instanceof PosixFileAttributes check for the owner-execute bit. This removes the duplicated fallback classification. The executable bit is still only read where the file system exposes POSIX permissions; without a POSIX view the attributes are BasicFileAttributes and the file is REGULAR. --- .../apache/commons/codec/digest/GitIdentifiers.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java b/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java index 634bdca61e..f323f75d29 100644 --- a/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java +++ b/src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java @@ -230,23 +230,19 @@ private static FileMode get(final Path path) throws IOException { // Git for Windows and its default core.fileMode=false, regular files are stored as 100644 even though Windows // reports every readable file as executable. BasicFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); - final BasicFileAttributes attributes; if (view == null) { view = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); - attributes = view.readAttributes(); - if (attributes.isSymbolicLink()) { - return SYMBOLIC_LINK; - } - return attributes.isDirectory() ? DIRECTORY : REGULAR; } - attributes = view.readAttributes(); + final BasicFileAttributes attributes = view.readAttributes(); if (attributes.isSymbolicLink()) { return SYMBOLIC_LINK; } if (attributes.isDirectory()) { return DIRECTORY; } - return ((PosixFileAttributes) attributes).permissions().contains(PosixFilePermission.OWNER_EXECUTE) ? EXECUTABLE : REGULAR; + final boolean executable = attributes instanceof PosixFileAttributes && + ((PosixFileAttributes) attributes).permissions().contains(PosixFilePermission.OWNER_EXECUTE); + return executable ? EXECUTABLE : REGULAR; } /**