Read a bounded tail of `hook-errors.log` and aggregate it. Returns `None` when the file is absent. A file that exists but cannot be read or parsed yields an empty report rather than an error, because the point of the report is to explain the situation.
(repo_root: &Path)
| 383 | /// read or parsed yields an empty report rather than an error, because the |
| 384 | /// point of the report is to explain the situation. |
| 385 | pub fn read_errors(repo_root: &Path) -> Option<HookErrors> { |
| 386 | let path = repo_root.join(".atomic").join("hook-errors.log"); |
| 387 | let meta = std::fs::metadata(&path).ok()?; |
| 388 | let file_len = meta.len(); |
| 389 | let mut file = std::fs::File::open(&path).ok()?; |
| 390 | |
| 391 | // Read only the tail; the file is unbounded and never rotated. |
| 392 | let start = file_len.saturating_sub(ERROR_LOG_TAIL_BYTES); |
| 393 | if start > 0 { |
| 394 | file.seek(SeekFrom::Start(start)).ok()?; |
| 395 | } |
| 396 | let mut buf = Vec::new(); |
| 397 | std::io::Read::read_to_end(&mut file, &mut buf).ok()?; |
| 398 | |
| 399 | let text = String::from_utf8_lossy(&buf); |
| 400 | // A tail seek can land mid-line, in which case the first line read is a |
| 401 | // fragment and must be dropped. When the whole file was read (start == |
| 402 | // 0) nothing is partial, so the first line is real and must be kept — |
| 403 | // dropping it would silently lose the oldest entry on every small log. |
| 404 | let body = if start > 0 { |
| 405 | match text.find('\n') { |
| 406 | Some(i) => &text[i + 1..], |
| 407 | None => "", |
| 408 | } |
| 409 | } else { |
| 410 | &text |
| 411 | }; |
| 412 | |
| 413 | let (entries, lines) = parse_error_log(body); |
| 414 | |
| 415 | Some(aggregate(&entries, lines, start)) |
| 416 | } |
| 417 | |
| 418 | /// Parse a log into entries, folding continuation lines into their entry. |
| 419 | /// |