gitutils.nim 2.9 KB

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