Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/main/java/org/apache/commons/codec/digest/GitIdentifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +54,12 @@
* </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>
*
Expand Down Expand Up @@ -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.
Comment on lines +227 to +231

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think the code is self-explaining, so this comment could be dropped. LLMs have the tendency of writing the history of their life as comments, but IMHO most of them are not necessary.

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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <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,
Expand Down
Loading