Resolve the (view, HEAD commit, HEAD tree) triple for the workspace with the §7.5/CB-8A detached alignment rule: an attached HEAD must name the desired view; a detached HEAD must project exactly to the desired view.
(
repo: &Repository,
working_copy: WorkingCopyId,
git: &GitRepository,
)
| 2692 | /// the §7.5/CB-8A detached alignment rule: an attached HEAD must name the |
| 2693 | /// desired view; a detached HEAD must project exactly to the desired view. |
| 2694 | pub(crate) fn workspace_head_aligned( |
| 2695 | repo: &Repository, |
| 2696 | working_copy: WorkingCopyId, |
| 2697 | git: &GitRepository, |
| 2698 | ) -> CliResult<(String, git2::Oid, git2::Oid)> { |
| 2699 | let head_obs = observe_head(git).map_err(observation_error)?; |
| 2700 | match &head_obs { |
| 2701 | HeadObservation::Attached { symref, .. } => { |
| 2702 | let branch = symref |
| 2703 | .strip_prefix("refs/heads/") |
| 2704 | .map(str::to_string) |
| 2705 | .ok_or_else(|| { |
| 2706 | git_error(format!("Git HEAD symref '{symref}' is not a local branch")) |
| 2707 | })?; |
| 2708 | let desired_view = repo |
| 2709 | .desired_view_name(working_copy) |
| 2710 | .map_err(CliError::from)?; |
| 2711 | if branch != desired_view { |
| 2712 | return Err(git_error(format!( |
| 2713 | "Git branch '{branch}' does not match current Atomic view '{desired_view}'" |
| 2714 | ))); |
| 2715 | } |
| 2716 | let head = git |
| 2717 | .head() |
| 2718 | .map_err(|error| git_error(format!("Git HEAD is unavailable: {error}")))?; |
| 2719 | let oid = head |
| 2720 | .target() |
| 2721 | .ok_or_else(|| git_error("Git HEAD does not point directly to a commit"))?; |
| 2722 | let tree = head |
| 2723 | .peel_to_commit() |
| 2724 | .and_then(|commit| commit.tree()) |
| 2725 | .map_err(|error| git_error(format!("cannot read Git HEAD tree: {error}")))?; |
| 2726 | Ok((branch, oid, tree.id())) |
| 2727 | } |
| 2728 | HeadObservation::Detached { oid } => { |
| 2729 | let desired_view = repo |
| 2730 | .desired_view_name(working_copy) |
| 2731 | .map_err(CliError::from)?; |
| 2732 | let commit = git.find_commit(*oid).map_err(|error| { |
| 2733 | git_error(format!("cannot read detached Git HEAD commit: {error}")) |
| 2734 | })?; |
| 2735 | let tree_id = commit.tree_id(); |
| 2736 | let policy = conversion_policy(git)?; |
| 2737 | let project = repo.project_tree(&desired_view, &policy).map_err(|error| { |
| 2738 | git_error(format!( |
| 2739 | "cannot project Atomic view '{desired_view}': {error}" |
| 2740 | )) |
| 2741 | })?; |
| 2742 | if project.git.root.as_bytes() != tree_id.as_bytes() { |
| 2743 | return Err(git_error(format!( |
| 2744 | "detached Git HEAD tree {tree_id} does not match the projection of view '{desired_view}'" |
| 2745 | ))); |
| 2746 | } |
| 2747 | Ok((desired_view, *oid, tree_id)) |
| 2748 | } |
| 2749 | HeadObservation::Unborn { symref } => { |
| 2750 | Err(git_error(format!("Git HEAD is unborn at '{symref}'"))) |
| 2751 | } |
no test coverage detected