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