Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 is_target_filename:
if current_file is not None and not current_file.is_rename:
raise UnidiffParseError('Target without source: %s' % line)
target_file = is_target_filename.group('filename')
target_timestamp = is_target_filename.group('timestamp')
if current_file is None:
# add current file to PatchSet
current_file = PatchedFile(
patch_info, source_file, target_file,
source_timestamp, target_timestamp)
self.append(current_file)
patch_info = None
continue
# check for hunk header
is_hunk_header = RE_HUNK_HEADER.match(line)
if is_hunk_header:
if current_file is None:
raise UnidiffParseError('Unexpected hunk found: %s' % line)
current_file._parse_hunk(line, diff, encoding)
continue
# check for no newline marker
is_no_newline = RE_NO_NEWLINE_MARKER.match(line)
if is_no_newline:
if current_file is None:
raise UnidiffParseError('Unexpected marker: %s' % line)
current_file._add_no_newline_marker_to_last_hunk()
continue
# sometimes hunks can be followed by empty lines
if line == '\n' and current_file is not None:
source_timestamp = check_source.group('timestamp')
current_patch = None
continue
# check for target file header
check_target = RE_TARGET_FILENAME.match(line)
if check_target:
target_file = check_target.group('filename')
target_timestamp = check_target.group('timestamp')
current_patch = PatchedFile(source_file, target_file,
source_timestamp, target_timestamp)
ret.append(current_patch)
continue
# check for hunk header
re_hunk_header = RE_HUNK_HEADER.match(line)
if re_hunk_header:
hunk_info = re_hunk_header.groups()
hunk = _parse_hunk(diff, *hunk_info)
current_patch.append(hunk)
return ret