This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 720bcfac588d245ba9c8667b5aa1c5f4e55a0c2e
Author: simpleqt <[email protected]>
AuthorDate: Sun Nov 15 00:01:40 2026 +0800

    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 +++++++++++++++++-----
 .../commons/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 d366f89f..634bdca6 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.io.InputStream;
 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 @@ import java.util.function.Supplier;
  * </p>
  *
  * <p>
+ * 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}.
+ * </p>
+ *
+ * <p>
  * This class is immutable and thread-safe. However, the {@link MessageDigest} 
instances passed to it generally won't be.
  * </p>
  *
@@ -211,18 +223,30 @@ public class GitIdentifiers {
          */
         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 0f5ba67b..de30da06 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 @@ class GitIdentifiersTest {
                 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.
+     *
+     * <p>
+     * {@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}.
+     * </p>
+     */
+    @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,

Reply via email to