Skip to content

Commit

Permalink
Install submodules and adds ignoreSubmodules flag (#1364)
Browse files Browse the repository at this point in the history
* Install submodules and adds `ignoreSubmodules` flag

* Update download.nim
  • Loading branch information
jmgomez authored Mar 11, 2025
1 parent bf28d04 commit 9ffc46b
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 125 deletions.
38 changes: 21 additions & 17 deletions src/nimblepkg/download.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,40 @@ proc updateSubmodules(dir: string) =
discard tryDoCmdEx(
&"git -C {dir} submodule update --init --recursive --depth 1")

proc doCheckout*(meth: DownloadMethod, downloadDir, branch: string) =
proc doCheckout*(meth: DownloadMethod, downloadDir, branch: string, options: Options) =
case meth
of DownloadMethod.git:
# Force is used here because local changes may appear straight after a clone
# has happened. Like in the case of git on Windows where it messes up the
# damn line endings.
discard tryDoCmdEx(&"git -C {downloadDir} checkout --force {branch}")
downloadDir.updateSubmodules
if not options.ignoreSubmodules:
downloadDir.updateSubmodules
of DownloadMethod.hg:
discard tryDoCmdEx(&"hg --cwd {downloadDir} checkout {branch}")

proc doClone(meth: DownloadMethod, url, downloadDir: string, branch = "",
onlyTip = true) =
onlyTip = true, options: Options) =
case meth
of DownloadMethod.git:
let
submoduleFlag = if not options.ignoreSubmodules: " --recurse-submodules" else: ""
depthArg = if onlyTip: "--depth 1" else: ""
branchArg = if branch == "": "" else: &"-b {branch}"
discard tryDoCmdEx(
"git clone --config core.autocrlf=false --config core.eol=lf --recursive " &
&"{depthArg} {branchArg} {url} {downloadDir}")
"git clone --config core.autocrlf=false --config core.eol=lf " &
&"{submoduleFlag} {depthArg} {branchArg} {url} {downloadDir}")
of DownloadMethod.hg:
let
tipArg = if onlyTip: "-r tip " else: ""
branchArg = if branch == "": "" else: &"-b {branch}"
discard tryDoCmdEx(&"hg clone {tipArg} {branchArg} {url} {downloadDir}")

proc gitFetchTags*(repoDir: string, downloadMethod: DownloadMethod) =
proc gitFetchTags*(repoDir: string, downloadMethod: DownloadMethod, options: Options) =
case downloadMethod:
of DownloadMethod.git:
tryDoCmdEx(&"git -C {repoDir} fetch --tags")
let submoduleFlag = if not options.ignoreSubmodules: " --recurse-submodules" else: ""
tryDoCmdEx(&"git -C {repoDir} fetch --tags" & submoduleFlag)
of DownloadMethod.hg:
assert false, "hg not supported"

Expand Down Expand Up @@ -144,7 +147,7 @@ proc getUrlData*(url: string): (string, Table[string, string]) =

proc cloneSpecificRevision(downloadMethod: DownloadMethod,
url, downloadDir: string,
vcsRevision: Sha1Hash) =
vcsRevision: Sha1Hash, options: Options) =
assert vcsRevision != notSetSha1Hash

