| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Landing a stack of changesets made somewhere else: what a hub does when |
| 5 | //! a client asks it to land a workspace's changesets. |
| 6 | |
| 7 | use braid_engine::read_changeset; |
| 8 | use braid_format::ObjectId; |
| 9 | use braid_repo::Repo; |
| 10 | use braid_store::{RefUpdate, TransactError}; |
| 11 | |
| 12 | use crate::error::Error; |
| 13 | use crate::identity::Identity; |
| 14 | use crate::rebase::{First, Replayed, replay}; |
| 15 | use crate::workspace::{LandReport, Landed, Placed, describe_landing, unplaced}; |
| 16 | |
| 17 | /// How [`land_stack`] puts a stack on a branch. |
| 18 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 19 | pub enum StackMode { |
| 20 | /// Fast-forward when the first changeset's base is the head, and rebase |
| 21 | /// the stack onto the head otherwise, as a workspace's landing does. |
| 22 | #[default] |
| 23 | Rebase, |
| 24 | /// Fast-forward only: the branch moves when the head is the first |
| 25 | /// changeset's first parent, or one of the changesets; otherwise the |
| 26 | /// landing is refused and the branch stays where it is. |
| 27 | FastForwardOnly, |
| 28 | } |
| 29 | |
| 30 | /// Lands `stack`, oldest first, on `branch` of `repo` in one op log |
| 31 | /// transaction, as [`Workspace::land`](crate::Workspace::land) lands a |
| 32 | /// workspace's changesets. Each changeset's first parent must be the one |
| 33 | /// before it. |
| 34 | /// |
| 35 | /// On a branch that does not exist yet, the branch is created at the last |
| 36 | /// changeset. Otherwise [`StackMode::Rebase`] fast-forwards or rebases the |
| 37 | /// stack onto the head, failing with [`Error::NotOnBranch`] when the first |
| 38 | /// changeset's base is not on the head's first-parent chain, and |
| 39 | /// [`StackMode::FastForwardOnly`] fast-forwards or answers |
| 40 | /// [`Landed::Refused`]. A stack the branch has already is |
| 41 | /// [`Landed::AlreadyApplied`] in both modes, so that landing it again after |
| 42 | /// a lost answer is harmless. With conflicts, a refusal, or nothing new, |
| 43 | /// the branch does not move and no operation is recorded. |
| 44 | /// |
| 45 | /// The changesets and everything they name must be stored, and should have |
| 46 | /// been admitted (see [`braid_engine::admit`]): this only lands them. |
| 47 | pub fn land_stack( |
| 48 | repo: &Repo, |
| 49 | branch: &str, |
| 50 | stack: &[ObjectId], |
| 51 | mode: StackMode, |
| 52 | agent: &str, |
| 53 | committer: &Identity, |
| 54 | ) -> Result<LandReport, Error> { |
| 55 | let store = repo.store(); |
| 56 | let Some(&last) = stack.last() else { |
| 57 | return Err(Error::EmptyStack); |
| 58 | }; |
| 59 | for (index, pair) in stack.windows(2).enumerate() { |
| 60 | if read_changeset(store, pair[1])?.parents.first() != Some(&pair[0]) { |
| 61 | return Err(Error::NotAStack { index: index + 1 }); |
| 62 | } |
| 63 | } |
| 64 | let first_parent = read_changeset(store, stack[0])?.parents.first().copied(); |
| 65 | let signature = committer.now(); |
| 66 | let mut history = repo.history(); |
| 67 | let mut head_before = None; |
| 68 | let mut landed = Landed::Nothing; |
| 69 | let mut placed = Vec::new(); |
| 70 | let mut merged = Vec::new(); |
| 71 | let prefix = format!("land stack on {branch}"); |
| 72 | let itself = |ids: &[ObjectId]| -> Vec<Placed> { |
| 73 | ids.iter() |
| 74 | .map(|&id| Placed { |
| 75 | from: id, |
| 76 | to: Some(id), |
| 77 | }) |
| 78 | .collect() |
| 79 | }; |
| 80 | let result = store.transact_with(agent, |refs| -> Result<_, Option<Error>> { |
| 81 | let head = refs.get(branch); |
| 82 | head_before = head; |
| 83 | let (tip, outcome) = match head { |
| 84 | None => { |
| 85 | placed = itself(stack); |
| 86 | landed = Landed::FastForward(last); |
| 87 | (last, "fast-forward") |
| 88 | } |
| 89 | Some(head) if mode == StackMode::FastForwardOnly => { |
| 90 | let from = if first_parent == Some(head) { |
| 91 | Some(0) |
| 92 | } else { |
| 93 | stack.iter().position(|&id| id == head).map(|at| at + 1) |
| 94 | }; |
| 95 | match from { |
| 96 | Some(from) if from < stack.len() => { |
| 97 | placed = unplaced(&stack[..from]); |
| 98 | placed.extend(itself(&stack[from..])); |
| 99 | landed = Landed::FastForward(last); |
| 100 | (last, "fast-forward") |
| 101 | } |
| 102 | Some(_) => { |
| 103 | placed = unplaced(stack); |
| 104 | landed = Landed::AlreadyApplied; |
| 105 | return Err(None); |
| 106 | } |
| 107 | None => { |
| 108 | let has_it = history |
| 109 | .first_parent_chain_contains(head, last, usize::MAX) |
| 110 | .map_err(|err| Some(err.into()))?; |
| 111 | placed = unplaced(stack); |
| 112 | landed = if has_it { |
| 113 | Landed::AlreadyApplied |
| 114 | } else { |
| 115 | Landed::Refused |
| 116 | }; |
| 117 | return Err(None); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | Some(head) => { |
| 122 | match replay( |
| 123 | store, |
| 124 | branch, |
| 125 | Some(head), |
| 126 | stack, |
| 127 | &signature, |
| 128 | First::MustBeOnBranch, |
| 129 | ) |
| 130 | .map_err(Some)? |
| 131 | { |
| 132 | Replayed::Done { |
| 133 | tip, placed: went, .. |
| 134 | } if tip == head => { |
| 135 | placed = went; |
| 136 | landed = Landed::AlreadyApplied; |
| 137 | return Err(None); |
| 138 | } |
| 139 | Replayed::Done { |
| 140 | tip, |
| 141 | rebased, |
| 142 | placed: went, |
| 143 | merged_paths, |
| 144 | } => { |
| 145 | placed = went; |
| 146 | merged = merged_paths; |
| 147 | if rebased { |
| 148 | landed = Landed::Rebased(tip); |
| 149 | (tip, "rebased") |
| 150 | } else { |
| 151 | landed = Landed::FastForward(tip); |
| 152 | (tip, "fast-forward") |
| 153 | } |
| 154 | } |
| 155 | Replayed::Stopped { |
| 156 | index, conflicts, .. |
| 157 | } => { |
| 158 | placed = unplaced(stack); |
| 159 | landed = Landed::Conflicts { |
| 160 | change: stack[index], |
| 161 | conflicts, |
| 162 | }; |
| 163 | return Err(None); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | }; |
| 168 | let description = describe_landing(&prefix, outcome, &placed, merged.len()); |
| 169 | Ok((vec![RefUpdate::new(branch, head, Some(tip))], description)) |
| 170 | }); |
| 171 | let operation = match result { |
| 172 | Ok(op) => Some(op.seq), |
| 173 | // The branch stays where it is. |
| 174 | Err(TransactError::Aborted(None)) => None, |
| 175 | Err(TransactError::Aborted(Some(err))) => return Err(err), |
| 176 | Err(TransactError::Store(err)) => return Err(err.into()), |
| 177 | }; |
| 178 | history.flush()?; |
| 179 | Ok(LandReport { |
| 180 | branch: branch.to_owned(), |
| 181 | head_before, |
| 182 | landed, |
| 183 | operation, |
| 184 | placed, |
| 185 | merged_paths: merged, |
| 186 | updated_paths: Vec::new(), |
| 187 | }) |
| 188 | } |