Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
super(TestPatchedFile, self).setUp()
self.patched_file = PatchedFile()
def __init__(self, patch_info=None, source='', target='',
source_timestamp=None, target_timestamp=None,
is_binary_file=False, is_rename=False):
super(PatchedFile, self).__init__()
self.patch_info = patch_info
self.source_file = source
self.source_timestamp = source_timestamp
self.target_file = target
self.target_timestamp = target_timestamp
self.is_binary_file = is_binary_file
self.is_rename = is_rename
# (in that case, source files should match)
if current_file is not None and not (current_file.is_rename and
current_file.source_file == source_file):
current_file = None
continue
# check for target file header
is_target_filename = RE_TARGET_FILENAME.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)
# reset current file
current_file = None
continue
# check for a git rename, target file
is_rename_target_filename = RE_RENAME_TARGET_FILENAME.match(line)
if is_rename_target_filename:
if current_file is not None:
raise UnidiffParseError('Target without source: %s' % line)
# prefix with 'b/' to match expected git source format
target_file = (
'b/' + is_rename_target_filename.group('filename'))
# keep line as patch_info
patch_info.append(line)
# add current file to PatchSet
current_file = PatchedFile(
patch_info, source_file, target_file, None, None,
is_rename=True)
self.append(current_file)
continue
# check for source file header
is_source_filename = RE_SOURCE_FILENAME.match(line)
if is_source_filename:
source_file = is_source_filename.group('filename')
source_timestamp = is_source_filename.group('timestamp')
# reset current file, unless we are processing a rename
# (in that case, source files should match)
if current_file is not None and not (current_file.is_rename and
current_file.source_file == source_file):
current_file = None
continue
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:
current_file._append_trailing_empty_line()
continue
is_binary_diff = RE_BINARY_DIFF.match(line)
if is_binary_diff:
source_file = is_binary_diff.group('source_filename')
target_file = is_binary_diff.group('target_filename')
patch_info.append(line)
current_file = PatchedFile(
patch_info, source_file, target_file, is_binary_file=True)
self.append(current_file)
patch_info = None
current_file = None
continue
# if nothing has matched above then this line is a patch info
if patch_info is None:
current_file = None
patch_info = PatchInfo()
patch_info.append(line)
for line in diff:
# check for source file header
check_source = RE_SOURCE_FILENAME.match(line)
if check_source:
source_file = check_source.group('filename')
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