fill not sections not returned by git diff instead of hacking with -U

This commit is contained in:
Bence Pőcze 2023-09-14 19:54:50 +02:00
parent 6e5c3d7f07
commit 54efe52193
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -33,18 +33,26 @@ class LineShiftChecker:
return file_list return file_list
def __get_shifted_lines_in_file(self, file_info) -> 'Dict[str, str | None]': def __get_shifted_lines_in_file(self, file_info) -> 'Dict[str, str | None]':
lines_in_source_file = self.__count_lines_in_source_file(file_info['src']) process_output = subprocess_readlines(['git', 'diff',
process_output = subprocess_readlines(['git', 'diff', f'-U{lines_in_source_file}',
self.revision_since, self.revision_until, '--', self.revision_since, self.revision_until, '--',
file_info['src'], file_info['dst']]) file_info['src'], file_info['dst']])
diff_started = False diff_started = False
shifted_lines = {} shifted_lines = {}
old_start = 1
new_start = 1
for line in process_output: for line in process_output:
matches = re.search(LineShiftChecker.DIFF_BLOCK_REGEX, line) matches = re.search(LineShiftChecker.DIFF_BLOCK_REGEX, line)
if matches: if matches:
old_start = int(matches.group(1)) diff_block_src_start = int(matches.group(1))
new_start = int(matches.group(3)) diff_block_dst_start = int(matches.group(3))
# fill shifted lines between 2 diff blocks
for i in range(0, diff_block_src_start - old_start):
shifted_lines[f'{file_info["src"]}:{old_start+i}'] = f'{file_info["dst"]}:{new_start+i}'
old_start = diff_block_src_start
new_start = diff_block_dst_start
diff_started = True diff_started = True
continue continue
@ -61,11 +69,10 @@ class LineShiftChecker:
shifted_lines[f'{file_info["src"]}:{old_start}'] = None shifted_lines[f'{file_info["src"]}:{old_start}'] = None
old_start += 1 old_start += 1
if not diff_started: # fill shifted lines until end of file
# it was a pure rename, so no diff blocks were returned, lines_in_source_file = self.__count_lines_in_source_file(file_info['src'])
# we just put the lines into the dict for i in range(0, lines_in_source_file - old_start + 1):
shifted_lines = {f'{file_info["src"]}:{i}': f'{file_info["dst"]}:{i}' shifted_lines[f'{file_info["src"]}:{old_start+i}'] = f'{file_info["dst"]}:{new_start+i}'
for i in range(0, lines_in_source_file)}
assert lines_in_source_file == len(shifted_lines) assert lines_in_source_file == len(shifted_lines)