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,
)
| 2736 | /// the §7.5/CB-8A detached alignment rule: an attached HEAD must name the |
| 2737 | /// desired view; a detached HEAD must project exactly to the desired view. |
| 2738 | pub(crate) fn workspace_head_aligned( |
| 2739 | repo: &Repository, |
| 2740 | working_copy: WorkingCopyId, |
| 2741 | git: &GitRepository, |
| 2742 | ) -> CliResult<(String, git2::Oid, git2::Oid)> { |
| 2743 | let head_obs = observe_head(git).map_err(observation_error)?; |
| 2744 | match &head_obs { |
| 2745 | HeadObservation::Attached { symref, .. } => { |
| 2746 | let branch = symref |
| 2747 | .strip_prefix("refs/heads/") |
| 2748 | .map(str::to_string) |
| 2749 | .ok_or_else(|| { |
| 2750 | git_error(format!("Git HEAD symref '{symref}' is not a local branch")) |
| 2751 | })?; |
| 2752 | let desired_view = repo |
| 2753 | .desired_view_name(working_copy) |
| 2754 | .map_err(CliError::from)?; |
| 2755 | if branch != desired_view { |
| 2756 | return Err(git_error(format!( |
| 2757 | "Git branch '{branch}' does not match current Atomic view '{desired_view}'" |
| 2758 | ))); |
| 2759 | } |
| 2760 | let head = git |
| 2761 | .head() |
| 2762 | .map_err(|error| git_error(format!("Git HEAD is unavailable: {error}")))?; |
| 2763 | let oid = head |
| 2764 | .target() |
| 2765 | .ok_or_else(|| git_error("Git HEAD does not point directly to a commit"))?; |
| 2766 | let tree = head |
| 2767 | .peel_to_commit() |
| 2768 | .and_then(|commit| commit.tree()) |
| 2769 | .map_err(|error| git_error(format!("cannot read Git HEAD tree: {error}")))?; |
| 2770 | Ok((branch, oid, tree.id())) |
| 2771 | } |
| 2772 | HeadObservation::Detached { oid } => { |
| 2773 | let desired_view = repo |
| 2774 | .desired_view_name(working_copy) |
| 2775 | .map_err(CliError::from)?; |
| 2776 | let commit = git.find_commit(*oid).map_err(|error| { |
| 2777 | git_error(format!("cannot read detached Git HEAD commit: {error}")) |
| 2778 | })?; |
| 2779 | let tree_id = commit.tree_id(); |
| 2780 | let policy = conversion_policy(git)?; |
| 2781 | let project = repo.project_tree(&desired_view, &policy).map_err(|error| { |
| 2782 | git_error(format!( |
| 2783 | "cannot project Atomic view '{desired_view}': {error}" |
| 2784 | )) |
| 2785 | })?; |
| 2786 | if project.git.root.as_bytes() != tree_id.as_bytes() { |
| 2787 | return Err(git_error(format!( |
| 2788 | "detached Git HEAD tree {tree_id} does not match the projection of view '{desired_view}'" |
| 2789 | ))); |
| 2790 | } |
| 2791 | Ok((desired_view, *oid, tree_id)) |
| 2792 | } |
| 2793 | HeadObservation::Unborn { symref } => { |
| 2794 | Err(git_error(format!("Git HEAD is unborn at '{symref}'"))) |
| 2795 | } |
no test coverage detected