|
| 1 | +use bstr::BString; |
| 2 | +use but_graph::VirtualBranchesTomlMetadata; |
| 3 | +use std::collections::BTreeSet; |
| 4 | + |
| 5 | +/// A way to determine what should be included in the snapshot when calling [create_tree()](function::create_tree). |
| 6 | +#[derive(Debug, Clone)] |
| 7 | +pub struct State { |
| 8 | + /// The result of a previous worktree changes call. |
| 9 | + /// |
| 10 | + /// It contains detailed information about the complete set of possible changes to become part of the worktree. |
| 11 | + pub changes: but_core::WorktreeChanges, |
| 12 | + /// Repository-relative and slash-separated paths that match any change in the [`changes`](State::changes) field. |
| 13 | + /// It is *not* error if there is no match, as there can be snapshots without working tree changes, but with other changes. |
| 14 | + /// It's up to the caller to check for that via [`Outcome::is_empty()`]. |
| 15 | + pub selection: BTreeSet<BString>, |
| 16 | + /// If `true`, store the current `HEAD` reference, i.e. its target, as well as the targets of all refs it's pointing to by symbolic link. |
| 17 | + pub head: bool, |
| 18 | +} |
| 19 | + |
| 20 | +/// Contains all state that the snapshot contains. |
| 21 | +#[derive(Debug, Copy, Clone)] |
| 22 | +pub struct Outcome { |
| 23 | + /// The snapshot itself, with all the subtrees available that are also listed in this structure. |
| 24 | + pub snapshot_tree: gix::ObjectId, |
| 25 | + /// For good measure, the input `HEAD^{tree}` that is used as the basis to learn about worktree changes. |
| 26 | + pub head_tree: gix::ObjectId, |
| 27 | + /// The `head_tree` with the selected worktree changes applied, suitable for being stored in a commit, |
| 28 | + /// or `None` if there was no change in the worktree. |
| 29 | + pub worktree: Option<gix::ObjectId>, |
| 30 | + /// The tree representing the current changed index, without conflicts, or `None` if there was no change to the index. |
| 31 | + pub index: Option<gix::ObjectId>, |
| 32 | + /// A tree with files in a custom storage format to allow keeping conflicting blobs reachable, along with detailed conflict information |
| 33 | + /// to allow restoring the conflict entries in the index. |
| 34 | + pub index_conflicts: Option<gix::ObjectId>, |
| 35 | + /// The tree representing the reference targets of all references within the *workspace*. |
| 36 | + pub workspace_references: Option<gix::ObjectId>, |
| 37 | + /// The tree representing the reference targets of all references reachable from `HEAD`, so typically `HEAD` itself, and the |
| 38 | + /// target object of the reference it is pointing to. |
| 39 | + pub head_references: Option<gix::ObjectId>, |
| 40 | + /// The tree representing the metadata of all references within the *workspace*. |
| 41 | + pub metadata: Option<gix::ObjectId>, |
| 42 | +} |
| 43 | + |
| 44 | +impl Outcome { |
| 45 | + /// Return `true` if the snapshot contains no information whatsoever, which is equivalent to being an empty tree. |
| 46 | + pub fn is_empty(&self) -> bool { |
| 47 | + self.snapshot_tree.is_empty_tree() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// A utility to more easily use *no* workspace or metadata. |
| 52 | +pub fn no_workspace_and_meta() -> Option<( |
| 53 | + &'static but_graph::projection::Workspace<'static>, |
| 54 | + &'static VirtualBranchesTomlMetadata, |
| 55 | +)> { |
| 56 | + None |
| 57 | +} |
| 58 | + |
| 59 | +pub(super) mod function { |
| 60 | + use super::{Outcome, State}; |
| 61 | + use crate::{DiffSpec, commit_engine}; |
| 62 | + use anyhow::bail; |
| 63 | + use but_core::RefMetadata; |
| 64 | + use gix::object::tree::EntryKind; |
| 65 | + use tracing::instrument; |
| 66 | + |
| 67 | + /// Create a tree that represents the snapshot for the given `selection`, with the basis for everything |
| 68 | + /// being the `head_tree_id` *(i.e. the tree to which `HEAD` is ultimately pointing to)*. |
| 69 | + /// Make this an empty tree if the `HEAD` is unborn. |
| 70 | + /// |
| 71 | + /// If `workspace_and_meta` is not `None`, the workspace and metadata to store in the snapshot. |
| 72 | + /// We will only store reference positions, and assume that their commits are safely stored in the reflog to not |
| 73 | + /// be garbage collected. Metadata is only stored for the references that are included in the `workspace`. |
| 74 | + /// |
| 75 | + /// Note that objects will be written into the repository behind `head_tree_id` unless it's configured |
| 76 | + /// to keep everything in memory. |
| 77 | + /// |
| 78 | + /// ### Snapshot Tree Format |
| 79 | + /// |
| 80 | + /// There are the following top-level trees, with their own sub-formats which aren't specified here. |
| 81 | + /// However, it's notable that they have to be implemented so that they remain compatible to prior versions |
| 82 | + /// of the tree. |
| 83 | + /// |
| 84 | + /// Note that all top-level entries are optional, and only present if there is a snapshot to store. |
| 85 | + /// |
| 86 | + /// * `worktree` |
| 87 | + /// - the tree of `HEAD + uncommitted files`. Technically this means that now possibly untracked files are known to Git, |
| 88 | + /// even though it might be that the respective objects aren't written to disk yet. |
| 89 | + #[instrument(skip(changes, _workspace_and_meta), err(Debug))] |
| 90 | + pub fn create_tree( |
| 91 | + head_tree_id: gix::Id<'_>, |
| 92 | + State { |
| 93 | + changes, |
| 94 | + selection, |
| 95 | + head: _, |
| 96 | + }: State, |
| 97 | + _workspace_and_meta: Option<(&but_graph::projection::Workspace, &impl RefMetadata)>, |
| 98 | + ) -> anyhow::Result<Outcome> { |
| 99 | + let repo = head_tree_id.repo; |
| 100 | + // TODO: refactor tree-creation to not assume commits anymore. |
| 101 | + let mut changes_to_apply: Vec<_> = changes |
| 102 | + .changes |
| 103 | + .iter() |
| 104 | + .filter(|c| selection.contains(&c.path)) |
| 105 | + .map(|c| Ok(DiffSpec::from(c))) |
| 106 | + .collect(); |
| 107 | + let (new_tree, base_tree) = commit_engine::tree::apply_worktree_changes( |
| 108 | + head_tree_id.into(), |
| 109 | + repo, |
| 110 | + &mut changes_to_apply, |
| 111 | + 0, /* context lines don't matter */ |
| 112 | + )?; |
| 113 | + |
| 114 | + let rejected = changes_to_apply |
| 115 | + .into_iter() |
| 116 | + .filter_map(Result::err) |
| 117 | + .collect::<Vec<_>>(); |
| 118 | + if !rejected.is_empty() { |
| 119 | + bail!( |
| 120 | + "It should be impossible to fail to apply changes that are in the tree that was provided as HEAD^{{tree}} - {rejected:?}" |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + let mut edit = repo.empty_tree().edit()?; |
| 125 | + |
| 126 | + let worktree = (new_tree != base_tree).then_some(new_tree.detach()); |
| 127 | + if let Some(worktree) = worktree { |
| 128 | + edit.upsert("worktree", EntryKind::Tree, worktree)?; |
| 129 | + } |
| 130 | + |
| 131 | + Ok(Outcome { |
| 132 | + snapshot_tree: edit.write()?.into(), |
| 133 | + head_tree: head_tree_id.into(), |
| 134 | + worktree, |
| 135 | + index: None, |
| 136 | + index_conflicts: None, |
| 137 | + workspace_references: None, |
| 138 | + head_references: None, |
| 139 | + metadata: None, |
| 140 | + }) |
| 141 | + } |
| 142 | +} |
0 commit comments