| 209 | @dataclasses.dataclass(eq=True) |
| 210 | @functools.total_ordering |
| 211 | class PackageCommitPatchData: |
| 212 | vcs_url: str |
| 213 | commit_hash: str |
| 214 | patch_text: Optional[str] = None |
| 215 | patch_checksum: Optional[str] = dataclasses.field(init=False, default=None) |
| 216 | |
| 217 | def __post_init__(self): |
| 218 | if not self.commit_hash: |
| 219 | raise ValueError("Commit must have a non-empty commit_hash.") |
| 220 | |
| 221 | if not is_commit(self.commit_hash): |
| 222 | raise ValueError(f"Commit must be a valid a commit_hash: {self.commit_hash}.") |
| 223 | |
| 224 | if not self.vcs_url: |
| 225 | raise ValueError("Commit must have a non-empty vcs_url.") |
| 226 | |
| 227 | if self.patch_text: |
| 228 | self.patch_checksum = compute_patch_checksum(self.patch_text) |
| 229 | |
| 230 | def __lt__(self, other): |
| 231 | if not isinstance(other, PackageCommitPatchData): |
| 232 | return NotImplemented |
| 233 | return self._cmp_key() < other._cmp_key() |
| 234 | |
| 235 | # TODO: Add cache |
| 236 | def _cmp_key(self): |
| 237 | return ( |
| 238 | self.vcs_url, |
| 239 | self.commit_hash, |
| 240 | self.patch_text, |
| 241 | self.patch_checksum, |
| 242 | ) |
| 243 | |
| 244 | def to_dict(self) -> dict: |
| 245 | """Return a normalized dictionary representation of the commit.""" |
| 246 | return { |
| 247 | "vcs_url": self.vcs_url, |
| 248 | "commit_hash": self.commit_hash, |
| 249 | "patch_text": self.patch_text, |
| 250 | "patch_checksum": self.patch_checksum, |
| 251 | } |
| 252 | |
| 253 | @classmethod |
| 254 | def from_dict(cls, data: dict): |
| 255 | """Create a PackageCommitPatchData instance from a dictionary.""" |
| 256 | return cls( |
| 257 | vcs_url=data.get("vcs_url"), |
| 258 | commit_hash=data.get("commit_hash"), |
| 259 | patch_text=data.get("patch_text"), |
| 260 | ) |
| 261 | |
| 262 | |
| 263 | @dataclasses.dataclass(eq=True) |
no outgoing calls