Interpret raw HEAD without requiring it to be attached or born.
(repository: &GitRepository)
| 585 | |
| 586 | /// Interpret raw HEAD without requiring it to be attached or born. |
| 587 | pub fn observe_head(repository: &GitRepository) -> Result<HeadObservation, ObservationError> { |
| 588 | let head = repository |
| 589 | .find_reference("HEAD") |
| 590 | .map_err(|error| ObservationError::new(format!("cannot read Git HEAD: {error}")))?; |
| 591 | |
| 592 | let symref = head |
| 593 | .symbolic_target() |
| 594 | .map_err(|error| ObservationError::new(format!("cannot read Git HEAD: {error}")))?; |
| 595 | if let Some(symref) = symref.map(str::to_string) { |
| 596 | match head.resolve() { |
| 597 | Ok(resolved) => { |
| 598 | let oid = resolved.target().ok_or_else(|| { |
| 599 | ObservationError::new(format!( |
| 600 | "Git HEAD target '{symref}' does not resolve directly to an object" |
| 601 | )) |
| 602 | })?; |
| 603 | Ok(HeadObservation::Attached { symref, oid }) |
| 604 | } |
| 605 | Err(error) if error.code() == ErrorCode::NotFound => { |
| 606 | let references = repository.references().map_err(|state_error| { |
| 607 | ObservationError::new(format!( |
| 608 | "cannot distinguish unborn Git HEAD from a missing branch: {state_error}" |
| 609 | )) |
| 610 | })?; |
| 611 | let mut has_resolved_ref = false; |
| 612 | for reference in references { |
| 613 | let reference = reference.map_err(|state_error| { |
| 614 | ObservationError::new(format!( |
| 615 | "cannot inspect Git refs while classifying HEAD: {state_error}" |
| 616 | )) |
| 617 | })?; |
| 618 | has_resolved_ref |= reference.target().is_some(); |
| 619 | } |
| 620 | if has_resolved_ref { |
| 621 | Ok(HeadObservation::MissingTarget { symref }) |
| 622 | } else { |
| 623 | Ok(HeadObservation::Unborn { symref }) |
| 624 | } |
| 625 | } |
| 626 | Err(error) if error.code() == ErrorCode::UnbornBranch => { |
| 627 | Ok(HeadObservation::Unborn { symref }) |
| 628 | } |
| 629 | Err(error) => Err(ObservationError::new(format!( |
| 630 | "cannot resolve Git HEAD target '{symref}': {error}" |
| 631 | ))), |
| 632 | } |
| 633 | } else if let Some(oid) = head.target() { |
| 634 | Ok(HeadObservation::Detached { oid }) |
| 635 | } else { |
| 636 | Err(ObservationError::new( |
| 637 | "Git HEAD is neither a symbolic reference nor a direct object ID", |
| 638 | )) |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /// Classify checkpoint eligibility using only supplied observations/evidence. |
| 643 | /// This function performs no I/O and cannot persist the provisional result. |
no test coverage detected