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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
import subprocess, re, os, tempfile
class GitError(Exception):
pass
class SHAError(GitError):
pass
class ExecutionError(GitError):
def __init__(self, errcode):
self.error_code = errcode
def _check(process):
if process.returncode != 0:
raise ExecutionError(process.returncode)
_sha_re = re.compile('^[0-9a-fA-F]*$')
def rev_parse(rev='HEAD', tree=None):
process = subprocess.Popen(['git', 'rev-parse', rev],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
sha = stdout.strip()
if not _sha_re.match(sha):
raise SHAError()
return sha
def clean(tree=None):
cmd = ['git', 'clean', '-f', '-x', '-d', '-q']
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
def status(tree=None):
'''
For interpretation of the porcelain output refer to
the git status(1) man page. In summary the first column is the
index state, the second represents the working tree state and
the third column are files in cases of renames. This gives back
None in case no changes are present, otherwise it returns a list
of dict entries with key values: index, work_tree, and files. The
files are a list of all files found on that entry, in case of a rename
this would consist of a list of 2 files.
As an example if you had this on your git status:
R udev/foo.sh -> poo/foo.sh
D scripts/bar.sh
?? .poo.py.swp
This would be transposed into the following dict:
results = status(tree=your_git_tree_path)
if not results:
return 0
for entry in results:
print entry
{'files': [' udev/foo.sh', 'poo/foo.sh'], 'index': 'R', 'work_tree': ' '}
{'files': [' scripts/bar.sh'], 'index': 'D', 'work_tree': ' '}
{'files': [' .poo.py.swp'], 'index': '?', 'work_tree': '?'}
'''
def split_status(entry):
if len(entry) == 0:
return None
if len(entry) == 1:
return dict(index=entry[0], work_tree=None, files=None)
if len(entry) == 2:
return dict(index=entry[0], work_tree=entry[1], files=None)
else:
return dict(index=entry[0], work_tree=entry[1],
files=entry[2:].split(' -> '))
cmd = ['git', 'status', '--porcelain']
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True,
universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
list_status = stdout.split('\n')
if (len(list_status) == 1 and list_status[0] == ''):
return None
return [split_status(entry) for entry in list_status]
def describe(rev='HEAD', tree=None, extra_args=[]):
cmd = ['git', 'describe', '--always']
cmd.extend(extra_args)
if rev is not None:
cmd.append(rev)
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
return stdout.strip()
def verify(git_tree):
tag = describe(rev=None, tree=git_tree, extra_args=['--dirty'])
cmd = ['git', 'tag', '-v', tag]
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True, cwd=git_tree)
stdout = process.communicate()[0]
process.wait()
return dict(r=process.returncode, output=stdout)
def paranoia(tree):
clean(tree)
poo = status(tree)
if (poo):
return dict(r=-1, output=poo)
return verify(tree)
def init(tree=None):
process = subprocess.Popen(['git', 'init'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
def add(path, tree=None):
process = subprocess.Popen(['git', 'add', path],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
def commit_all(message, tree=None):
add('.', tree=tree)
process = subprocess.Popen(['git', 'commit', '--allow-empty', '-a', '-m', message],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
def ls_tree(rev, files, tree=None):
process = subprocess.Popen(['git', 'ls-tree', '-z', '-r', rev, '--', ] + list(files),
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
files = stdout.split('\0')
ret = []
for f in files:
if not f:
continue
meta, f = f.split('\t', 1)
meta = meta.split()
meta.append(f)
ret.append(meta)
process.wait()
_check(process)
return ret
def get_blob(blob, outf, tree=None):
try:
import git, gitdb
r = git.Repo(path=tree)
b = r.rev_parse(blob + '^{blob}')
b.stream_data(outf)
except ImportError:
process = subprocess.Popen(['git', 'show', blob],
stdout=outf, close_fds=True, cwd=tree)
process.wait()
_check(process)
def clone(gittree, outputdir, options=[]):
process = subprocess.Popen(['git', 'clone'] + options + [gittree, outputdir])
process.wait()
_check(process)
def set_origin(giturl, gitdir):
process = subprocess.Popen(['git', 'remote', 'rm', 'origin'],
close_fds=True, universal_newlines=True, cwd=gitdir)
process.wait()
process = subprocess.Popen(['git', 'remote', 'add', 'origin', giturl],
close_fds=True, universal_newlines=True, cwd=gitdir)
process.wait()
_check(process)
def remote_update(gitdir):
process = subprocess.Popen(['git', 'remote', 'update'],
close_fds=True, universal_newlines=True, cwd=gitdir)
process.wait()
_check(process)
def shortlog(from_commit, to_commit, tree=None):
process = subprocess.Popen(['git', 'shortlog', from_commit + '..' + to_commit],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, universal_newlines=True,
cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
return stdout
def commit_env_vars(commitid, tree=None):
process = subprocess.Popen(['git', 'show', '--name-only',
'--format=format:GIT_AUTHOR_NAME=%an%nGIT_AUTHOR_EMAIL=%ae%nGIT_AUTHOR_DATE=%aD%x00',
commitid],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True,
cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
data = stdout.split('\x00')[0]
vals = data.split('\n')
d = {}
for k, v in map(lambda x: x.split('=', 1), vals):
d[k] = v
return d
def commit_message(commitid, tree=None):
process = subprocess.Popen(['git', 'show', '--name-only',
'--format=format:%s%n%n%b%x00', commitid],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True,
cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
return stdout.split('\x00')[0]
def remove_config(cfg, tree=None):
process = subprocess.Popen(['git', 'config', '--unset-all', cfg],
close_fds=True, universal_newlines=True, cwd=tree)
process.wait()
_check(process)
def ls_remote(branch, tree=None, remote='origin'):
process = subprocess.Popen(['git', 'ls-remote', '--exit-code', remote, 'refs/heads/' + branch],
stdout=subprocess.PIPE,
close_fds=True, universal_newlines=True, cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
sha = stdout.split()[0]
if not _sha_re.match(sha):
raise SHAError()
return sha
def add(fn, tree=None):
process = subprocess.Popen(['git', 'add', fn], cwd=tree,
close_fds=True, universal_newlines=True)
process.wait()
_check(process)
def commit(msg, tree=None, env = {}, opts=[]):
stdin = tempfile.NamedTemporaryFile(mode='wr')
stdin.write(msg)
stdin.seek(0)
process = subprocess.Popen(['git', 'commit', '--file=-'] + opts,
stdin=stdin.file, universal_newlines=True, env=env,
cwd=tree)
process.wait()
_check(process)
def push(opts=[], tree=None):
process = subprocess.Popen(['git', 'push'] + opts,
close_fds=True, universal_newlines=True, cwd=tree)
process.wait()
_check(process)
def log_commits(from_commit, to_commit, tree=None):
process = subprocess.Popen(['git', 'log', '--first-parent', '--format=format:%H',
from_commit + '..' + to_commit],
stdout=subprocess.PIPE,
close_fds=True, universal_newlines=True,
cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
vals = stdout.split()
vals.reverse()
return vals
def commit_env_vars(commitid, tree=None):
process = subprocess.Popen(['git', 'show', '--name-only',
'--format=format:GIT_AUTHOR_NAME=%an%nGIT_AUTHOR_EMAIL=%ae%nGIT_AUTHOR_DATE=%aD%x00',
commitid],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
close_fds=True, universal_newlines=True,
cwd=tree)
stdout = process.communicate()[0]
process.wait()
_check(process)
data = stdout.split('\x00')[0]
vals = data.split('\n')
d = {}
for k, v in map(lambda x: x.split('=', 1), vals):
d[k] = v
return d
def rm(opts=[], tree=None):
process = subprocess.Popen(['git', 'rm'] + opts,
close_fds=True, universal_newlines=True, cwd=tree)
process.wait()
_check(process)
def reset(opts=[], tree=None):
process = subprocess.Popen(['git', 'reset'] + opts,
close_fds=True, universal_newlines=True, cwd=tree)
process.wait()
_check(process)
|