(content: str, filemode: Filemode)
Return the lines with line number from a git patch.
Return the lines with line number from a git patch.
Return the lines with line number from a git patch .
def get_lines_from_patch(content: str, filemode: Filemode) -> Iterable[Line]:
    """Return the lines with line number from a git patch."""
    content += "\n"
    pre_index = 0
    post_index = 0

    for line in content.split("\n"):
        line_type = line[:1]
        line_content = ""
        line_pre_index = None
        line_post_index = None
        category = None

        if line_type == " ":
            line_content = line[1:]
            pre_index += 1
            post_index += 1
            line_pre_index = pre_index
            line_post_index = post_index
        elif line_type == "@":
            m = REGEX_PATCH_HEADER.search(line)
            if m is None:
                continue
            pre_index = int(m.groupdict()["pre_index"])
            post_index = int(m.groupdict()["post_index"])
            line_content = m.groupdict()["line_content"][:-1]

            if filemode == Filemode.NEW or filemode == Filemode.DELETE:
                pre_index = 1
                post_index = 1

            if line_content:
                line_type = " "
                pre_index -= 1
                post_index -= 1
                line_pre_index = None
                line_post_index = None
                category = LineCategory.empty
        elif line_type == "+":
            post_index += 1
            line_post_index = post_index
            line_content = line[1:]
            category = LineCategory.addition
        elif line_type == "-":
            pre_index += 1
            line_pre_index = pre_index
            line_content = line[1:]
            category = LineCategory.deletion
        elif line_type == "\\":
            # This type of line should'nt contain any secret; no need to set indices
            line_content = line[1:]

        if line_type and line_content is not None:
            yield Line(
                content=line_content,
                category=category,
                pre_index=line_pre_index,
                post_index=line_post_index,
            )
def get_lines_from_patch ( content : str , filemode : Filemode ) -> Iterable [ Line ] : content += "\n" pre_index = 0 post_index = 0 for line in content . split ( "\n" ) : line_type = line [ : 1 ] line_content = "" line_pre_index = None line_post_index = None category = None if line_type == " " : line_content = line [ 1 : ] pre_index += 1 post_index += 1 line_pre_index = pre_index line_post_index = post_index elif line_type == "@" : m = REGEX_PATCH_HEADER . search ( line ) if m is None : continue pre_index = int ( m . groupdict ( ) [ "pre_index" ] ) post_index = int ( m . groupdict ( ) [ "post_index" ] ) line_content = m . groupdict ( ) [ "line_content" ] [ : - 1 ] if filemode == Filemode . NEW or filemode == Filemode . DELETE : pre_index = 1 post_index = 1 if line_content : line_type = " " pre_index -= 1 post_index -= 1 line_pre_index = None line_post_index = None category = LineCategory . empty elif line_type == "+" : post_index += 1 line_post_index = post_index line_content = line [ 1 : ] category = LineCategory . addition elif line_type == "-" : pre_index += 1 line_pre_index = pre_index line_content = line [ 1 : ] category = LineCategory . deletion elif line_type == "\\" : # This type of line should'nt contain any secret; no need to set indices line_content = line [ 1 : ] if line_type and line_content is not None : yield Line ( content = line_content , category = category , pre_index = line_pre_index , post_index = line_post_index , )