diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2021-01-28 22:51:12 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-29 07:51:12 +0100 |
commit | 478d15f7f4bb48a8442e54d8ab08aaade39e5270 (patch) | |
tree | 273d965edc5b69cf46c0c8b25adadd930e689417 /lib/std/private | |
parent | b0f38a63c4916be139f1a289264a5df68bcb4c07 (diff) | |
download | Nim-478d15f7f4bb48a8442e54d8ab08aaade39e5270.tar.gz |
improve code in categories.nim; add std/private/gitutils; fix flakyness in nim CI (cloneDependency in deps.nim) (#16856)
* improve code in categories.nim; gitutils; fix flakyness in deps.nim * cleanups
Diffstat (limited to 'lib/std/private')
-rw-r--r-- | lib/std/private/gitutils.nim | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/std/private/gitutils.nim b/lib/std/private/gitutils.nim new file mode 100644 index 000000000..bf5e7cb1f --- /dev/null +++ b/lib/std/private/gitutils.nim @@ -0,0 +1,40 @@ +##[ +internal API for now, API subject to change +]## + +# xxx move other git utilities here; candidate for stdlib. + +import std/[os, osproc, strutils] + +const commitHead* = "HEAD" + +template retryCall*(maxRetry = 3, backoffDuration = 1.0, call: untyped): bool = + ## Retry `call` up to `maxRetry` times with exponential backoff and initial + ## duraton of `backoffDuration` seconds. + ## This is in particular useful for network commands that can fail. + runnableExamples: + doAssert not retryCall(maxRetry = 2, backoffDuration = 0.1, false) + var i = 0 + doAssert: retryCall(maxRetry = 3, backoffDuration = 0.1, (i.inc; i >= 3)) + doAssert retryCall(call = true) + var result = false + var t = backoffDuration + for i in 0..<maxRetry: + if call: + result = true + break + if i == maxRetry - 1: break + sleep(int(t * 1000)) + t = t * 2 # exponential backoff + result + +proc isGitRepo*(dir: string): bool = + ## This command is used to get the relative path to the root of the repository. + ## Using this, we can verify whether a folder is a git repository by checking + ## whether the command success and if the output is empty. + let (output, status) = execCmdEx("git rev-parse --show-cdup", workingDir = dir) + # On Windows there will be a trailing newline on success, remove it. + # The value of a successful call typically won't have a whitespace (it's + # usually a series of ../), so we know that it's safe to unconditionally + # remove trailing whitespaces from the result. + result = status == 0 and output.strip() == "" |