How to use the unidiff.constants.RE_HUNK_BODY_LINE.match function in unidiff

To help you get started, we’ve selected a few unidiff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github matiasb / python-unidiff / unidiff / parser.py View on Github external
def _parse_hunk(diff, source_start, source_len, target_start, target_len,
                section_header):
    """Parse a diff hunk details."""
    hunk = Hunk(source_start, source_len, target_start, target_len,
                section_header)
    source_line_no = hunk.source_start
    target_line_no = hunk.target_start

    for line in diff:
        valid_line = RE_HUNK_BODY_LINE.match(line)
        if not valid_line:
            raise UnidiffParseError('Hunk diff line expected: %s' % line)

        line_type = valid_line.group('line_type')
        value = valid_line.group('value')
        original_line = Line(value, line_type=line_type)
        if line_type == LINE_TYPE_ADDED:
            original_line.target_line_no = target_line_no
            target_line_no += 1
        elif line_type == LINE_TYPE_REMOVED:
            original_line.source_line_no = source_line_no
            source_line_no += 1
        elif line_type == LINE_TYPE_CONTEXT:
            original_line.target_line_no = target_line_no
            target_line_no += 1
            original_line.source_line_no = source_line_no
github matiasb / python-unidiff / unidiff / patch.py View on Github external
def _parse_hunk(self, header, diff, encoding):
        """Parse hunk details."""
        header_info = RE_HUNK_HEADER.match(header)
        hunk_info = header_info.groups()
        hunk = Hunk(*hunk_info)

        source_line_no = hunk.source_start
        target_line_no = hunk.target_start
        expected_source_end = source_line_no + hunk.source_length
        expected_target_end = target_line_no + hunk.target_length

        for diff_line_no, line in diff:
            if encoding is not None:
                line = line.decode(encoding)

            valid_line = RE_HUNK_BODY_LINE.match(line)
            if not valid_line:
                valid_line = RE_HUNK_EMPTY_BODY_LINE.match(line)

            if not valid_line:
                raise UnidiffParseError('Hunk diff line expected: %s' % line)

            line_type = valid_line.group('line_type')
            if line_type == LINE_TYPE_EMPTY:
                line_type = LINE_TYPE_CONTEXT
            value = valid_line.group('value')
            original_line = Line(value, line_type=line_type)
            if line_type == LINE_TYPE_ADDED:
                original_line.target_line_no = target_line_no
                target_line_no += 1
            elif line_type == LINE_TYPE_REMOVED:
                original_line.source_line_no = source_line_no