List the lines changed between two Git refs for a specific file.
(ref_a: str, ref_b: str, file: Path)
| 66 | |
| 67 | |
| 68 | def get_diff_lines(ref_a: str, ref_b: str, file: Path) -> list[int]: |
| 69 | """List the lines changed between two Git refs for a specific file.""" |
| 70 | diff_output = subprocess.run( |
| 71 | [ |
| 72 | "git", |
| 73 | "diff", |
| 74 | "--unified=0", |
| 75 | f"{ref_a}...{ref_b}", |
| 76 | "--", |
| 77 | str(file), |
| 78 | ], |
| 79 | stdout=subprocess.PIPE, |
| 80 | check=True, |
| 81 | text=True, |
| 82 | encoding="UTF-8", |
| 83 | ) |
| 84 | |
| 85 | # Scrape line offsets + lengths from diff and convert to line numbers |
| 86 | line_matches = DIFF_PATTERN.finditer(diff_output.stdout) |
| 87 | # Removed and added line counts are 1 if not printed |
| 88 | line_match_values = [ |
| 89 | line_match.groupdict(default=1) for line_match in line_matches |
| 90 | ] |
| 91 | line_ints = [ |
| 92 | (int(match_value["lineb"]), int(match_value["added"])) |
| 93 | for match_value in line_match_values |
| 94 | ] |
| 95 | line_ranges = [ |
| 96 | range(line_b, line_b + added) for line_b, added in line_ints |
| 97 | ] |
| 98 | line_numbers = list(itertools.chain(*line_ranges)) |
| 99 | |
| 100 | return line_numbers |
| 101 | |
| 102 | |
| 103 | def get_para_line_numbers(file_obj: TextIO) -> list[list[int]]: |
no test coverage detected
searching dependent graphs…