gitutils.nim 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ##[
  2. internal API for now, API subject to change
  3. ]##
  4. # xxx move other git utilities here; candidate for stdlib.
  5. import std/[os, osproc, strutils, tempfiles]
  6. when defined(nimPreviewSlimSystem):
  7. import std/[assertions, syncio]
  8. const commitHead* = "HEAD"
  9. template retryCall*(maxRetry = 3, backoffDuration = 1.0, call: untyped): bool =
  10. ## Retry `call` up to `maxRetry` times with exponential backoff and initial
  11. ## duraton of `backoffDuration` seconds.
  12. ## This is in particular useful for network commands that can fail.
  13. runnableExamples:
  14. doAssert not retryCall(maxRetry = 2, backoffDuration = 0.1, false)
  15. var i = 0
  16. doAssert: retryCall(maxRetry = 3, backoffDuration = 0.1, (i.inc; i >= 3))
  17. doAssert retryCall(call = true)
  18. var result = false
  19. var t = backoffDuration
  20. for i in 0..<maxRetry:
  21. if call:
  22. result = true
  23. break
  24. if i == maxRetry - 1: break
  25. sleep(int(t * 1000))
  26. t = t * 2 # exponential backoff
  27. result
  28. proc isGitRepo*(dir: string): bool =
  29. ## This command is used to get the relative path to the root of the repository.
  30. ## Using this, we can verify whether a folder is a git repository by checking
  31. ## whether the command success and if the output is empty.
  32. let (output, status) = execCmdEx("git rev-parse --show-cdup", workingDir = dir)
  33. # On Windows there will be a trailing newline on success, remove it.
  34. # The value of a successful call typically won't have a whitespace (it's
  35. # usually a series of ../), so we know that it's safe to unconditionally
  36. # remove trailing whitespaces from the result.
  37. result = status == 0 and output.strip() == ""
  38. proc diffFiles*(path1, path2: string): tuple[output: string, same: bool] =
  39. ## Returns a human readable diff of files `path1`, `path2`, the exact form of
  40. ## which is implementation defined.
  41. # This could be customized, e.g. non-git diff with `diff -uNdr`, or with
  42. # git diff options (e.g. --color-moved, --word-diff).
  43. # in general, `git diff` has more options than `diff`.
  44. var status = 0
  45. (result.output, status) = execCmdEx("git diff --no-index $1 $2" % [path1.quoteShell, path2.quoteShell])
  46. doAssert (status == 0) or (status == 1)
  47. result.same = status == 0
  48. proc diffStrings*(a, b: string): tuple[output: string, same: bool] =
  49. ## Returns a human readable diff of `a`, `b`, the exact form of which is
  50. ## implementation defined.
  51. ## See also `experimental.diff`.
  52. runnableExamples:
  53. let a = "ok1\nok2\nok3\n"
  54. let b = "ok1\nok2 alt\nok3\nok4\n"
  55. let (c, same) = diffStrings(a, b)
  56. doAssert not same
  57. let (c2, same2) = diffStrings(a, a)
  58. doAssert same2
  59. runnableExamples("-r:off"):
  60. let a = "ok1\nok2\nok3\n"
  61. let b = "ok1\nok2 alt\nok3\nok4\n"
  62. echo diffStrings(a, b).output
  63. template tmpFileImpl(prefix, str): auto =
  64. let path = genTempPath(prefix, "")
  65. writeFile(path, str)
  66. path
  67. let patha = tmpFileImpl("diffStrings_a_", a)
  68. let pathb = tmpFileImpl("diffStrings_b_", b)
  69. defer:
  70. removeFile(patha)
  71. removeFile(pathb)
  72. result = diffFiles(patha, pathb)