-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit_manager.py
205 lines (184 loc) · 7.16 KB
/
git_manager.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import re
import subprocess
from cudatext import *
LOG = False
MY_DECOR_COLOR = 0xFF
DIFF_TAG = app_proc(PROC_GET_UNIQUE_TAG, '')
class GitManager:
def __init__(self):
self.git = 'git'
self.prefix = ''
self.filename = ''
self.last_error = ''
def run_git(self, args, silence_errors=False):
try:
self.last_error = ''
cmd = [self.git] + args
cwd = self.getcwd()
my_env = os.environ.copy()
# removing GIT environment vars if any, they are interfering with our git calls
my_env = { k:v for k,v in my_env.items() if not k.upper().startswith('GIT_') }
if os.name=='nt':
# make sure console does not come up
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
p = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd,
startupinfo=startupinfo,
env=my_env)
else:
my_env["PATH"] = "/usr/local/bin:/usr/bin:" + my_env["PATH"]
my_env["LANG"] = "en_US"
p = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd,
env=my_env)
#p.wait() # makes deadlock if process gives lot of data
stdoutdata, stderrdata = p.communicate()
out_text = stdoutdata.decode('utf-8', errors='replace')
error_text = stderrdata.decode('utf-8', errors='replace') if not silence_errors else ''
# don't always show error_text, it may be normal message for 'push' action
if (
'fatal: ' in error_text or
'error: ' in error_text
):
print("NOTE: Git Status: ", error_text)
self.last_error = error_text
''' #debug
if stdoutdata:
print('Git for:', repr(args), ', gets:', stdoutdata)
else:
print('Git fails:', repr(args))
'''
except Exception as e:
raise Exception('{}, command: {}'.format(e, ' '.join(cmd)))
return (p.returncode, out_text)
def getcwd(self):
f = self.filename
cwd = None
if f:
cwd = os.path.dirname(f)
return cwd
def branch_ex(self):
"""
Returns tuple:
- str: branch name
- boolean: detached or not.
"""
(exit_code, output) = self.run_git(["status", "-u", "no"], silence_errors=True)
if exit_code != 0:
return (None, None)
m = re.search(r"^On branch\s(.*?)\n",output)
if m:
return (m.group(1), False)
m = re.search(r"^HEAD detached at\s(.*?)\n",output)
if m:
return (m.group(1), True)
m = re.search(r"^HEAD detached from\s(.*?)\n",output)
if m:
s = m.group(1)
# find commit hash
(exit_code, output) = self.run_git(["rev-parse", '--short', 'HEAD'], silence_errors=True)
if exit_code == 0 and output:
s = output.strip()
return (s, True)
return (None, None)
def branch(self):
return self.branch_ex()[0]
def is_dirty(self):
(exit_code, output) = self.run_git(["diff-index", "--quiet", "HEAD"], silence_errors=True)
return exit_code == 1
def commit_count(self):
(exit_code, output) = self.run_git(["rev-list", "--count", "HEAD"], silence_errors=True)
if exit_code != 0:
return 0
return int(output)
### This code shows dirty state when we have untacked files, bad.
#def is_dirty(self):
# (exit_code, output) = self.run_git(["status", "-s"])
# return bool(output)
def unpushed_info__old(self, branch):
a, b = 0, 0
if branch:
exit_code, output = self.run_git(["branch", "-v"])
if output:
m = re.search(r"\* .*?\[behind ([0-9])+\]", output, flags=re.MULTILINE)
if m:
a = int(m.group(1))
m = re.search(r"\* .*?\[ahead ([0-9])+\]", output, flags=re.MULTILINE)
if m:
b = int(m.group(1))
return (a, b)
def unpushed_info(self, branch):
if branch:
(exit_code, output) = self.run_git(
['rev-list', '--left-right', '--count', 'origin/'+branch+'...'+branch],
silence_errors=True
)
m = re.search(r"(\d+)\s+(\d+)", output)
if m:
return (int(m.group(1)), int(m.group(2)))
else:
return (0,0)
return (0,0)
def diff(self, filename_):
if filename_ != ed.get_filename():
return
exit_code, output = self.run_git(["diff", "-U0", filename_])
if exit_code != 0:
return ''
ed.decor(DECOR_DELETE_BY_TAG, tag=DIFF_TAG)
parts = re.findall(r"@@ \-(.*) @@", output)
lines_ = []
for part in parts:
lines = part.split(' +', maxsplit=1)
line = lines[1]
parts_ = line.split(',', maxsplit=1)
if len(parts_) == 2 and isinstance(parts_, list):
lines_.append(parts_)
else:
lines_.append(line)
if lines_:
for line_ in lines_:
if len(line_) == 2 and isinstance(line_, list):
begin_ = int(line_[0]) - 1
end_ = int(line_[0]) + int(line_[1]) - 1
if begin_ == end_:
ed.decor(DECOR_SET, line=begin_, tag=DIFF_TAG, text='', color=MY_DECOR_COLOR)
else:
for l in range(begin_, end_):
ed.decor(DECOR_SET, line=l, tag=DIFF_TAG, text='', color=MY_DECOR_COLOR)
else:
line__ = int(line_) - 1
ed.decor(DECOR_SET, line=line__, tag=DIFF_TAG, text='', color=MY_DECOR_COLOR)
return lines_
def badge(self, filename):
if not filename:
return ""
if not os.path.isfile(filename):
return ""
if filename != ed.get_filename():
return
pass; LOG and print("getting badge:", filename)
self.filename = filename
self.diff(filename)
branch, detached = self.branch_ex()
if not branch:
return ""
ret = branch
if self.is_dirty():
ret = ret + "*"
a, b = self.unpushed_info(branch)
if a:
ret = ret + "-%d" % a
if b:
ret = ret + "+%d" % b
if detached:
ret = ret + " [detached]"
return self.prefix + ret