summary refs log tree commit diff stats
path: root/tools/deps.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tools/deps.nim')
-rw-r--r--tools/deps.nim36
1 files changed, 29 insertions, 7 deletions
diff --git a/tools/deps.nim b/tools/deps.nim
index 888237761..e43f7a2b4 100644
--- a/tools/deps.nim
+++ b/tools/deps.nim
@@ -1,22 +1,44 @@
-import os, uri, strformat
+import std/[os, uri, strformat, strutils]
+import std/private/gitutils
+
+when defined(nimPreviewSlimSystem):
+  import std/assertions
 
 proc exec(cmd: string) =
   echo "deps.cmd: " & cmd
   let status = execShellCmd(cmd)
   doAssert status == 0, cmd
 
-const commitHead* = "HEAD"
+proc execRetry(cmd: string) =
+  let ok = retryCall(call = block:
+    let status = execShellCmd(cmd)
+    let result = status == 0
+    if not result:
+      echo fmt"failed command: '{cmd}', status: {status}"
+    result)
+  doAssert ok, cmd
 
-proc cloneDependency*(destDirBase: string, url: string, commit = commitHead, appendRepoName = true) =
+proc cloneDependency*(destDirBase: string, url: string, commit = commitHead,
+                      appendRepoName = true, allowBundled = false) =
   let destDirBase = destDirBase.absolutePath
   let p = url.parseUri.path
   let name = p.splitFile.name
   var destDir = destDirBase
   if appendRepoName: destDir = destDir / name
-  let destDir2 = destDir.quoteShell
+  let quotedDestDir = destDir.quoteShell
   if not dirExists(destDir):
     # note: old code used `destDir / .git` but that wouldn't prevent git clone
     # from failing
-    exec fmt"git clone -q {url} {destDir2}"
-  exec fmt"git -C {destDir2} fetch -q"
-  exec fmt"git -C {destDir2} checkout -q {commit}"
+    execRetry fmt"git clone -q {url} {quotedDestDir}"
+  if isGitRepo(destDir):
+    let oldDir = getCurrentDir()
+    setCurrentDir(destDir)
+    try:
+      execRetry "git fetch -q"
+      exec fmt"git checkout -q {commit}"
+    finally:
+      setCurrentDir(oldDir)
+  elif allowBundled:
+    discard "this dependency was bundled with Nim, don't do anything"
+  else:
+    quit "FAILURE: " & destdir & " already exists but is not a git repo"