From e37c851af1a8896985902e54debf9777af895433 Mon Sep 17 00:00:00 2001 From: "Pocze Bence (XC-DX/ESP2-Bp)" Date: Mon, 25 Sep 2023 09:23:15 +0200 Subject: [PATCH] do not diff submodules --- pydiffchecker/helper.py | 33 +++++++++++++++++++++++++++++ pydiffchecker/line_shift_checker.py | 17 ++++----------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/pydiffchecker/helper.py b/pydiffchecker/helper.py index 6a6c214..9ac102c 100644 --- a/pydiffchecker/helper.py +++ b/pydiffchecker/helper.py @@ -13,3 +13,36 @@ def subprocess_readlines(cmd, cwd=None) -> Iterator[str]: if process.returncode != 0: raise subprocess.CalledProcessError(process.returncode, cmd) + + +def get_changed_files(since, until, diff_filter=None, cwd=None): + command = ['git', 'diff-tree', '-r'] + if diff_filter: + command.extend([f'--diff-filter={diff_filter}']) + command.extend([since, until]) + + raw_diff = subprocess_readlines(command, cwd=cwd) + return [parse_raw_file_info(raw_diff_entry) + for raw_diff_entry in raw_diff] + + +def parse_raw_file_info(raw_diff_entry): + diff_entry = raw_diff_entry.lstrip(':').split() + + mode_src = diff_entry[0] + mode_dst = diff_entry[1] + sha1_src = diff_entry[2] + sha1_dst = diff_entry[3] + status = diff_entry[4] + src = diff_entry[5] + dst = diff_entry[6] if len(diff_entry) > 6 else src + + return { + 'mode_src': mode_src, + 'mode_dst': mode_dst, + 'sha1_src': sha1_src, + 'sha1_dst': sha1_dst, + 'status': status, + 'src': src, + 'dst': dst + } diff --git a/pydiffchecker/line_shift_checker.py b/pydiffchecker/line_shift_checker.py index fe183a2..e4644a0 100644 --- a/pydiffchecker/line_shift_checker.py +++ b/pydiffchecker/line_shift_checker.py @@ -1,6 +1,6 @@ import re from typing import List, Dict, Tuple, Sized, Iterable, Iterator -from .helper import subprocess_readlines +from .helper import subprocess_readlines, get_changed_files class ShiftedLines(Sized, Iterable): @@ -28,6 +28,7 @@ class ShiftedLines(Sized, Iterable): class LineShiftChecker: + SUBMODULE_MODE = '160000' DIFF_BLOCK_REGEX = r'@@ -(\d+),(\d+) \+(\d+),(\d+) @@' def __init__(self, revision_since: str, revision_until: str) -> None: @@ -39,18 +40,8 @@ class LineShiftChecker: for file_info in self.__get_changed_files()} def __get_changed_files(self) -> List[Dict]: - process_output = subprocess_readlines(['git', 'diff', '--name-status', '--diff-filter=MR', - self.revision_since, self.revision_until]) - - file_list = [] - for line in process_output: - raw_file_info = line.split() - file_list.append({ - 'src': raw_file_info[1], - 'dst': raw_file_info[2] if len(raw_file_info) > 2 else raw_file_info[1], - }) - - return file_list + return [file_info for file_info in get_changed_files(self.revision_since, self.revision_until, diff_filter='MR') + if file_info['mode_src'] != LineShiftChecker.SUBMODULE_MODE] def __get_shifted_lines_in_file(self, file_info: Dict[str, str]) -> ShiftedLines: process_output = subprocess_readlines(['git', 'diff',