steid

@jamesgill /

steid/src/domain/object.rs
Rust14.1 KBCode·Blame·History·Raw
1//! Git objects, as the domain sees them.
2//!
3//! Deliberately not a model of git: an id, what a tree entry is, and enough of a commit
4//! to list one. [0006](../../plans/decisions/0006-git-binary-behind-narrow-ports.md)
5//! rejected modelling git objects properly as premature, and it still is — this exists
6//! so a query port returns meaning rather than `String`s.
7
8use std::{fmt, time::SystemTime};
9
10use super::{DomainError, RefName, RepoPath};
11
12/// The id of a git object, hex-encoded.
13///
14/// Accepts both widths git uses: 40 characters for SHA-1 and 64 for SHA-256. Steid
15/// creates SHA-1 repositories today, and refusing the wider form would turn a
16/// repository created elsewhere unreadable rather than merely unsupported.
17#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
18pub struct ObjectId(String);
19
20impl ObjectId {
21 const SHA1_LEN: usize = 40;
22 const SHA256_LEN: usize = 64;
23
24 /// How much of an id to show. Seven is what git itself abbreviates to by default.
25 const SHORT_LEN: usize = 7;
26
27 pub fn new(value: impl AsRef<str>) -> Result<Self, DomainError> {
28 let value = value.as_ref().trim();
29
30 if value.len() != Self::SHA1_LEN && value.len() != Self::SHA256_LEN {
31 return Err(DomainError::validation(
32 "object id",
33 format!(
34 "an object id is {} or {} characters, got {}",
35 Self::SHA1_LEN,
36 Self::SHA256_LEN,
37 value.len()
38 ),
39 ));
40 }
41
42 if !value.chars().all(|c| c.is_ascii_hexdigit()) {
43 return Err(DomainError::validation(
44 "object id",
45 "an object id is hexadecimal",
46 ));
47 }
48
49 // Lowercased on the way in so two spellings of one id compare equal.
50 Ok(Self(value.to_ascii_lowercase()))
51 }
52
53 pub fn from_trusted(value: impl Into<String>) -> Self {
54 Self(value.into())
55 }
56
57 pub fn as_str(&self) -> &str {
58 &self.0
59 }
60
61 /// The abbreviated form, for display.
62 ///
63 /// Never for lookup: an abbreviation can become ambiguous as a repository grows,
64 /// which is exactly the bug that only appears once a repository is large.
65 pub fn short(&self) -> &str {
66 &self.0[..Self::SHORT_LEN.min(self.0.len())]
67 }
68}
69
70impl fmt::Display for ObjectId {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 f.write_str(&self.0)
73 }
74}
75
76/// What a tree entry is.
77///
78/// Git encodes this in a file mode, which is a POSIX mode only by resemblance — the
79/// meaningful values are a fixed set, so this is an enum rather than a bitfield.
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
81pub enum EntryKind {
82 /// A directory.
83 ///
84 /// Ordered first so a listing sorts directories above files without a custom
85 /// comparator at every call site.
86 Tree,
87 /// A file.
88 Blob,
89 /// A symbolic link. Its "content" is the target path.
90 Symlink,
91 /// Another repository, mounted as a submodule. Steid cannot look inside one.
92 Submodule,
93}
94
95impl EntryKind {
96 /// Reads git's mode field.
97 ///
98 /// Returns `Result`, never a default: an unknown mode silently becoming a file
99 /// would render a submodule as an empty blob.
100 pub fn from_mode(mode: &str) -> Result<Self, DomainError> {
101 // Trimmed because `ls-tree` pads the mode of a tree to six characters with a
102 // leading zero, while `cat-file` does not.
103 match mode.trim().trim_start_matches('0') {
104 "40000" => Ok(Self::Tree),
105 "100644" | "100755" => Ok(Self::Blob),
106 "120000" => Ok(Self::Symlink),
107 "160000" => Ok(Self::Submodule),
108 other => Err(DomainError::validation(
109 "mode",
110 format!("unknown git file mode {other:?}"),
111 )),
112 }
113 }
114
115 pub fn is_tree(self) -> bool {
116 self == Self::Tree
117 }
118
119 pub fn as_str(self) -> &'static str {
120 match self {
121 Self::Tree => "tree",
122 Self::Blob => "blob",
123 Self::Symlink => "symlink",
124 Self::Submodule => "submodule",
125 }
126 }
127}
128
129/// One entry in a directory listing.
130#[derive(Debug, Clone, PartialEq, Eq)]
131pub struct TreeEntry {
132 /// The entry's own name, never a path — the containing path is the caller's.
133 pub name: String,
134 pub kind: EntryKind,
135 pub id: ObjectId,
136 /// A blob's size in bytes. `None` for anything without one.
137 pub size: Option<u64>,
138}
139
140impl TreeEntry {
141 /// Orders a listing the way a file browser does: directories first, then by name.
142 ///
143 /// Case-insensitive, because a listing sorted by byte value puts every capitalised
144 /// name above every lowercase one, which reads as unsorted.
145 pub fn ordering_key(&self) -> (EntryKind, String) {
146 (self.kind, self.name.to_lowercase())
147 }
148}
149
150/// Whether a ref is a branch or a tag.
151///
152/// The two are told apart by which namespace the ref lives in, not by what it points
153/// at: a lightweight tag and a branch both point straight at a commit, so the object
154/// says nothing about which one a visitor asked for.
155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum RefKind {
157 Branch,
158 Tag,
159}
160
161/// A branch or a tag, short-named as a switcher shows it.
162///
163/// Short (`main`, not `refs/heads/main`) because that is what the browse URLs take and
164/// what a person recognises. The ambiguity a full name would resolve — a branch and a
165/// tag sharing a name — is carried by [`kind`](Self::kind) instead.
166#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct GitRef {
168 pub name: RefName,
169 pub kind: RefKind,
170}
171
172/// A tag and when it was made, for the "latest tag" a repository page states.
173///
174/// The time is git's `creatordate`: the tag's own date for an annotated tag, and the
175/// date of the commit it points at for a lightweight one. That is the only definition
176/// that gives both kinds a usable answer — a lightweight tag has no date of its own,
177/// and reporting nothing for one would make the newest tag look older than it is.
178#[derive(Debug, Clone, PartialEq, Eq)]
179pub struct TagSummary {
180 pub name: RefName,
181 pub created_at: SystemTime,
182}
183
184/// A commit, reduced to what a log entry shows.
185#[derive(Debug, Clone, PartialEq, Eq)]
186pub struct CommitSummary {
187 pub id: ObjectId,
188 /// The first line of the message.
189 pub summary: String,
190 pub author_name: String,
191 pub committed_at: SystemTime,
192}
193
194/// The commit that last touched one entry in a directory listing.
195///
196/// Narrower than [`CommitSummary`] because the column it fills is narrower: a summary
197/// truncated to one line, a relative date, and a link to the commit. The author is
198/// deliberately absent — a per-row author on a single-user instance is a column of the
199/// same name repeated, and the commit page is one click away.
200#[derive(Debug, Clone, PartialEq, Eq)]
201pub struct EntryCommit {
202 pub id: ObjectId,
203 pub summary: String,
204 pub committed_at: SystemTime,
205}
206
207/// A branch, with everything one row of the branches page shows.
208///
209/// The commit fields are the branch tip's, read in the same `for-each-ref` that named
210/// the branch — a page listing thirty branches cannot afford a `git log` each.
211///
212/// `is_default` comes from git's own `%(HEAD)` marker rather than from a second
213/// `symbolic-ref` call, which is what keeps the whole page to one process.
214#[derive(Debug, Clone, PartialEq, Eq)]
215pub struct BranchRow {
216 pub name: RefName,
217 /// Whether `HEAD` points at this branch: the repository's default.
218 pub is_default: bool,
219 pub commit: ObjectId,
220 /// The first line of the tip commit's message.
221 pub summary: String,
222 pub committed_at: SystemTime,
223}
224
225/// A tag, with everything one row of the tags page shows.
226///
227/// An annotated tag is a git object of its own carrying a message and a date; a
228/// lightweight tag is just a name for a commit. Both are here, and
229/// [`annotated`](Self::annotated) is what tells them apart — not the presence of a
230/// message, because an annotated tag may have an empty one.
231#[derive(Debug, Clone, PartialEq, Eq)]
232pub struct TagRow {
233 pub name: RefName,
234 /// The commit the tag names, peeled through the tag object when there is one, so
235 /// both kinds of tag report the thing a visitor would browse.
236 pub commit: ObjectId,
237 /// The first line of an annotated tag's own message. `None` for a lightweight tag,
238 /// which has none — the commit's subject is the commit's, not the tag's.
239 pub message: Option<String>,
240 pub annotated: bool,
241 /// git's `creatordate`: the tag's own date when it has one, the commit's otherwise.
242 pub created_at: SystemTime,
243}
244
245/// One matching line, as `git grep` reports it.
246///
247/// Carries the whole line rather than the matched fragment: a line can hold more than
248/// one match, and deciding which parts to mark is the page's job, not the port's.
249#[derive(Debug, Clone, PartialEq, Eq)]
250pub struct GrepHit {
251 pub path: RepoPath,
252 /// 1-based, the way git counts and the way a `#L…` anchor spells it.
253 pub line: u32,
254 /// 1-based, at the first match on the line. Not used for rendering — the whole
255 /// line is re-scanned for every occurrence — but it is what git was asked for and
256 /// it is the answer to "where", which nothing else here holds.
257 pub column: u32,
258 /// The line itself, without its newline, and cut if it is absurdly long — see
259 /// `application::search`.
260 pub text: String,
261}
262
263/// A commit, in the detail a commit page shows.
264///
265/// Separate from [`CommitSummary`] rather than an extension of it: a log reads fifty
266/// commits and wants four fields each, while a commit page reads one and wants all of
267/// them. Asking git for the wider format fifty times over would be paid on the page
268/// that can least afford it.
269///
270/// The author and the committer are kept apart because they genuinely differ — a
271/// rebase, a patch applied by a maintainer, a cherry-pick — and a page that showed only
272/// one would quietly misattribute all three.
273#[derive(Debug, Clone, PartialEq, Eq)]
274pub struct CommitDetail {
275 pub id: ObjectId,
276 /// The tree this commit points at, so a page can link to browsing it.
277 pub tree: ObjectId,
278 /// Every parent, in git's order. Empty for a root commit, two or more for a merge.
279 pub parents: Vec<ObjectId>,
280 /// The first line of the message.
281 pub summary: String,
282 /// Everything after the first paragraph break, verbatim. Empty when there is none.
283 pub body: String,
284 pub author_name: String,
285 pub author_email: String,
286 pub authored_at: SystemTime,
287 pub committer_name: String,
288 pub committer_email: String,
289 pub committed_at: SystemTime,
290}
291
292impl CommitDetail {
293 /// Whether the commit was recorded by someone other than the person who wrote it.
294 ///
295 /// The one question a page asks before deciding to name the committer at all:
296 /// showing "Ada authored and Ada committed" on every ordinary commit is noise.
297 pub fn has_distinct_committer(&self) -> bool {
298 self.committer_name != self.author_name || self.committer_email != self.author_email
299 }
300
301 /// Whether this commit has no parent — the first commit in a history.
302 pub fn is_root(&self) -> bool {
303 self.parents.is_empty()
304 }
305}
306
307/// A commit as a feed entry needs it: the summary line plus the message body.
308///
309/// Not a field on [`CommitSummary`], and deliberately: a log page renders fifty
310/// one-line rows and has no use for fifty bodies, so the body is asked for only by the
311/// one caller that shows it. Composition rather than a wider struct keeps the two
312/// readings of a log honestly different.
313#[derive(Debug, Clone, PartialEq, Eq)]
314pub struct CommitEntry {
315 pub commit: CommitSummary,
316 /// Everything after the subject line, verbatim. Empty when the message is one line.
317 pub body: String,
318}
319
320#[cfg(test)]
321mod tests {
322 use super::*;
323
324 const SHA1: &str = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0";
325
326 #[test]
327 fn both_hash_widths_are_accepted() {
328 assert!(ObjectId::new(SHA1).is_ok());
329 assert!(ObjectId::new("a".repeat(64)).is_ok());
330 }
331
332 #[test]
333 fn an_id_is_lowercased_so_two_spellings_compare_equal() {
334 assert_eq!(
335 ObjectId::new(SHA1.to_uppercase()).expect("valid"),
336 ObjectId::new(SHA1).expect("valid")
337 );
338 }
339
340 #[test]
341 fn a_wrong_width_or_non_hex_id_is_refused() {
342 for value in ["", "abc", &"a".repeat(39), &"a".repeat(41), &"g".repeat(40)] {
343 assert!(ObjectId::new(value).is_err(), "{value:?} should be refused");
344 }
345 }
346
347 #[test]
348 fn the_short_form_is_for_display_only() {
349 assert_eq!(ObjectId::new(SHA1).expect("valid").short(), "a1b2c3d");
350 }
351
352 #[test]
353 fn git_modes_map_to_kinds() {
354 for (mode, expected) in [
355 ("040000", EntryKind::Tree),
356 ("40000", EntryKind::Tree),
357 ("100644", EntryKind::Blob),
358 ("100755", EntryKind::Blob),
359 ("120000", EntryKind::Symlink),
360 ("160000", EntryKind::Submodule),
361 ] {
362 assert_eq!(
363 EntryKind::from_mode(mode).expect("known mode"),
364 expected,
365 "mode {mode}"
366 );
367 }
368 }
369
370 #[test]
371 fn an_unknown_mode_is_an_error_not_a_default() {
372 // Defaulting would render a submodule as an empty file.
373 assert!(EntryKind::from_mode("100600").is_err());
374 assert!(EntryKind::from_mode("").is_err());
375 }
376
377 #[test]
378 fn a_listing_sorts_directories_first_then_case_insensitively_by_name() {
379 let entry = |name: &str, kind| TreeEntry {
380 name: name.to_owned(),
381 kind,
382 id: ObjectId::new(SHA1).expect("valid"),
383 size: None,
384 };
385
386 let mut entries = [
387 entry("README.md", EntryKind::Blob),
388 entry("src", EntryKind::Tree),
389 entry("Cargo.toml", EntryKind::Blob),
390 entry("migrations", EntryKind::Tree),
391 ];
392 entries.sort_by_key(TreeEntry::ordering_key);
393
394 assert_eq!(
395 entries.iter().map(|e| e.name.as_str()).collect::<Vec<_>>(),
396 vec!["migrations", "src", "Cargo.toml", "README.md"]
397 );
398 }
399}