|
| 1 | +"""This was reintroduced to deal with any bugs in pygit2 (or the libgit2 C library it |
| 2 | +binds to). The `parse_diff()` function here is only used when |
| 3 | +`pygit2.Diff.parse_diff()` function fails in `cpp_linter.git.parse_diff()`""" |
| 4 | +import re |
| 5 | +from typing import Optional, List, Tuple, cast |
| 6 | +from . import FileObj, logger |
| 7 | + |
| 8 | + |
| 9 | +DIFF_FILE_DELIMITER = re.compile(r"^diff --git a/.*$", re.MULTILINE) |
| 10 | +DIFF_FILE_NAME = re.compile(r"^\+\+\+\sb?/(.*)$", re.MULTILINE) |
| 11 | +DIFF_RENAMED_FILE = re.compile(r"^rename to (.*)$", re.MULTILINE) |
| 12 | +DIFF_BINARY_FILE = re.compile(r"^Binary\sfiles\s", re.MULTILINE) |
| 13 | +HUNK_INFO = re.compile(r"@@\s\-\d+,\d+\s\+(\d+,\d+)\s@@", re.MULTILINE) |
| 14 | + |
| 15 | + |
| 16 | +def _get_filename_from_diff(front_matter: str) -> Optional[re.Match]: |
| 17 | + """Get the filename from content in the given diff front matter.""" |
| 18 | + filename_match = DIFF_FILE_NAME.search(front_matter) |
| 19 | + if filename_match is not None: |
| 20 | + return filename_match |
| 21 | + |
| 22 | + # check for renamed file name |
| 23 | + rename_match = DIFF_RENAMED_FILE.search(front_matter) |
| 24 | + if rename_match is not None and front_matter.lstrip().startswith("similarity"): |
| 25 | + return rename_match |
| 26 | + # We may need to compensate for other instances where the filename is |
| 27 | + # not directly after `+++ b/`. Binary files are another example of this. |
| 28 | + if DIFF_BINARY_FILE.search(front_matter) is None: |
| 29 | + # log the case and hope it helps in the future |
| 30 | + logger.warning( # pragma: no cover |
| 31 | + "Unrecognized diff starting with:\n%s", |
| 32 | + "\n".join(front_matter.splitlines()), |
| 33 | + ) |
| 34 | + return None |
| 35 | + |
| 36 | +def parse_diff(full_diff: str) -> List[FileObj]: |
| 37 | + """Parse a given diff into file objects. |
| 38 | +
|
| 39 | + :param full_diff: The complete diff for an event. |
| 40 | + :returns: A `list` of `FileObj` instances containing information about the files |
| 41 | + changed. |
| 42 | + """ |
| 43 | + file_objects: List[FileObj] = [] |
| 44 | + logger.error("Using pure python to parse diff because pygit2 failed!") |
| 45 | + file_diffs = DIFF_FILE_DELIMITER.split(full_diff.lstrip("\n")) |
| 46 | + for diff in file_diffs: |
| 47 | + if not diff or diff.lstrip().startswith("deleted file"): |
| 48 | + continue |
| 49 | + first_hunk = HUNK_INFO.search(diff) |
| 50 | + hunk_start = -1 if first_hunk is None else first_hunk.start() |
| 51 | + diff_front_matter = diff[:hunk_start] |
| 52 | + filename_match = _get_filename_from_diff(diff_front_matter) |
| 53 | + if filename_match is None: |
| 54 | + continue |
| 55 | + filename = cast(str, filename_match.groups(0)[0]) |
| 56 | + if first_hunk is None: |
| 57 | + continue |
| 58 | + diff_chunks, additions = _parse_patch(diff[first_hunk.start() :]) |
| 59 | + file_objects.append(FileObj(filename, additions, diff_chunks)) |
| 60 | + return file_objects |
| 61 | + |
| 62 | + |
| 63 | +def _parse_patch(full_patch: str) -> Tuple[List[List[int]], List[int]]: |
| 64 | + """Parse a diff's patch accordingly. |
| 65 | +
|
| 66 | + :param full_patch: The entire patch of hunks for 1 file. |
| 67 | + :returns: |
| 68 | + A `tuple` of lists where |
| 69 | +
|
| 70 | + - Index 0 is the ranges of lines in the diff. Each item in this `list` is a |
| 71 | + 2 element `list` describing the starting and ending line numbers. |
| 72 | + - Index 1 is a `list` of the line numbers that contain additions. |
| 73 | + """ |
| 74 | + ranges: List[List[int]] = [] |
| 75 | + # additions is a list line numbers in the diff containing additions |
| 76 | + additions: List[int] = [] |
| 77 | + line_numb_in_diff: int = 0 |
| 78 | + chunks = HUNK_INFO.split(full_patch) |
| 79 | + for index, chunk in enumerate(chunks): |
| 80 | + if index % 2 == 1: |
| 81 | + # each odd element holds the starting line number and number of lines |
| 82 | + start_line, hunk_length = [int(x) for x in chunk.split(",")] |
| 83 | + ranges.append([start_line, hunk_length + start_line]) |
| 84 | + line_numb_in_diff = start_line |
| 85 | + continue |
| 86 | + # each even element holds the actual line changes |
| 87 | + for i, line in enumerate(chunk.splitlines()): |
| 88 | + if line.startswith("+"): |
| 89 | + additions.append(line_numb_in_diff) |
| 90 | + if not line.startswith("-") and i: # don't increment on first line |
| 91 | + line_numb_in_diff += 1 |
| 92 | + return (ranges, additions) |
0 commit comments