A before/after file pair on local disk
| 9 | |
| 10 | @dataclass |
| 11 | class LocalFileDiff: |
| 12 | """A before/after file pair on local disk""" |
| 13 | |
| 14 | a_root: str |
| 15 | """Path to the root dir of the left side of the diff""" |
| 16 | a_path: str |
| 17 | """Full path to the left file on disk (may be empty).""" |
| 18 | b_root: str |
| 19 | """Path to the root dir of the right side of the diff""" |
| 20 | b_path: str |
| 21 | """Full path to the right file on disk (may be empty if a_path != '').""" |
| 22 | is_move: bool |
| 23 | """Is this a move between the two files?""" |
| 24 | num_add: Union[int, None] = None |
| 25 | num_delete: Union[int, None] = None |
| 26 | |
| 27 | @property |
| 28 | def a(self): |
| 29 | if self.a_path == '': |
| 30 | return '' |
| 31 | return os.path.relpath(self.a_path, self.a_root) |
| 32 | |
| 33 | @property |
| 34 | def b(self): |
| 35 | if self.b_path == '': |
| 36 | return '' |
| 37 | return os.path.relpath(self.b_path, self.b_root) |
| 38 | |
| 39 | @property |
| 40 | def type(self): |
| 41 | if self.a_path == '': |
| 42 | return 'add' |
| 43 | elif self.b_path == '': |
| 44 | return 'delete' |
| 45 | elif self.is_move: |
| 46 | return 'move' |
| 47 | return 'change' |
| 48 | |
| 49 | @staticmethod |
| 50 | def from_diff_raw_line(line: RawDiffLine, a_dir: str, b_dir: str): |
| 51 | status = line.status |
| 52 | # A, C (copy), D, M, R, T (change in type), U (unmerged), X (bug) |
| 53 | if status == 'A': |
| 54 | return LocalFileDiff(a_dir, '', b_dir, line.path, is_move=False, num_add=line.num_add, num_delete=line.num_delete) |
| 55 | if status == 'D': |
| 56 | return LocalFileDiff(a_dir, line.path, b_dir, '', is_move=False, num_add=line.num_add, num_delete=line.num_delete) |
| 57 | if line.dst_path: |
| 58 | return LocalFileDiff(a_dir, line.path, b_dir, line.dst_path, is_move=True, num_add=line.num_add, num_delete=line.num_delete) |
| 59 | dst_path = os.path.join(b_dir, os.path.relpath(line.path, a_dir)) |
| 60 | return LocalFileDiff(a_dir, line.path, b_dir, dst_path, is_move=False, num_add=line.num_add, num_delete=line.num_delete) |
no outgoing calls
no test coverage detected