Skip to content

Commit

Permalink
FileOps: support symlinked files or directories
Browse files Browse the repository at this point in the history
Otherwise, the formatter won't be able to use a symlinked configuration
file.
  • Loading branch information
kitbellew committed Jan 14, 2022
1 parent d0c11e9 commit 57441d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final class AbsoluteFile(val path: Path) extends AnyVal {

@inline def isDirectory: Boolean = FileOps.isDirectory(path)
@inline def isRegularFile: Boolean = FileOps.isRegularFile(path)
@inline def isRegularFileNoLinks: Boolean = FileOps.isRegularFileNoLinks(path)
@inline def attributes: BasicFileAttributes = FileOps.getAttributes(path)

@inline def listFiles: Seq[AbsoluteFile] = join(FileOps.listFiles(path))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,36 @@ import org.scalafmt.CompatCollections.JavaConverters._

object FileOps {

@inline
def getLastModifiedMsec(file: Path): Long =
Files.getLastModifiedTime(file, LinkOption.NOFOLLOW_LINKS).toMillis
def getLastModifiedMsec(file: Path): Long = {
val attributes = getAttributesNoLinks(file)
val mtime = attributes.lastModifiedTime().toMillis
if (attributes.isSymbolicLink)
math.max(mtime, Files.getLastModifiedTime(file).toMillis)
else mtime
}

@inline
def isDirectory(file: Path): Boolean =
Files.isDirectory(file)

@inline
def isDirectoryNoLinks(file: Path): Boolean =
Files.isDirectory(file, LinkOption.NOFOLLOW_LINKS)

@inline
def isRegularFile(file: Path): Boolean =
Files.isRegularFile(file)

@inline
def isRegularFileNoLinks(file: Path): Boolean =
Files.isRegularFile(file, LinkOption.NOFOLLOW_LINKS)

@inline
def getAttributes(file: Path): BasicFileAttributes =
Files.readAttributes(file, classOf[BasicFileAttributes])

@inline
def getAttributesNoLinks(file: Path): BasicFileAttributes =
Files.readAttributes(
file,
classOf[BasicFileAttributes],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private class GitOpsImpl(val workingDirectory: AbsoluteFile) extends GitOps {

override def lsTree(dir: AbsoluteFile*): Seq[AbsoluteFile] = {
val cmd = Seq("git", "ls-files", "--full-name") ++ dir.map(_.toString())
withRoot(exec(cmd)).filter(_.isRegularFile)
withRoot(exec(cmd)).filter(_.isRegularFileNoLinks)
}

override def rootDir: Option[AbsoluteFile] = tryRoot.toOption
Expand Down

0 comments on commit 57441d9

Please sign in to comment.