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..f323f75d29 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,26 @@ 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); + if (view == null) { + view = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); + } + final BasicFileAttributes 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; + final boolean executable = attributes instanceof PosixFileAttributes && + ((PosixFileAttributes) attributes).permissions().contains(PosixFilePermission.OWNER_EXECUTE); + return executable ? 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,