-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_calls.py
611 lines (462 loc) · 17.6 KB
/
git_calls.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# -*- coding: utf-8 -*-
"""
Wrapper around git command line interface
"""
from __future__ import division, print_function
import subprocess
import sys
import time
import re
import os
from collections import namedtuple
from pandas import Timestamp
from utils import Commit
try:
reload(sys)
sys.setdefaultencoding('utf-8')
except:
pass
#
# Configuration.
#
TIMEZONE = 'Australia/Melbourne' # The timezone used for all commit times. TODO Make configurable
SHA_LEN = 8 # The number of characters used when displaying git SHA-1 hashes
STRICT_CHECKING = True # For validating code.
ExecResult = namedtuple('ExecResult', ['command', 'ret', 'out', 'err'])
def _is_windows():
"""Returns: True if running on a MS-Windows operating system."""
try:
sys.getwindowsversion()
except:
return False
else:
return True
IS_WINDOWS = _is_windows()
if IS_WINDOWS:
import win32api
import win32process
import win32con
def lowpriority():
""" Set the priority of the process to below-normal.
http://stackoverflow.com/questions/1023038/change-process-priority-in-python-cross-platform
"""
pid = win32api.GetCurrentProcessId()
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
win32process.SetPriorityClass(handle, win32process.BELOW_NORMAL_PRIORITY_CLASS)
else:
def lowpriority():
os.nice(1)
def truncate_sha(sha):
"""The way we show git SHA-1 hashes in reports."""
return sha[:SHA_LEN]
def to_timestamp(date_s):
"""Convert string `date_s' to pandas Timestamp in `TIMEZONE`
NOTE: The idea is to get all times in one timezone.
"""
return Timestamp(date_s).tz_convert(TIMEZONE)
concat = ''.join
path_join = os.path.join
def decode_to_str(bytes):
"""Decode byte list `bytes` to a unicode string trying utf-8 encoding first then latin-1.
"""
if bytes is None:
return None
try:
return bytes.decode('utf-8')
except:
return bytes.decode('latin-1')
def run_program(command, async_timeout=60.0):
ps = subprocess.Popen(command,
# shell=True
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = ps.communicate()[:2]
t0 = time.time()
while True:
ret = ps.poll()
if ret is not None or time.time() <= t0 + async_timeout:
break
time.sleep(1.0)
return ret, out, err
def border(name):
return '%s: %s' % (name, (78 - len(name)) * '^')
BORDER = '^' * 80
BORDER_FAILURE = '*' * 80
def format_error(exec_result, name=None, failure=None):
command, ret, out, err = exec_result
assert name is None or isinstance(name, str), (name, type(name))
assert isinstance(ret, int), (ret, type(ret))
if name is None:
# print('-' * 80)
# print(command)
name = ' '.join(command)
summary = '%s: ret=%d,out=%d,err=%d' % (name, ret, len(out), len(err))
parts = [border('out'), out, border('err'), err]
if failure:
parts.extend([BORDER_FAILURE, failure])
parts.extend([BORDER, summary])
return '\n%s\n' % '\n'.join(parts)
_git_debugging = False
def git_debugging(debugging=None):
global _git_debugging
current_debugging = _git_debugging
if debugging is not None:
_git_debugging = debugging
return current_debugging
test_commit = """
commit 66270d2f85404a3425917c1906a418ead6d2cf0e
Author: Smita Khanzo <[email protected]>
Date: Wed Mar 2 11:40:00 2016 +1100
PC-8368: Reverting the temporary change that was made for the test build.
Hello there
"""
RE_LOG_ENTRY = re.compile(r'''
commit\s+(?P<sha>[a-f0-9]{40})\s*\n
(?:Merge:\s*(?P<merge1>[a-f0-9]+)\s+(?P<merge2>[a-f0-9]+)\s*\n)?
Author:\s*(?P<author>.*?)\s*\n
Date:\s*(?P<date>.*?)\s*\n
\s*(?P<body>.*)\s*
$
''', re.VERBOSE | re.DOTALL | re.MULTILINE)
RE_AUTHOR = re.compile(r'<([^@]+@[^@]+)>')
# !@#$ Move to caller
AUTHOR_ALIASES = {
}
def extract_commit(text, issue_extractor):
assert issue_extractor is None or callable(issue_extractor)
m = RE_LOG_ENTRY.search(text)
assert m, text[:1000]
# print(m.groups())
d = m.groupdict()
n = RE_AUTHOR.search(d['author'])
assert n, d['author']
author = n.group(1).lower()
d['author'] = AUTHOR_ALIASES.get(author, author)
d['date'] = to_timestamp(d['date'])
d['body'] = d['body'].strip()
d['issue'] = issue_extractor(d['body'])
commit = Commit(**d)
return commit
if False:
from pprint import pprint
m = RE_LOG_ENTRY.search(test_commit)
print(m.groups())
pprint(m.groupdict())
commit = extract_commit(test_commit)
pprint(commit)
for x in commit:
print(type(x), x)
assert False
def exec_output(command, require_output):
"""Executes `command` which is a list of strings. If `require_output` is True then raise an
exception is there is no stdout.
Returns: ret, output_str, error_str
ret: return code from exec'd process
output_str: stdout of the child process as a string
error_str: stderr of the child process as a string
"""
exception = None
output = None
error = None
ret = -1
if _git_debugging:
print('exec_output(%s)' % ' '.join(command))
try:
ret, output, error = run_program(command)
except Exception as e:
exception = e
if exception is None and require_output and not output:
exception = RuntimeError('exec_output: command=%s' % command)
output_str = decode_to_str(output) if output is not None else ''
error_str = decode_to_str(error) if error is not None else ''
exec_result = ExecResult(command, ret, output_str, error_str)
if exception is not None:
format_error(exec_result)
raise exception
return exec_result
def exec_output_lines(command, require_output, separator=None):
"""Executes `command` which is a list of strings. If `require_output` is True then raise an
exception is there is no stdout.
Returns: ret, output_str, error_str
ret: return code from exec'd process
output_lines: stdout of the child process as a list of strings, one string per line
error_str: stderr of the child process as a string
"""
exec_result = exec_output(command, require_output)
if separator is None:
separator = '\n'
output_lines = exec_result.out.split(separator)
# if separator is not None:
# output_lines = exec_result.out.split(separator)
# else:
# output_lines = exec_result.out.splitlines()
assert output_lines, format_error(exec_result)
while output_lines and not output_lines[-1]:
# print(len(output_lines), output_lines[-1])
output_lines.pop()
return output_lines, exec_result
def exec_headline(command):
"""Execute `command` which is a list of strings.
Returns: The first line stdout of the child process.
Returns: ret, output_str, error_str
ret: return code from exec'd process
output_line: he first line stdout of the child process.
error_str: stderr of the child process as a string
"""
output_lines, exec_result = exec_output_lines(command, True)
return output_lines[0], exec_result
def git_config_set(key, value):
return exec_output(['git', 'config', key, value], False)
def git_config_unset(key):
return exec_output(['git', 'config', '--unset', key], False)
def git_reset(obj, hard=True):
command = ['git', 'reset']
if hard:
command.append('--hard')
command.append(obj)
return exec_output(command, False)
def git_file_list(path_patterns=()):
"""Returns: List of files in current git revision matching `path_patterns`.
This is basically git ls-files.
"""
return exec_output_lines(['git', 'ls-files', '--exclude-standard'] + path_patterns, False)
def git_commit_file_list(sha):
"""
git diff-tree --no-commit-id --name-only -r
"""
command = ['git', 'diff-tree', '--no-commit-id', '--name-only', '-r', '-M', sha]
return exec_output_lines(command, False)
def git_diff_lines(treeish, path):
"""
treeish: A git tree-ish id, typically a commit id
path: Path to a file
Returns: patch for diff of blob specified by (`treeish`, `path`) and its parent
This is basically
git diff-tree -p e3eb9aa88741ee250defa1bcd43e6b2385f556b1 -- providers/airprint/mac/airprint.c
"""
command = ['git', 'diff-tree', '-p', '--full-index', treeish, '--', path]
return exec_output_lines(command, False)
def git_pending_list(path_patterns=()):
"""Returns: List of git pending files matching `path_patterns`.
"""
return exec_output_lines(['git', 'diff', '--name-only'] + path_patterns, False)
def git_file_list_no_pending(path_patterns=()):
"""Returns: List of non-pending files in current git revision matching `path_patterns`.
"""
file_list = git_file_list(path_patterns)
pending = set(git_pending_list(path_patterns))
return [path for path in file_list if path not in pending]
def git_diff(rev1, rev2):
"""Returns: List of files that differ in git revisions `rev1` and `rev2`.
"""
return exec_output_lines(['git', 'diff', '--name-only', rev1, rev2], False)
def git_log(pattern=None, inclusions=None, exclusions=None):
"""Returns: List of commits in ancestors of current git revision with commit messages matching
`pattern`.
This is basically git log --grep=<pattern>.
"""
# git ls-files -z returns a '\0' separated list of files terminated with '\0\0'
command = ['git', 'log', '-z',
'--perl-regexp',
# '--grep="%s"' % pattern
'--no-merges'
]
if pattern:
command.append("--grep='%s'" % pattern)
if inclusions:
command.extend(inclusions)
if exclusions:
command.extend(['^%s' % obj for obj in exclusions])
bin_list, exec_result = exec_output_lines(command, False, '\0')
print('bin_list=%d' % len(bin_list))
# print(exec_result)
commit_list = []
for commit in bin_list:
if not commit:
break
commit_list.append(commit)
assert commit_list
return commit_list, exec_result
def git_log_extract(issue_extractor, pattern=None, inclusions=None, exclusions=None):
assert issue_extractor is None or callable(issue_extractor)
entry_list, exec_result = git_log(pattern, inclusions, exclusions)
return [extract_commit(entry, issue_extractor) for entry in entry_list], exec_result
def git_show(obj=None, quiet=False):
"""Returns: Description of a git object `obj`, which is typically a commit.
https://git-scm.com/docs/git-show
"""
if obj is not None:
assert isinstance(obj, str), obj
command = ['git', 'show']
if quiet:
command.append('--quiet')
if obj is not None:
command.append(obj)
return exec_output_lines(command, True)
def git_show_extract(issue_extractor, obj=None):
assert issue_extractor is None or callable(issue_extractor)
lines, exec_result = git_show(obj=obj, quiet=True)
text = '\n'.join(lines)
return extract_commit(text, issue_extractor), exec_result
def git_show_oneline(obj=None):
"""Returns: One-line description of a git object `obj`, which is typically a commit.
https://git-scm.com/docs/git-show
"""
command = ['git', 'show', '--oneline', '--quiet']
if obj is not None:
command.append(obj)
return exec_headline(command)
def git_show_sha(obj=None):
"""Returns: SHA-1 hash `obj`, which is typically a commit.
https://git-scm.com/docs/git-show
"""
command = ['git', 'show', '--format=%H']
if obj is not None:
command.append(obj)
return exec_headline(command)
if False:
ret, line, err = git_show_sha()
print('ret=%d,err="%s"' % (ret, err))
print(line)
assert False
# def git_date(obj):
# """Returns: Date of a git object `obj`, which is typically a commit.
# NOTE: The returned date is standardized to timezone TIMEZONE.
# """
# date_s = exec_headline(['git', 'show', '--pretty=format:%ai', '--quiet', obj])
# return to_timestamp(date_s)
RE_REMOTE_URL = re.compile(r'(https?://.*/[^/]+(?:\.git)?)\s+\(fetch\)')
RE_REMOTE_NAME = re.compile(r'https?://.*/(.+?)(\.git)?$')
def git_remote():
"""Returns: The remote URL and a short name for the current repository.
"""
# $ git remote -v
# origin https://github.com/FFTW/fftw3.git (fetch)
# origin https://github.com/FFTW/fftw3.git (push)
try:
output_lines, exec_result = exec_output_lines(['git', 'remote', '-v'], True)
except Exception as e:
print('git_remote error: %s' % e)
return None, None
for line in output_lines:
m = RE_REMOTE_URL.search(line)
if not m:
continue
remote_url = m.group(1)
remote_name = RE_REMOTE_NAME.search(remote_url).group(1)
return remote_url, remote_name
raise RuntimeError('No remote')
def git_describe():
"""Returns: git describe of current revision.
"""
return exec_headline(['git', 'describe', '--always'])
def git_name():
"""Returns: git name of current revision.
"""
return ' '.join(exec_headline(['git', 'name-rev', 'HEAD']).split()[1:])
def git_current_branch():
"""Returns: git name of current branch or None if there is no current branch (detached HEAD).
"""
branch, exec_result = exec_headline(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
if branch == 'HEAD': # Detached HEAD?
branch = None
return branch, exec_result
def git_current_revision():
"""Returns: SHA-1 of current revision.
"""
return exec_headline(['git', 'rev-parse', 'HEAD'])
def git_revision_description():
"""Returns: Our best guess at describing the current revision"""
description = git_current_branch()
if not description:
description = git_describe()
return description
def git_blame_text(path):
"""Returns: git blame text for file `path`
"""
return exec_output(['git', 'blame', '-l', '-f', '-w', '-M', path], False)
RE_REMOTE = re.compile(r'origin/(.*?)\s*$')
RE_LOCAL = re.compile(r'\*?\s*(.*?)\s*$')
def git_track_all(delete_local):
"""Track all remote branches
If `delete_local` is True then delete all local branches (except checked out branch)
before tracking.
Returns: local_branches, exec_result
local_branches: List of local branches with remotes
exec_result: Result of exec_output()
"""
local_branches0, exec_result = exec_output_lines(['git', 'branch'], False)
assert exec_result.ret == 0, format_error(exec_result)
local_branches = set()
for i, local in enumerate(local_branches0):
m = RE_LOCAL.search(local)
assert m, 'local="%s"' % local
local = m.group(1)
local_branches.add(local)
current_branch, exec_result = git_current_branch()
assert exec_result.ret == 0, format_error(exec_result)
if delete_local:
removed_branches = set()
for local in local_branches:
if local == current_branch:
continue
exec_result = exec_output(['git', 'branch', '-D', local], False)
assert exec_result.ret == 0, format_error(exec_result)
removed_branches.add(local)
local_branches -= removed_branches
remote_branches0, exec_result = exec_output_lines(['git', 'branch', '-r'], False)
assert exec_result.ret == 0, format_error(exec_result)
local_remote = {}
for i, remote in enumerate(remote_branches0):
if 'origin/HEAD ->' in remote:
continue
m = RE_LOCAL.search(remote)
assert m, remote
remote = m.group(1)
m = RE_REMOTE.search(remote)
assert m, remote
local = m.group(1)
local_remote[local] = remote
for i, (local, remote) in enumerate(sorted(local_remote.items())):
if local in local_branches:
continue
print('%3d: Tracking "%s" from "%s"' % (i, local, remote))
exec_result = exec_output(['git', 'branch', '--track', local], False)
if exec_result.ret != 0:
print('Could not track remote branch local="%s",remote="%s"' % (local, remote))
assert exec_result.ret == 0, format_error(exec_result)
local_branches.add(local)
# Only interested in branches that have a remote
local_branches = {local: remote for local in local_branches if local in local_remote}
return local_branches, exec_result
def git_fetch():
"""
"""
return exec_output(['git', 'fetch', '--all', '--tags', '--prune', '--force'], False)
def git_checkout(branch, force=False):
"""
"""
command = ['git', 'checkout']
if force:
command.append('--force')
command.append(branch)
return exec_output(command, False)
def _find_conflicts(out_lines):
return [line for line in out_lines if line.startswith('CONFLICT')]
def git_pull(branch):
"""
"""
out_lines, exec_result = exec_output_lines(['git', 'pull', '--force', '--ff', 'origin', branch],
False)
return _find_conflicts(out_lines), out_lines, exec_result
def git_push(branch):
"""
"""
assert False, branch
return exec_output(['git', 'push', 'origin', branch], False)
def git_merge(branch):
"""
"""
out_lines, exec_result = exec_output_lines(['git', 'merge', branch], False)
return _find_conflicts(out_lines), out_lines, exec_result