dce0bf3feat: browse a repository's files and history8d | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::{fmt, time::SystemTime}; |
| 9 | |
1b7587dfeat: finding a string in a repository, and landing on the line18h | 10 | use super::{DomainError, RefName, RepoPath}; |
dce0bf3feat: browse a repository's files and history8d | 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 18 | pub struct ObjectId(String); |
| 19 | |
| 20 | impl ObjectId { |
| 21 | const SHA1_LEN: usize = 40; |
| 22 | const SHA256_LEN: usize = 64; |
| 23 | |
| 24 | |
| 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 | |
| 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 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | pub fn short(&self) -> &str { |
| 66 | &self.0[..Self::SHORT_LEN.min(self.0.len())] |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl fmt::Display for ObjectId { |
| 71 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 72 | f.write_str(&self.0) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 81 | pub enum EntryKind { |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | Tree, |
| 87 | |
| 88 | Blob, |
| 89 | |
| 90 | Symlink, |
| 91 | |
| 92 | Submodule, |
| 93 | } |
| 94 | |
| 95 | impl EntryKind { |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | pub fn from_mode(mode: &str) -> Result<Self, DomainError> { |
| 101 | |
| 102 | |
| 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 | |
| 130 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 131 | pub struct TreeEntry { |
| 132 | |
| 133 | pub name: String, |
| 134 | pub kind: EntryKind, |
| 135 | pub id: ObjectId, |
| 136 | |
| 137 | pub size: Option<u64>, |
| 138 | } |
| 139 | |
| 140 | impl TreeEntry { |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | pub fn ordering_key(&self) -> (EntryKind, String) { |
| 146 | (self.kind, self.name.to_lowercase()) |
| 147 | } |
| 148 | } |
| 149 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 156 | pub enum RefKind { |
| 157 | Branch, |
| 158 | Tag, |
| 159 | } |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 167 | pub struct GitRef { |
| 168 | pub name: RefName, |
| 169 | pub kind: RefKind, |
| 170 | } |
| 171 | |
dd5b600feat: the facts a repository page states about itself19h | 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 179 | pub struct TagSummary { |
| 180 | pub name: RefName, |
| 181 | pub created_at: SystemTime, |
| 182 | } |
| 183 | |
dce0bf3feat: browse a repository's files and history8d | 184 | |
| 185 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 186 | pub struct CommitSummary { |
| 187 | pub id: ObjectId, |
| 188 | |
| 189 | pub summary: String, |
| 190 | pub author_name: String, |
| 191 | pub committed_at: SystemTime, |
| 192 | } |
| 193 | |
3bbcd9ffeat: a branch and a tag are rows, not just names19h | 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 202 | pub struct BranchRow { |
| 203 | pub name: RefName, |
| 204 | |
| 205 | pub is_default: bool, |
| 206 | pub commit: ObjectId, |
| 207 | |
| 208 | pub summary: String, |
| 209 | pub committed_at: SystemTime, |
| 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 219 | pub struct TagRow { |
| 220 | pub name: RefName, |
| 221 | |
| 222 | |
| 223 | pub commit: ObjectId, |
| 224 | |
| 225 | |
| 226 | pub message: Option<String>, |
| 227 | pub annotated: bool, |
| 228 | |
| 229 | pub created_at: SystemTime, |
| 230 | } |
| 231 | |
1b7587dfeat: finding a string in a repository, and landing on the line18h | 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 237 | pub struct GrepHit { |
| 238 | pub path: RepoPath, |
| 239 | |
| 240 | pub line: u32, |
| 241 | |
| 242 | |
| 243 | |
| 244 | pub column: u32, |
| 245 | |
| 246 | |
| 247 | pub text: String, |
| 248 | } |
| 249 | |
2268309feat: a commit is a page, and two revisions can be compared18h | 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 261 | pub struct CommitDetail { |
| 262 | pub id: ObjectId, |
| 263 | |
| 264 | pub tree: ObjectId, |
| 265 | |
| 266 | pub parents: Vec<ObjectId>, |
| 267 | |
| 268 | pub summary: String, |
| 269 | |
| 270 | pub body: String, |
| 271 | pub author_name: String, |
| 272 | pub author_email: String, |
| 273 | pub authored_at: SystemTime, |
| 274 | pub committer_name: String, |
| 275 | pub committer_email: String, |
| 276 | pub committed_at: SystemTime, |
| 277 | } |
| 278 | |
| 279 | impl CommitDetail { |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | pub fn has_distinct_committer(&self) -> bool { |
| 285 | self.committer_name != self.author_name || self.committer_email != self.author_email |
| 286 | } |
| 287 | |
| 288 | |
| 289 | pub fn is_root(&self) -> bool { |
| 290 | self.parents.is_empty() |
| 291 | } |
| 292 | } |
| 293 | |
dce0bf3feat: browse a repository's files and history8d | 294 | #[cfg(test)] |
| 295 | mod tests { |
| 296 | use super::*; |
| 297 | |
| 298 | const SHA1: &str = "a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0"; |
| 299 | |
| 300 | #[test] |
| 301 | fn both_hash_widths_are_accepted() { |
| 302 | assert!(ObjectId::new(SHA1).is_ok()); |
| 303 | assert!(ObjectId::new("a".repeat(64)).is_ok()); |
| 304 | } |
| 305 | |
| 306 | #[test] |
| 307 | fn an_id_is_lowercased_so_two_spellings_compare_equal() { |
| 308 | assert_eq!( |
| 309 | ObjectId::new(SHA1.to_uppercase()).expect("valid"), |
| 310 | ObjectId::new(SHA1).expect("valid") |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | #[test] |
| 315 | fn a_wrong_width_or_non_hex_id_is_refused() { |
| 316 | for value in ["", "abc", &"a".repeat(39), &"a".repeat(41), &"g".repeat(40)] { |
| 317 | assert!(ObjectId::new(value).is_err(), "{value:?} should be refused"); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | #[test] |
| 322 | fn the_short_form_is_for_display_only() { |
| 323 | assert_eq!(ObjectId::new(SHA1).expect("valid").short(), "a1b2c3d"); |
| 324 | } |
| 325 | |
| 326 | #[test] |
| 327 | fn git_modes_map_to_kinds() { |
| 328 | for (mode, expected) in [ |
| 329 | ("040000", EntryKind::Tree), |
| 330 | ("40000", EntryKind::Tree), |
| 331 | ("100644", EntryKind::Blob), |
| 332 | ("100755", EntryKind::Blob), |
| 333 | ("120000", EntryKind::Symlink), |
| 334 | ("160000", EntryKind::Submodule), |
| 335 | ] { |
| 336 | assert_eq!( |
| 337 | EntryKind::from_mode(mode).expect("known mode"), |
| 338 | expected, |
| 339 | "mode {mode}" |
| 340 | ); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | #[test] |
| 345 | fn an_unknown_mode_is_an_error_not_a_default() { |
| 346 | |
| 347 | assert!(EntryKind::from_mode("100600").is_err()); |
| 348 | assert!(EntryKind::from_mode("").is_err()); |
| 349 | } |
| 350 | |
| 351 | #[test] |
| 352 | fn a_listing_sorts_directories_first_then_case_insensitively_by_name() { |
| 353 | let entry = |name: &str, kind| TreeEntry { |
| 354 | name: name.to_owned(), |
| 355 | kind, |
| 356 | id: ObjectId::new(SHA1).expect("valid"), |
| 357 | size: None, |
| 358 | }; |
| 359 | |
| 360 | let mut entries = [ |
| 361 | entry("README.md", EntryKind::Blob), |
| 362 | entry("src", EntryKind::Tree), |
| 363 | entry("Cargo.toml", EntryKind::Blob), |
| 364 | entry("migrations", EntryKind::Tree), |
| 365 | ]; |
| 366 | entries.sort_by_key(TreeEntry::ordering_key); |
| 367 | |
| 368 | assert_eq!( |
| 369 | entries.iter().map(|e| e.name.as_str()).collect::<Vec<_>>(), |
| 370 | vec!["migrations", "src", "Cargo.toml", "README.md"] |
| 371 | ); |
| 372 | } |
| 373 | } |