git.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. """
  2. Git SCM backend for Digress.
  3. """
  4. from subprocess import Popen, PIPE, STDOUT
  5. import re
  6. from digress.errors import SCMError
  7. GIT_BRANCH_EXPR = re.compile("[*] (.*)")
  8. def checkout(revision):
  9. """
  10. Checkout a revision from git.
  11. """
  12. proc = Popen([
  13. "git",
  14. "checkout",
  15. "-f",
  16. revision
  17. ], stdout=PIPE, stderr=STDOUT)
  18. output = proc.communicate()[0].strip()
  19. if proc.returncode != 0:
  20. raise SCMError("checkout error: %s" % output)
  21. def rev_parse(ref):
  22. proc = Popen([
  23. "git",
  24. "rev-parse",
  25. ref
  26. ], stdout=PIPE, stderr=STDOUT)
  27. output = proc.communicate()[0].strip()
  28. if proc.returncode != 0:
  29. raise SCMError("rev-parse error: %s" % output)
  30. return output
  31. def current_rev():
  32. """
  33. Get the current revision.
  34. """
  35. return rev_parse("HEAD")
  36. def current_branch():
  37. """
  38. Get the current branch.
  39. """
  40. proc = Popen([
  41. "git",
  42. "branch",
  43. "--no-color"
  44. ], stdout=PIPE, stderr=STDOUT)
  45. output = proc.communicate()[0].strip()
  46. if proc.returncode != 0:
  47. raise SCMError("branch error: %s" % output)
  48. branch_name = GIT_BRANCH_EXPR.findall(output)[0]
  49. return branch_name != "(no branch)" and branch_name or None
  50. def revisions(rev_a, rev_b):
  51. """
  52. Get a list of revisions from one to another.
  53. """
  54. proc = Popen([
  55. "git",
  56. "log",
  57. "--format=%H", ("%s...%s" % (rev_a, rev_b))
  58. ], stdout=PIPE, stderr=STDOUT)
  59. output = proc.communicate()[0].strip()
  60. if proc.returncode != 0:
  61. raise SCMError("log error: %s" % output)
  62. return output.split("\n")
  63. def stash():
  64. """
  65. Stash the repository.
  66. """
  67. proc = Popen([
  68. "git",
  69. "stash",
  70. "save",
  71. "--keep-index"
  72. ], stdout=PIPE, stderr=STDOUT)
  73. output = proc.communicate()[0].strip()
  74. if proc.returncode != 0:
  75. raise SCMError("stash error: %s" % output)
  76. def unstash():
  77. """
  78. Unstash the repository.
  79. """
  80. proc = Popen(["git", "stash", "pop"], stdout=PIPE, stderr=STDOUT)
  81. proc.communicate()
  82. def bisect(*args):
  83. """
  84. Perform a bisection.
  85. """
  86. proc = Popen((["git", "bisect"] + list(args)), stdout=PIPE, stderr=STDOUT)
  87. output = proc.communicate()[0]
  88. if proc.returncode != 0:
  89. raise SCMError("bisect error: %s" % output)
  90. return output
  91. def dirty():
  92. """
  93. Check if the working tree is dirty.
  94. """
  95. proc = Popen(["git", "status"], stdout=PIPE, stderr=STDOUT)
  96. output = proc.communicate()[0].strip()
  97. if proc.returncode != 0:
  98. raise SCMError("status error: %s" % output)
  99. if "modified:" in output:
  100. return True
  101. else:
  102. return False