display("Cloning", "revision: " & $vcsRevision, priority = MediumPriority)
Expand All @@ -158,7 +161,8 @@ proc cloneSpecificRevision(downloadMethod: DownloadMethod,
discard tryDoCmdEx(
&"git -C {downloadDir} fetch --depth 1 origin {vcsRevision}")
discard tryDoCmdEx(&"git -C {downloadDir} reset --hard FETCH_HEAD")
downloadDir.updateSubmodules
if not options.ignoreSubmodules:
downloadDir.updateSubmodules
of DownloadMethod.hg:
discard tryDoCmdEx(&"hg clone {url} -r {vcsRevision}")

Expand Down Expand Up @@ -372,7 +376,7 @@ proc doDownload(url, downloadDir: string, verRange: VersionRange,
if downloadTarball(url, options):
discard doDownloadTarball(url, downloadDir, $vcsRevision, false)
else:
cloneSpecificRevision(downMethod, url, downloadDir, vcsRevision)
cloneSpecificRevision(downMethod, url, downloadDir, vcsRevision, options)
result.vcsRevision = vcsRevision
elif verRange.kind == verSpecial:
# We want a specific commit/branch/tag here.
Expand All @@ -382,7 +386,7 @@ proc doDownload(url, downloadDir: string, verRange: VersionRange,
result.vcsRevision = doDownloadTarball(url, downloadDir, "HEAD", true)
else:
doClone(downMethod, url, downloadDir,
onlyTip = not options.forceFullClone)
onlyTip = not options.forceFullClone, options = options)
else:
assert ($verRange.spe)[0] == '#',
"The special version must start with '#'."
Expand All @@ -392,10 +396,10 @@ proc doDownload(url, downloadDir: string, verRange: VersionRange,
url, downloadDir, specialVersion, true)
else:
# Grab the full repo.
doClone(downMethod, url, downloadDir, onlyTip = false)
doClone(downMethod, url, downloadDir, onlyTip = false, options = options)
# Then perform a checkout operation to get the specified branch/commit.
# `spe` starts with '#', trim it.
doCheckout(downMethod, downloadDir, specialVersion)
doCheckout(downMethod, downloadDir, specialVersion, options = options)
result.version = verRange.spe
else:
case downMethod
Expand All @@ -415,26 +419,26 @@ proc doDownload(url, downloadDir: string, verRange: VersionRange,
display("Cloning", "latest tagged version: " & latest.tag,
priority = MediumPriority)
doClone(downMethod, url, downloadDir, latest.tag,
onlyTip = not options.forceFullClone)
onlyTip = not options.forceFullClone, options = options)
else:
display("Warning:", "The package has no tagged releases, downloading HEAD instead.", Warning,
priority = HighPriority)
if downloadTarball(url, options):
result.vcsRevision = doDownloadTarball(url, downloadDir, "HEAD", true)
else:
# If no commits have been tagged on the repo we just clone HEAD.
doClone(downMethod, url, downloadDir, onlyTip = not options.forceFullClone) # Grab HEAD.
doClone(downMethod, url, downloadDir, onlyTip = not options.forceFullClone, options = options) # Grab HEAD.
of DownloadMethod.hg:
doClone(downMethod, url, downloadDir,
onlyTip = not options.forceFullClone)
onlyTip = not options.forceFullClone, options = options)
result.version = getHeadName(downMethod)
let versions = getTagsList(downloadDir, downMethod).getVersionList()

if versions.len > 0:
getLatestByTag:
display("Switching", "to latest tagged version: " & latest.tag,
priority = MediumPriority)
doCheckout(downMethod, downloadDir, latest.tag)
doCheckout(downMethod, downloadDir, latest.tag, options = options)
else:
display("Warning:", "The package has no tagged releases, downloading HEAD instead.", Warning,
priority = HighPriority)
Expand Down
6 changes: 3 additions & 3 deletions src/nimblepkg/nimblesat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ proc getTaggedVersions*(repoDir, pkgName: string, options: Options): Option[Tagg
displayWarning(&"Error reading tagged versions: {e.msg}", HighPriority)
return none(TaggedPackageVersions)
else:
none(TaggedPackageVersions)
return none(TaggedPackageVersions)

proc saveTaggedVersions*(repoDir, pkgName: string, taggedVersions: TaggedPackageVersions, options: Options) =
var file: string
Expand Down Expand Up @@ -552,7 +552,7 @@ proc getPackageMinimalVersionsFromRepo*(repoDir: string, pkg: PkgTuple, version:
copyDir(repoDir, tempDir)
var tags = initOrderedTable[Version, string]()
try:
gitFetchTags(tempDir, downloadMethod)
gitFetchTags(tempDir, downloadMethod, options)
tags = getTagsList(tempDir, downloadMethod).getVersionList()
except CatchableError as e:
displayWarning(&"Error fetching tags for {name}: {e.msg}", HighPriority)
Expand All @@ -576,7 +576,7 @@ proc getPackageMinimalVersionsFromRepo*(repoDir: string, pkg: PkgTuple, version:
displayInfo(&"Ignoring {name}:{tagVersion} because out of range {pkg[1]}")
break

doCheckout(downloadMethod, tempDir, tag)
doCheckout(downloadMethod, tempDir, tag, options)
let nimbleFile = findNimbleFile(tempDir, true, options)
if options.useDeclarativeParser:
result.addUnique getMinimalInfo(nimbleFile, name, options)
Expand Down
4 changes: 4 additions & 0 deletions src/nimblepkg/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type
maxTaggedVersions*: int # Maximum number of tags to check for a package when discovering versions in a local repo
useDeclarativeParser*: bool # Whether to use the declarative parser for parsing nimble files (only when solver is SAT)
features*: seq[string] # Features to be activated. Only used when using the declarative parser
ignoreSubmodules*: bool # Whether to ignore submodules when cloning a repository

ActionType* = enum
actionNil, actionRefresh, actionInit, actionDump, actionPublish, actionUpgrade
Expand Down Expand Up @@ -280,6 +281,7 @@ Nimble Options:
--maximumTaggedVersions Maximum number of tags to check for a package when discovering versions for the SAT solver. 0 means all.
--parser:declarative|nimvm Use the declarative parser or the nimvm parser (default).
--features Activate features. Only used when using the declarative parser.
--ignoreSubmodules Ignore submodules when cloning a repository.
For more information read the GitHub readme:
https://github.com/nim-lang/nimble#readme
"""
Expand Down Expand Up @@ -684,6 +686,8 @@ proc parseFlag*(flag, val: string, result: var Options, kind = cmdLongOption) =
raise nimbleError(&"{val} is not a valid value")
of "features":
result.features = val.split(";").mapIt(it.strip)
of "ignoresubmodules":
result.ignoreSubmodules = true
else: isGlobalFlag = false

var wasFlagHandled = true
Expand Down
3 changes: 2 additions & 1 deletion tests/tissues.nim
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,5 @@ suite "issues":
let message = "compiling nim package using " & nimBin
check exitCode == QuitSuccess
check output.contains(message)
removeDir("testDir-1251")
removeDir("testDir-1251")

Loading

0 comments on commit 9ffc46b

Please sign in to comment.