Skip to content
Open
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
15 changes: 12 additions & 3 deletions src/nimblepkg/checksums.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) Dominik Picheta. All rights reserved.
# BSD License. Look at license.txt for more info.

import os, strformat, algorithm
import os, strformat, strutils, algorithm
import common, version, sha1hashes, vcstools, paths, cli
import pkg/checksums/sha1

Expand Down Expand Up @@ -57,10 +57,12 @@ proc updateSha1Checksum(checksum: var Sha1State, fileName, filePath: string) =
if bytesRead == 0: break
checksum.update(buffer.toOpenArray(0, bytesRead - 1))

proc calculateDirSha1Checksum*(dir: string): Sha1Hash =
proc calculateDirSha1Checksum*(dir: string, skipDirs: seq[string] = @[]): Sha1Hash =
## Recursively calculates the sha1 checksum of the contents of the directory
## `dir` and its subdirectories.
##
## Files inside directories listed in `skipDirs` are excluded from the checksum.
##
## Raises a `NimbleError` if:
## - the external command for getting the package file list fails.
## - the directory does not exist.
Expand All @@ -69,5 +71,12 @@ proc calculateDirSha1Checksum*(dir: string): Sha1Hash =
packageFiles.sort
var checksum = newSha1State()
for file in packageFiles:
updateSha1Checksum(checksum, file, dir / file)
var shouldSkip = false
for skipDir in skipDirs:
let normalizedSkipDir = skipDir.strip(leading = false, trailing = true, chars = {'/'})
if file == normalizedSkipDir or file.startsWith(normalizedSkipDir & "/"):
shouldSkip = true
break
if not shouldSkip:
updateSha1Checksum(checksum, file, dir / file)
result = initSha1Hash($SecureHash(checksum.finalize()))
2 changes: 1 addition & 1 deletion src/nimblepkg/packageparser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ proc readPackageInfo(pkgInfo: var PackageInfo, nf: NimbleFile, options: Options,
if not fileDir.startsWith(options.getPkgsDir()):
# If the `.nimble` file is not in the installation directory we have to get
# some of the package meta data from its directory.
pkgInfo.basicInfo.checksum = calculateDirSha1Checksum(fileDir)
pkgInfo.basicInfo.checksum = calculateDirSha1Checksum(fileDir, pkgInfo.skipDirs)
# By default specialVersion is the same as version.
pkgInfo.metaData.specialVersions.incl pkgInfo.basicInfo.version
# If the `fileDir` is a VCS repository we can get some of the package meta
Expand Down
4 changes: 2 additions & 2 deletions tests/lockfile-subdep/nimble.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"downloadMethod": "git",
"dependencies": [],
"checksums": {
"sha1": "02cf5882b51550d7faddbce7eedac35c94dea729"
"sha1": "a27b39aa20dec862fa22c3a33137803cacbe2bbc"
}
},
"x11": {
Expand All @@ -33,7 +33,7 @@
"x11"
],
"checksums": {
"sha1": "dd020f93fcf762e28d3d1462a7bba0c91a5a36dd"
"sha1": "59e9de65fac3d8123b5f1b677b3fa18beb02d855"
}
}
}
Expand Down
Loading