diff --git a/rosdistro_reviewer/git_lines.py b/rosdistro_reviewer/git_lines.py index e5b9047..431d095 100644 --- a/rosdistro_reviewer/git_lines.py +++ b/rosdistro_reviewer/git_lines.py @@ -46,48 +46,48 @@ def get_added_lines( :returns: Mapping of relative file paths to sequences of line number ranges, or None if no changes were detected """ - repo = Repo(path) - - if head_ref is not None: - head = repo.commit(head_ref) - else: - head = None - - if target_ref is not None: - target = repo.commit(target_ref) - elif head is not None: - target = head.parents[0] - else: - target = repo.head.commit - - if head is not None: - for base in repo.merge_base(target, head): - if base is not None: - break + with Repo(path) as repo: + if head_ref is not None: + head = repo.commit(head_ref) else: - raise RuntimeError( - f"No merge base found between '{target_ref}' and '{head_ref}'") - else: - base = target - - diffs = base.diff(head, paths, True) - - lines: Dict[str, List[int]] = {} - for diff in diffs: - if not diff.b_path: - continue - patch = f"""--- {diff.a_path if diff.a_path else '/dev/null'} + head = None + + if target_ref is not None: + target = repo.commit(target_ref) + elif head is not None: + target = head.parents[0] + else: + target = repo.head.commit + + if head is not None: + for base in repo.merge_base(target, head): + if base is not None: + break + else: + raise RuntimeError( + f"No merge base found between '{target_ref}' and " + f"'{head_ref}'") + else: + base = target + + diffs = base.diff(head, paths, True) + + lines: Dict[str, List[int]] = {} + for diff in diffs: + if not diff.b_path: + continue + patch = f"""--- {diff.a_path if diff.a_path else '/dev/null'} +++ {diff.b_path} {diff.diff.decode()}""" - patchset = unidiff.PatchSet(patch) - for file in patchset: - for hunk in file: - for line in hunk: - if line.line_type != unidiff.LINE_TYPE_ADDED: - continue - lines.setdefault( - os.path.normpath(file.path), - []).append(line.target_line_no) + patchset = unidiff.PatchSet(patch) + for file in patchset: + for hunk in file: + for line in hunk: + if line.line_type != unidiff.LINE_TYPE_ADDED: + continue + lines.setdefault( + os.path.normpath(file.path), + []).append(line.target_line_no) if not lines: return None diff --git a/rosdistro_reviewer/yaml_changes.py b/rosdistro_reviewer/yaml_changes.py index a8af9b3..8805940 100644 --- a/rosdistro_reviewer/yaml_changes.py +++ b/rosdistro_reviewer/yaml_changes.py @@ -79,14 +79,13 @@ def get_changed_yaml( if not changes: return None - repo = Repo(path) - data = {} if head_ref is not None: - for yaml_path in paths: - data[yaml_path] = yaml.load( - repo.tree(head_ref)[yaml_path].data_stream, - Loader=AnnotatedSafeLoader) + with Repo(path) as repo: + for yaml_path in paths: + data[yaml_path] = yaml.load( + repo.tree(head_ref)[yaml_path].data_stream, + Loader=AnnotatedSafeLoader) else: for yaml_path in paths: with (path / yaml_path).open('r') as f: