| 1 | |
| 2 | |
| 3 | use crate::{ |
| 4 | application::port::GitQueryError, |
| 5 | domain::{BranchRow, GitRef, ObjectId, RefKind, RefName, TagRow, TagSummary}, |
| 6 | }; |
| 7 | |
| 8 | use super::unix_time; |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | pub(super) const REF_FORMAT: &str = "--format=%(refname)%00"; |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | pub(super) const SNAPSHOT_FORMAT: &str = "--format=%(refname)%00%(objectname)%00"; |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | pub(super) fn parse_ref_snapshot(stdout: &[u8]) -> Vec<(RefName, ObjectId)> { |
| 31 | let fields = ref_fields(stdout); |
| 32 | let mut snapshot = Vec::with_capacity(fields.len() / 2); |
| 33 | |
| 34 | for pair in fields.chunks_exact(2) { |
| 35 | |
| 36 | |
| 37 | |
| 38 | let Ok(full) = std::str::from_utf8(pair[0].trim_ascii()) else { |
| 39 | continue; |
| 40 | }; |
| 41 | let Ok(id) = std::str::from_utf8(pair[1].trim_ascii()) else { |
| 42 | continue; |
| 43 | }; |
| 44 | |
| 45 | let (Ok(name), Ok(id)) = (RefName::new(full), ObjectId::new(id)) else { |
| 46 | continue; |
| 47 | }; |
| 48 | |
| 49 | snapshot.push((name, id)); |
| 50 | } |
| 51 | |
| 52 | snapshot |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | pub(super) fn parse_refs(stdout: &[u8]) -> Vec<GitRef> { |
| 62 | let mut refs = Vec::new(); |
| 63 | |
| 64 | for record in stdout.split(|byte| *byte == 0) { |
| 65 | let record = record.trim_ascii(); |
| 66 | |
| 67 | if record.is_empty() { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | let Ok(full) = std::str::from_utf8(record) else { |
| 75 | continue; |
| 76 | }; |
| 77 | |
| 78 | let (kind, short) = if let Some(short) = full.strip_prefix("refs/heads/") { |
| 79 | (RefKind::Branch, short) |
| 80 | } else if let Some(short) = full.strip_prefix("refs/tags/") { |
| 81 | (RefKind::Tag, short) |
| 82 | } else { |
| 83 | |
| 84 | |
| 85 | |
| 86 | continue; |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | let Ok(name) = RefName::new(short) else { |
| 93 | continue; |
| 94 | }; |
| 95 | |
| 96 | refs.push(GitRef { name, kind }); |
| 97 | } |
| 98 | |
| 99 | refs |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | pub(super) const TAG_FORMAT: &str = "--format=%(refname)%00%(creatordate:unix)%00"; |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | pub(super) fn parse_latest_tag(stdout: &[u8]) -> Option<TagSummary> { |
| 116 | let fields: Vec<&[u8]> = stdout |
| 117 | .split(|byte| *byte == 0) |
| 118 | .map(<[u8]>::trim_ascii) |
| 119 | .filter(|field| !field.is_empty()) |
| 120 | .collect(); |
| 121 | |
| 122 | let [name, created_at] = fields[..] else { |
| 123 | return None; |
| 124 | }; |
| 125 | |
| 126 | let short = std::str::from_utf8(name).ok()?.strip_prefix("refs/tags/")?; |
| 127 | let created_at: i64 = std::str::from_utf8(created_at).ok()?.parse().ok()?; |
| 128 | |
| 129 | Some(TagSummary { |
| 130 | |
| 131 | |
| 132 | name: RefName::new(short).ok()?, |
| 133 | created_at: unix_time(created_at), |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | pub(super) const BRANCH_FORMAT: &str = "--format=%(refname)%00%(HEAD)%00%(objectname)%00%(committerdate:unix)%00%(contents:subject)%00"; |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | pub(super) const TAG_ROW_FORMAT: &str = "--format=%(refname)%00%(objecttype)%00%(objectname)%00%(*objectname)%00%(creatordate:unix)%00%(contents:subject)%00"; |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | fn ref_fields(stdout: &[u8]) -> Vec<&[u8]> { |
| 167 | let mut fields: Vec<&[u8]> = stdout.split(|byte| *byte == 0).collect(); |
| 168 | fields.pop(); |
| 169 | fields |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | fn subject(field: &[u8]) -> Option<String> { |
| 177 | let line = String::from_utf8_lossy(field) |
| 178 | .lines() |
| 179 | .next() |
| 180 | .unwrap_or_default() |
| 181 | .trim() |
| 182 | .to_owned(); |
| 183 | |
| 184 | (!line.is_empty()).then_some(line) |
| 185 | } |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | pub(super) fn parse_branches(stdout: &[u8]) -> Result<Vec<BranchRow>, GitQueryError> { |
| 195 | let fields = ref_fields(stdout); |
| 196 | let mut rows = Vec::with_capacity(fields.len() / 5); |
| 197 | |
| 198 | for record in fields.chunks(5) { |
| 199 | let [name, head, commit, committed_at, summary] = record[..] else { |
| 200 | return Err(GitQueryError::new( |
| 201 | "git listed a branch with missing fields", |
| 202 | )); |
| 203 | }; |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | let Some(name) = short_ref(name.trim_ascii(), "refs/heads/") else { |
| 209 | continue; |
| 210 | }; |
| 211 | |
| 212 | let Ok(commit) = ObjectId::new(String::from_utf8_lossy(commit).trim()) else { |
| 213 | continue; |
| 214 | }; |
| 215 | |
| 216 | let committed_at = String::from_utf8_lossy(committed_at); |
| 217 | let Ok(committed_at) = committed_at.trim().parse::<i64>() else { |
| 218 | continue; |
| 219 | }; |
| 220 | |
| 221 | rows.push(BranchRow { |
| 222 | name, |
| 223 | |
| 224 | is_default: head.trim_ascii() == b"*", |
| 225 | commit, |
| 226 | summary: subject(summary).unwrap_or_default(), |
| 227 | committed_at: unix_time(committed_at), |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | Ok(rows) |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | pub(super) fn parse_tags(stdout: &[u8]) -> Result<Vec<TagRow>, GitQueryError> { |
| 238 | let fields = ref_fields(stdout); |
| 239 | let mut rows = Vec::with_capacity(fields.len() / 6); |
| 240 | |
| 241 | for record in fields.chunks(6) { |
| 242 | let [name, kind, object, peeled, created_at, message] = record[..] else { |
| 243 | return Err(GitQueryError::new("git listed a tag with missing fields")); |
| 244 | }; |
| 245 | |
| 246 | let Some(name) = short_ref(name.trim_ascii(), "refs/tags/") else { |
| 247 | continue; |
| 248 | }; |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | let annotated = kind.trim_ascii() == b"tag"; |
| 254 | let id = if peeled.trim_ascii().is_empty() { |
| 255 | object |
| 256 | } else { |
| 257 | peeled |
| 258 | }; |
| 259 | |
| 260 | let Ok(commit) = ObjectId::new(String::from_utf8_lossy(id).trim()) else { |
| 261 | continue; |
| 262 | }; |
| 263 | |
| 264 | let created_at = String::from_utf8_lossy(created_at); |
| 265 | let Ok(created_at) = created_at.trim().parse::<i64>() else { |
| 266 | continue; |
| 267 | }; |
| 268 | |
| 269 | rows.push(TagRow { |
| 270 | name, |
| 271 | commit, |
| 272 | |
| 273 | |
| 274 | message: annotated.then(|| subject(message)).flatten(), |
| 275 | annotated, |
| 276 | created_at: unix_time(created_at), |
| 277 | }); |
| 278 | } |
| 279 | |
| 280 | Ok(rows) |
| 281 | } |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | fn short_ref(full: &[u8], namespace: &str) -> Option<RefName> { |
| 289 | let full = std::str::from_utf8(full).ok()?; |
| 290 | RefName::new(full.strip_prefix(namespace)?).ok() |
| 291 | } |
| 292 | |
| 293 | #[cfg(test)] |
| 294 | mod tests { |
| 295 | use super::*; |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | #[test] |
| 303 | fn refs_are_parsed_from_nul_terminated_records() { |
| 304 | |
| 305 | |
| 306 | |
| 307 | let stdout = b"refs/heads/main\0\nrefs/tags/v1.0\0\nrefs/pull/7/head\0\n"; |
| 308 | let refs = parse_refs(stdout); |
| 309 | |
| 310 | assert_eq!(refs.len(), 2); |
| 311 | assert_eq!(refs[0].name.as_str(), "main"); |
| 312 | assert_eq!(refs[0].kind, RefKind::Branch); |
| 313 | assert_eq!(refs[1].name.as_str(), "v1.0"); |
| 314 | assert_eq!(refs[1].kind, RefKind::Tag); |
| 315 | } |
| 316 | |
| 317 | #[test] |
| 318 | fn nothing_is_parsed_from_an_empty_listing() { |
| 319 | assert!(parse_refs(b"").is_empty()); |
| 320 | } |
| 321 | |
| 322 | #[test] |
| 323 | fn a_tag_record_is_parsed_past_the_trailing_newline() { |
| 324 | |
| 325 | |
| 326 | let tag = parse_latest_tag(b"refs/tags/v1.0\x001700000000\x00\n").expect("a tag"); |
| 327 | |
| 328 | assert_eq!(tag.name.as_str(), "v1.0"); |
| 329 | assert_eq!(tag.created_at, unix_time(1_700_000_000)); |
| 330 | } |
| 331 | |
| 332 | #[test] |
| 333 | fn nothing_is_parsed_from_an_empty_tag_listing() { |
| 334 | assert_eq!(parse_latest_tag(b""), None); |
| 335 | |
| 336 | assert_eq!( |
| 337 | parse_latest_tag(b"refs/heads/main\x001700000000\x00\n"), |
| 338 | None |
| 339 | ); |
| 340 | } |
| 341 | |
| 342 | #[test] |
| 343 | fn branch_records_survive_the_newline_git_puts_between_them() { |
| 344 | let rows = parse_branches( |
| 345 | b"refs/heads/main\x00*\x00aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\x001700000000\x00first\x00\nrefs/heads/side\x00 \x00bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\x001700000100\x00second\x00\n", |
| 346 | ) |
| 347 | .expect("should parse"); |
| 348 | |
| 349 | assert_eq!(rows.len(), 2); |
| 350 | assert!(rows[0].is_default); |
| 351 | assert_eq!(rows[0].summary, "first"); |
| 352 | |
| 353 | |
| 354 | assert_eq!(rows[1].name.as_str(), "side"); |
| 355 | assert!(!rows[1].is_default); |
| 356 | assert_eq!(rows[1].committed_at, unix_time(1_700_000_100)); |
| 357 | } |
| 358 | |
| 359 | #[test] |
| 360 | fn a_lightweight_tags_empty_peel_does_not_shift_the_fields_after_it() { |
| 361 | |
| 362 | |
| 363 | let rows = parse_tags( |
| 364 | b"refs/tags/v1.0\x00commit\x00aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\x00\x001700000000\x00a commit subject\x00\n", |
| 365 | ) |
| 366 | .expect("should parse"); |
| 367 | |
| 368 | assert_eq!(rows.len(), 1); |
| 369 | assert_eq!(rows[0].name.as_str(), "v1.0"); |
| 370 | assert_eq!(rows[0].commit.as_str(), "a".repeat(40)); |
| 371 | assert!(!rows[0].annotated); |
| 372 | assert_eq!(rows[0].message, None); |
| 373 | assert_eq!(rows[0].created_at, unix_time(1_700_000_000)); |
| 374 | } |
| 375 | |
| 376 | #[test] |
| 377 | fn nothing_is_parsed_from_an_empty_row_listing() { |
| 378 | assert_eq!(parse_branches(b"").expect("should parse"), Vec::new()); |
| 379 | assert_eq!(parse_tags(b"").expect("should parse"), Vec::new()); |
| 380 | } |
| 381 | |
| 382 | #[test] |
| 383 | fn a_record_with_the_wrong_number_of_fields_is_a_fault() { |
| 384 | |
| 385 | assert!(parse_branches(b"refs/heads/main\x00*\x00\n").is_err()); |
| 386 | } |
| 387 | } |