|
| 1 | +import subprocess |
| 2 | +import re |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +# The following classes are introduced to imitate their counterparts in pygit2, |
| 6 | +# so that the output of 'blame_via_subprocess' can be swapped with pygit2's own |
| 7 | +# blame output. |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class GitRef: |
| 11 | + """ |
| 12 | + A reference to a commit |
| 13 | + """ |
| 14 | + hex: str |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class BlameHunk: |
| 18 | + """ |
| 19 | + A chunk of a blame output which has the same commit information |
| 20 | + for a consecutive set of lines |
| 21 | + """ |
| 22 | + orig_commit_id: GitRef |
| 23 | + orig_start_line_number: int |
| 24 | + final_start_line_number: int |
| 25 | + lines_in_hunk: int = 1 |
| 26 | + |
| 27 | + |
| 28 | +def blame_via_subprocess(path, commit, start_line, num_lines): |
| 29 | + """ |
| 30 | + Generate a list of blame hunks by calling 'git blame' as a separate process. |
| 31 | + This is a workaround for the slowness of pygit2's own blame algorithm. |
| 32 | + See https://github.com/aspiers/git-deps/issues/1 |
| 33 | + """ |
| 34 | + cmd = [ |
| 35 | + 'git', 'blame', |
| 36 | + '--porcelain', |
| 37 | + '-L', "%d,+%d" % (start_line, num_lines), |
| 38 | + commit, '--', path |
| 39 | + ] |
| 40 | + output = subprocess.check_output(cmd, universal_newlines=True) |
| 41 | + |
| 42 | + current_hunk = None |
| 43 | + for line in output.split('\n'): |
| 44 | + m = re.match(r'^([0-9a-f]{40}) (\d+) (\d+) (\d+)$', line) |
| 45 | + |
| 46 | + if m: # starting a new hunk |
| 47 | + if current_hunk: |
| 48 | + yield current_hunk |
| 49 | + dependency_sha1, orig_line_num, line_num, length = m.group(1, 2, 3, 4) |
| 50 | + orig_line_num = int(orig_line_num) |
| 51 | + line_num = int(line_num) |
| 52 | + length = int(length) |
| 53 | + current_hunk = BlameHunk( |
| 54 | + orig_commit_id=GitRef(dependency_sha1), |
| 55 | + orig_start_line_number = orig_line_num, |
| 56 | + final_start_line_number = line_num, |
| 57 | + lines_in_hunk = length |
| 58 | + ) |
| 59 | + |
| 60 | + if current_hunk: |
| 61 | + yield current_hunk |
0 commit comments