Skip to content

Commit

Permalink
Add a flavor of PathRef using relative path to calculate digest
Browse files Browse the repository at this point in the history
  • Loading branch information
CircuitCoder committed Oct 31, 2022
1 parent 50b43db commit 37cafd7
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion main/api/src/mill/api/PathRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ object PathRef {
* @return
*/
def apply(path: os.Path, quick: Boolean = false): PathRef = {
inner(path, quick, _.toString().getBytes())
}

/**
* Create a [[PathRef]] by recursively digesting the content of a given `path`, using path relativized to a base path.
* @param base The base path.
* @param path The digested path.
* @param quick If `true` the digest is only based to some file attributes (like mtime and size).
* If `false` the digest is created of the files content.
* @return
*/
def rel(base: os.Path, path: os.RelPath, quick: Boolean = false): PathRef = {
inner(path.resolveFrom(base), quick, _.relativeTo(base).toString().getBytes())
}

private def inner(path: os.Path, quick: Boolean, pathDigest: os.Path => Array[Byte]): PathRef = {
val sig = {
val isPosix = path.wrapped.getFileSystem.supportedFileAttributeViews().contains("posix")
val digest = MessageDigest.getInstance("MD5")
Expand All @@ -40,7 +56,7 @@ object PathRef {
(path, attrs) <-
os.walk.attrs(path, includeTarget = true, followLinks = true).sortBy(_._1.toString)
) {
digest.update(path.toString.getBytes)
digest.update(pathDigest(path))
if (!attrs.isDir) {
if (isPosix) {
updateWithInt(os.perms(path, followLinks = false).value)
Expand Down

0 comments on commit 37cafd7

Please sign in to comment.