diff options
author | Antonin Delpeuch <antonin@delpeuch.eu> | 2023-09-20 12:31:26 +0200 |
---|---|---|
committer | Antonin Delpeuch <antonin@delpeuch.eu> | 2023-09-23 10:42:47 +0200 |
commit | 70fdff7898416dfa26999194c387abdd17e8acb8 (patch) | |
tree | eb05e624d46941495f9f8581a40ee6db6b118a10 /git_deps | |
parent | caca4f6936ed1226ef20792542af5189cc110078 (diff) | |
download | git-deps-70fdff7898416dfa26999194c387abdd17e8acb8.tar.gz |
Blame via pygit2 instead of subprocess
Closes #1.
Diffstat (limited to 'git_deps')
-rw-r--r-- | git_deps/detector.py | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/git_deps/detector.py b/git_deps/detector.py index 1836a56..a7036c9 100644 --- a/git_deps/detector.py +++ b/git_deps/detector.py @@ -172,24 +172,26 @@ class DependencyDetector(object): line_to_culprit = {} - for line in blame.split('\n'): - self.process_hunk_line(dependent, dependent_sha1, parent, - path, line, line_to_culprit) + for blame_hunk in blame: + self.process_blame_hunk(dependent, dependent_sha1, parent, + path, blame_hunk, line_to_culprit) self.debug_hunk(line_range_before, line_range_after, hunk, line_to_culprit) - def process_hunk_line(self, dependent, dependent_sha1, parent, - path, line, line_to_culprit): - self.logger.debug(" ! " + line.rstrip()) - m = re.match(r'^([0-9a-f]{40}) (\d+) (\d+)( \d+)?$', line) - if not m: - return + def process_blame_hunk(self, dependent, dependent_sha1, parent, + path, blame_hunk, line_to_culprit): + + orig_line_num = blame_hunk.orig_start_line_number + line_num = blame_hunk.final_start_line_number + dependency_sha1 = blame_hunk.orig_commit_id.hex + line_representation = f"{dependency_sha1} {orig_line_num} {line_num}" + + self.logger.debug(f" ! {line_representation}") - dependency_sha1, orig_line_num, line_num = m.group(1, 2, 3) - line_num = int(line_num) dependency = self.get_commit(dependency_sha1) - line_to_culprit[line_num] = dependency.hex + for i in range(blame_hunk.lines_in_hunk): + line_to_culprit[line_num + i] = dependency.hex if self.is_excluded(dependency): self.logger.debug( @@ -206,7 +208,7 @@ class DependencyDetector(object): self.record_dependency_source(parent, dependent, dependent_sha1, dependency, dependency_sha1, - path, line_num, line) + path, line_num, line_representation) def debug_hunk(self, line_range_before, line_range_after, hunk, line_to_culprit): @@ -234,13 +236,10 @@ class DependencyDetector(object): self.notify_listeners("new_dependent", dependent) def run_blame(self, hunk, parent, path): - cmd = [ - 'git', 'blame', - '--porcelain', - '-L', "%d,+%d" % (hunk.old_start, hunk.old_lines), - parent.hex, '--', path - ] - return subprocess.check_output(cmd, universal_newlines=True) + return self.repo.blame(path, + newest_commit=parent.hex, + min_line=hunk.old_start, + max_line=hunk.old_start + hunk.old_lines - 1) def is_excluded(self, commit): if self.options.exclude_commits is not None: |