Skip to content

Commit

Permalink
Ensure git.Repo instances are closed
Browse files Browse the repository at this point in the history
This is mostly whitespace changes to use git.Repo instances as a context
manager so they're closed properly.
  • Loading branch information
cottsay committed Sep 19, 2024
1 parent 5e0a145 commit 97354ad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
78 changes: 39 additions & 39 deletions rosdistro_reviewer/git_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions rosdistro_reviewer/yaml_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 97354ad

Please sign in to comment.