|
1 | 1 | from pathlib import Path |
| 2 | +from typing import Generator |
2 | 3 |
|
3 | | -from git import Repo |
| 4 | +from git import Diff, Repo |
| 5 | +from git.objects import Commit |
4 | 6 |
|
5 | | -from .dtos import Commit, Tree |
6 | | -from .enums import Mode, Type |
| 7 | +from .dtos import Blob as BlobDTO |
| 8 | +from .dtos import Commit as CommitDTO |
| 9 | +from .dtos import Tree as TreeDTO |
7 | 10 |
|
| 11 | +ROOT = Path() |
8 | 12 |
|
9 | | -def read_commit(repo_path: Path, ref: str) -> Commit: |
10 | | - # Find the commit at the given ref in the local repo. |
11 | | - repo = Repo(repo_path) |
12 | | - commit = repo.commit(ref) |
13 | 13 |
|
| 14 | +def iter_blobs(item: Diff) -> Generator[BlobDTO, None, None]: |
| 15 | + match (item.change_type): |
| 16 | + case "A": |
| 17 | + # File added |
| 18 | + assert item.b_path is not None |
| 19 | + assert item.b_blob is not None |
| 20 | + |
| 21 | + yield BlobDTO( |
| 22 | + path=Path(item.b_path.lstrip("/")), |
| 23 | + sha=item.b_blob.hexsha, |
| 24 | + ) |
| 25 | + case "D": |
| 26 | + # File deleted |
| 27 | + assert item.a_path is not None |
| 28 | + |
| 29 | + yield BlobDTO( |
| 30 | + path=Path(item.a_path.lstrip("/")), |
| 31 | + sha=None, |
| 32 | + ) |
| 33 | + case "C": |
| 34 | + # File copied |
| 35 | + assert item.a_path is not None |
| 36 | + assert item.a_blob is not None |
| 37 | + assert item.b_path is not None |
| 38 | + assert item.b_blob is not None |
| 39 | + |
| 40 | + yield BlobDTO( |
| 41 | + path=Path(item.b_path.lstrip("/")), |
| 42 | + sha=item.b_blob.hexsha, |
| 43 | + ) |
| 44 | + case "R": |
| 45 | + # File renamed |
| 46 | + assert item.a_path is not None |
| 47 | + assert item.a_blob is not None |
| 48 | + assert item.b_path is not None |
| 49 | + assert item.b_blob is not None |
| 50 | + |
| 51 | + yield BlobDTO( |
| 52 | + path=Path(item.a_path.lstrip("/")), |
| 53 | + sha=None, |
| 54 | + ) |
| 55 | + yield BlobDTO( |
| 56 | + path=Path(item.b_path.lstrip("/")), |
| 57 | + sha=item.b_blob.hexsha, |
| 58 | + ) |
| 59 | + case "M": |
| 60 | + # File modified |
| 61 | + assert item.b_path is not None |
| 62 | + assert item.b_blob is not None |
| 63 | + |
| 64 | + yield BlobDTO( |
| 65 | + path=Path(item.b_path.lstrip("/")), |
| 66 | + sha=item.b_blob.hexsha, |
| 67 | + ) |
| 68 | + case "T": |
| 69 | + # File changed type (TODO) |
| 70 | + raise NotImplementedError("Diff type 'T' is not supported.") |
| 71 | + case _: |
| 72 | + raise ValueError(f"Unexpected diff type: {item.change_type}") |
| 73 | + |
| 74 | + |
| 75 | +def build_trees(blobs: list[BlobDTO]) -> TreeDTO: |
| 76 | + trees: dict[Path, TreeDTO] = {} |
| 77 | + |
| 78 | + # Create the root tree |
| 79 | + root = trees[ROOT] = TreeDTO(path=ROOT) |
| 80 | + |
| 81 | + # Create all trees |
| 82 | + for blob in blobs: |
| 83 | + path = blob.path.parent |
| 84 | + while path != ROOT: |
| 85 | + trees.setdefault(path, TreeDTO(path)) |
| 86 | + path = path.parent |
| 87 | + |
| 88 | + # Attach blobs to trees |
| 89 | + for blob in blobs: |
| 90 | + trees[blob.path.parent].blobs.append(blob) |
| 91 | + |
| 92 | + # Attach all trees to their parents |
| 93 | + for tree in trees.values(): |
| 94 | + if tree.path == ROOT: |
| 95 | + continue |
| 96 | + |
| 97 | + trees[tree.path.parent].trees.append(tree) |
| 98 | + |
| 99 | + return root |
| 100 | + |
| 101 | + |
| 102 | +def find_parent(commit: Commit) -> Commit: |
14 | 103 | if not commit.parents: |
15 | 104 | raise ValueError("Cannot create a repo's initial commit.") |
16 | 105 |
|
17 | 106 | if len(commit.parents) > 1: |
18 | 107 | raise ValueError("Cannot create a merge commit.") |
19 | 108 |
|
20 | | - parent = commit.parents[0] |
| 109 | + return commit.parents[0] |
21 | 110 |
|
22 | | - # Compute the diff between the commit and its parent |
23 | | - diff = commit.diff(parent.hexsha) |
24 | | - |
25 | | - # Inspect the diff to create a commit |
26 | | - for item in diff: |
27 | | - # TODO |
28 | | - pass |
29 | 111 |
|
| 112 | +def extract_message(commit: Commit) -> str: |
30 | 113 | if isinstance(commit.message, str): |
31 | | - message = commit.message |
| 114 | + return commit.message |
32 | 115 | else: |
33 | | - message = commit.message.decode("utf-8") |
| 116 | + return commit.message.decode("utf-8") |
| 117 | + |
| 118 | + |
| 119 | +def read_commit(repo_path: Path, ref: str) -> CommitDTO: |
| 120 | + # Find the commit at the given ref in the local repo. |
| 121 | + repo = Repo(repo_path) |
| 122 | + commit = repo.commit(ref) |
| 123 | + |
| 124 | + # Compute the diff between the parent and this commit |
| 125 | + parent = find_parent(commit) |
| 126 | + diff = parent.diff(commit.hexsha) |
| 127 | + |
| 128 | + # Inspect the diff to create a commit |
| 129 | + blobs = [blob for diff_item in diff for blob in iter_blobs(diff_item)] |
| 130 | + |
| 131 | + root = build_trees(blobs) |
34 | 132 |
|
35 | | - return Commit( |
| 133 | + return CommitDTO( |
36 | 134 | base_tree=parent.tree.hexsha, |
37 | | - message=message, |
| 135 | + message=extract_message(commit), |
38 | 136 | parents=[parent.hexsha], |
39 | | - tree=Tree( |
40 | | - path="", |
41 | | - mode=Mode.SUBDIRECTORY, |
42 | | - type=Type.TREE, |
43 | | - blobs=[], |
44 | | - trees=[], |
45 | | - sha="", |
46 | | - ), |
| 137 | + tree=root, |
47 | 138 | ) |
0 commit comments