1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
"""Pydantic models of git objects.
Warning
--------
Pydantic objects defined in this module are documented `here
<../pydantic_models.html#git-objects>`_.
"""
from __future__ import annotations
import abc
from enum import Enum
from typing import ClassVar, Optional, cast
from pydantic import BaseModel, ConfigDict, Field, computed_field
from .constants import DictStrStr
GitCommitRawDataType = dict[str, str | list[str]]
"""
Type of the data associated with a git commit object.
value ``str`` is for the tree associated with a commit
value ``list[str]`` is for the parents (there can be 0, 1 or many).
"""
#: Type of raw data associated with a git tree object
GitTreeRawDataType = list[DictStrStr]
#: Type of raw data associated with a git tag object
GitTagRawDataType = DictStrStr
class GitObjectKind(str, Enum):
"""Git object kind/type."""
blob = "blob"
tree = "tree"
commit = "commit"
tag = "tag"
class GitObject(BaseModel, abc.ABC):
"""A base class for git objects."""
model_config = ConfigDict(extra="forbid")
@property
@abc.abstractmethod
def kind(self) -> GitObjectKind:
"""The object type."""
@computed_field(repr=True)
def is_ready(self) -> bool:
"""Indicates whether the object is ready to use.
Note
-----
See note in :func:`~git_dag.git_repository.GitInspector.get_raw_objects`.
"""
return self._is_ready
# https://docs.pydantic.dev/2.0/usage/computed_fields/
@is_ready.setter # type: ignore[no-redef]
def is_ready(self, ready: bool) -> None:
self._is_ready = ready
sha: str
_is_ready: bool = False
class GitBlob(GitObject):
"""Git blob object."""
model_config = ConfigDict(extra="forbid")
kind: ClassVar[GitObjectKind] = GitObjectKind.blob
_is_ready: bool = True
class GitTag(GitObject):
"""Git (annotated) tag object."""
model_config = ConfigDict(extra="forbid")
kind: ClassVar[GitObjectKind] = GitObjectKind.tag
name: str
raw_data: GitTagRawDataType = Field(repr=False)
# I keep track of deleted (annotated) tags that haven't been garbage-collected
is_deleted: bool = False
_anchor: GitObject
@property
def anchor(self) -> GitObject:
"""Return the associated anchor.
Note
-----
An annotated tag can point to another tag: https://stackoverflow.com/a/19812276
"""
return self._anchor
@anchor.setter
def anchor(self, anchor: GitObject) -> None:
self._anchor = anchor
@property
def tagger(self) -> str:
"""Return tagger."""
return self.raw_data["taggername"]
@property
def tagger_email(self) -> str:
"""Return tagger email."""
return self.raw_data["taggeremail"]
@property
def tagger_date(self) -> str:
"""Return tagger date."""
return self.raw_data["taggerdate"]
@property
def message(self) -> str:
"""Return the message."""
return self.raw_data["message"]
class GitCommit(GitObject):
"""Git commit object."""
model_config = ConfigDict(extra="forbid")
kind: ClassVar[GitObjectKind] = GitObjectKind.commit
is_reachable: bool
raw_data: GitCommitRawDataType = Field(repr=False)
_tree: GitTree
_parents: list[GitCommit]
@property
def tree(self) -> GitTree:
"""Return the associated tree (there can be exactly one)."""
return self._tree
@tree.setter
def tree(self, tree: GitTree) -> None:
self._tree = tree
@property
def parents(self) -> list[GitCommit]:
"""Return the parents."""
return self._parents
@parents.setter
def parents(self, parents: list[GitCommit]) -> None:
self._parents = parents
@property
def author(self) -> str:
"""Return the author."""
return cast(str, self.raw_data["author"])
@property
def author_email(self) -> str:
"""Return the author email."""
return cast(str, self.raw_data["author_email"])
@property
def author_date(self) -> str:
"""Return the author date."""
return cast(str, self.raw_data["author_date"])
@property
def committer(self) -> str:
"""Return the committer."""
return cast(str, self.raw_data["committer"])
@property
def committer_email(self) -> str:
"""Return the committer email."""
return cast(str, self.raw_data["committer_email"])
@property
def committer_date(self) -> str:
"""Return the committer date."""
return cast(str, self.raw_data["committer_date"])
@property
def message(self) -> str:
"""Return the commit message."""
return cast(str, self.raw_data["message"])
class GitTree(GitObject):
"""Git tree object."""
model_config = ConfigDict(extra="forbid")
kind: ClassVar[GitObjectKind] = GitObjectKind.tree
#: Raw data.
raw_data: GitTreeRawDataType = Field(repr=False)
#: Child trees and blobs.
_children: list[GitTree | GitBlob]
# Set to True when it is known apriory that there would be no children
# e.g., for the empty git tree object
no_children: bool = False
@property
def children(self) -> list[GitTree | GitBlob]:
"""Return the children."""
if self.no_children:
return []
return self._children
@children.setter
def children(self, children: list[GitTree | GitBlob]) -> None:
if self.no_children and children:
raise TypeError("Attempting to set children when there should be none.")
self._children = children
class GitTagLightweight(BaseModel):
"""Git lightweight tag (this is not a ``GitObject``)."""
model_config = ConfigDict(extra="forbid")
name: str
anchor: GitObject
class GitBranch(BaseModel):
"""A branch."""
model_config = ConfigDict(extra="forbid")
name: str
commit: GitCommit
is_local: bool = False
tracking: Optional[str] = None
class GitStash(BaseModel):
"""A stash."""
model_config = ConfigDict(extra="forbid")
index: int
title: str
commit: GitCommit
class GitHead(BaseModel):
"""A head (local or remote)."""
model_config = ConfigDict(extra="forbid")
commit: Optional[GitCommit] = None
branch: Optional[GitBranch] = None
@property
def is_defined(self) -> bool:
"""Is the HEAD defined."""
return self.commit is not None
@property
def is_detached(self) -> bool:
"""Is the HEAD detached."""
return self.branch is None
def __repr__(self) -> str:
if not self.is_defined:
return "None"
if self.is_detached:
return "DETACHED"
# type narrowing to make mypy happy
assert (self.commit is not None) and (self.branch is not None)
return f"{self.commit.sha} ({self.branch.name})"
|