| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! Stacks landed the way a hub lands them, and a workspace landing in two |
| 5 | //! steps: what to land, then moving onto the head that holds it. |
| 6 | |
| 7 | mod common; |
| 8 | |
| 9 | use std::fs; |
| 10 | |
| 11 | use braid_format::ObjectId; |
| 12 | use braid_wc::{CommitRequest, Error, Landed, StackMode, Workspace, land_stack}; |
| 13 | use common::{Fixture, ada}; |
| 14 | |
| 15 | fn commit_all(ws: &Workspace<'_>, message: &str) -> ObjectId { |
| 16 | ws.commit(&CommitRequest { |
| 17 | message: Some(message.as_bytes().to_vec()), |
| 18 | paths: Vec::new(), |
| 19 | author: None, |
| 20 | committer: ada(), |
| 21 | agent: "tester".to_owned(), |
| 22 | force: false, |
| 23 | }) |
| 24 | .unwrap() |
| 25 | .changeset |
| 26 | .unwrap() |
| 27 | } |
| 28 | |
| 29 | fn land(f: &Fixture, stack: &[ObjectId], mode: StackMode) -> braid_wc::LandReport { |
| 30 | land_stack(&f.repo, "main", stack, mode, "hub", &ada()).unwrap() |
| 31 | } |
| 32 | |
| 33 | /// The first parents from `id` down, `id` first. |
| 34 | fn chain(f: &Fixture, id: ObjectId) -> Vec<ObjectId> { |
| 35 | let mut out = Vec::new(); |
| 36 | let mut current = Some(id); |
| 37 | while let Some(id) = current { |
| 38 | out.push(id); |
| 39 | current = braid_engine::read_changeset(f.repo.store(), id) |
| 40 | .unwrap() |
| 41 | .parents |
| 42 | .first() |
| 43 | .copied(); |
| 44 | } |
| 45 | out |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn a_stack_rebases_whole_or_stops_whole() { |
| 50 | let f = Fixture::new(); |
| 51 | let seed = f.commit(&[], vec![("a", f.file("a\n")), ("x", f.file("1\n2\n3\n"))]); |
| 52 | f.set_branch("main", seed); |
| 53 | let one = f.commit(&[seed], vec![("b", f.file("b\n"))]); |
| 54 | let two = f.commit(&[one], vec![("c", f.file("c\n"))]); |
| 55 | let three = f.commit(&[two], vec![("d", f.file("d\n"))]); |
| 56 | // Someone else landed meanwhile. |
| 57 | let theirs = f.commit(&[seed], vec![("a", f.file("a, theirs\n"))]); |
| 58 | f.set_branch("main", theirs); |
| 59 | |
| 60 | let report = land(&f, &[one, two, three], StackMode::Rebase); |
| 61 | let Landed::Rebased(tip) = report.landed else { |
| 62 | panic!("expected a rebase, got {:?}", report.landed); |
| 63 | }; |
| 64 | assert_eq!(report.head_before, Some(theirs)); |
| 65 | assert_eq!(f.head("main"), Some(tip)); |
| 66 | assert!(report.operation.is_some()); |
| 67 | assert_eq!(report.placed.len(), 3); |
| 68 | assert!(report.placed.iter().all(|p| p.to.is_some())); |
| 69 | assert_eq!(report.placed[2].to, Some(tip)); |
| 70 | // The three, rebased in order, on top of theirs. |
| 71 | let main = chain(&f, tip); |
| 72 | assert_eq!(main[3], theirs); |
| 73 | assert_eq!(main[4], seed); |
| 74 | |
| 75 | // The same stack again, as after a lost answer: nothing moves. |
| 76 | let again = land(&f, &[one, two, three], StackMode::Rebase); |
| 77 | assert_eq!(again.landed, Landed::AlreadyApplied); |
| 78 | assert_eq!(again.operation, None); |
| 79 | assert_eq!(f.head("main"), Some(tip)); |
| 80 | |
| 81 | // A stack whose second changeset conflicts with main: the first is |
| 82 | // clean, but nothing lands. |
| 83 | let clean = f.commit(&[seed], vec![("e", f.file("e\n"))]); |
| 84 | let clash = f.commit(&[clean], vec![("a", f.file("a, ours\n"))]); |
| 85 | let report = land(&f, &[clean, clash], StackMode::Rebase); |
| 86 | let Landed::Conflicts { change, conflicts } = &report.landed else { |
| 87 | panic!("expected conflicts, got {:?}", report.landed); |
| 88 | }; |
| 89 | assert_eq!(*change, clash); |
| 90 | assert_eq!(conflicts[0].path, b"a"); |
| 91 | assert_eq!(report.operation, None); |
| 92 | assert!(report.placed.iter().all(|p| p.to.is_none())); |
| 93 | assert_eq!(f.head("main"), Some(tip)); |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn fast_forward_only_moves_or_refuses() { |
| 98 | let f = Fixture::new(); |
| 99 | let seed = f.commit(&[], vec![("a", f.file("a\n"))]); |
| 100 | let one = f.commit(&[seed], vec![("b", f.file("b\n"))]); |
| 101 | let two = f.commit(&[one], vec![("c", f.file("c\n"))]); |
| 102 | |
| 103 | // A branch that does not exist is created at the last changeset, whatever |
| 104 | // the first one's parent. |
| 105 | let report = land(&f, &[one, two], StackMode::FastForwardOnly); |
| 106 | assert_eq!(report.landed, Landed::FastForward(two)); |
| 107 | assert_eq!(report.head_before, None); |
| 108 | assert_eq!(f.head("main"), Some(two)); |
| 109 | |
| 110 | // On the head: a fast-forward. |
| 111 | let three = f.commit(&[two], vec![("d", f.file("d\n"))]); |
| 112 | let four = f.commit(&[three], vec![("e", f.file("e\n"))]); |
| 113 | let report = land(&f, &[three], StackMode::FastForwardOnly); |
| 114 | assert_eq!(report.landed, Landed::FastForward(three)); |
| 115 | // A stack holding the head goes on from it. |
| 116 | let report = land(&f, &[two, three, four], StackMode::FastForwardOnly); |
| 117 | assert_eq!(report.landed, Landed::FastForward(four)); |
| 118 | let went: Vec<Option<ObjectId>> = report.placed.iter().map(|p| p.to).collect(); |
| 119 | assert_eq!(went, [None, None, Some(four)]); |
| 120 | assert_eq!(f.head("main"), Some(four)); |
| 121 | |
| 122 | // Again, and an older stack: the branch has them. |
| 123 | for stack in [&[three, four][..], &[one, two][..]] { |
| 124 | let report = land(&f, stack, StackMode::FastForwardOnly); |
| 125 | assert_eq!(report.landed, Landed::AlreadyApplied, "{stack:?}"); |
| 126 | assert_eq!(report.operation, None); |
| 127 | } |
| 128 | |
| 129 | // Built on an older changeset: refused, and main stays. |
| 130 | let late = f.commit(&[two], vec![("f", f.file("f\n"))]); |
| 131 | let report = land(&f, &[late], StackMode::FastForwardOnly); |
| 132 | assert_eq!(report.landed, Landed::Refused); |
| 133 | assert_eq!(report.head(), Some(four)); |
| 134 | assert_eq!(report.operation, None); |
| 135 | assert_eq!(f.head("main"), Some(four)); |
| 136 | // The same in rebase mode lands it. |
| 137 | let report = land(&f, &[late], StackMode::Rebase); |
| 138 | assert!(matches!(report.landed, Landed::Rebased(_)), "{report:?}"); |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn what_is_not_a_stack_is_refused() { |
| 143 | let f = Fixture::new(); |
| 144 | let seed = f.commit(&[], vec![("a", f.file("a\n"))]); |
| 145 | f.set_branch("main", seed); |
| 146 | let one = f.commit(&[seed], vec![("b", f.file("b\n"))]); |
| 147 | let other = f.commit(&[seed], vec![("c", f.file("c\n"))]); |
| 148 | for mode in [StackMode::Rebase, StackMode::FastForwardOnly] { |
| 149 | assert!(matches!( |
| 150 | land_stack(&f.repo, "main", &[], mode, "hub", &ada()), |
| 151 | Err(Error::EmptyStack) |
| 152 | )); |
| 153 | assert!(matches!( |
| 154 | land_stack(&f.repo, "main", &[one, other], mode, "hub", &ada()), |
| 155 | Err(Error::NotAStack { index: 1 }) |
| 156 | )); |
| 157 | } |
| 158 | // Rebasing needs the base on the branch's first-parent chain. |
| 159 | let elsewhere = f.commit(&[], vec![("z", f.file("z\n"))]); |
| 160 | let on_it = f.commit(&[elsewhere], vec![("y", f.file("y\n"))]); |
| 161 | assert!(matches!( |
| 162 | land_stack(&f.repo, "main", &[on_it], StackMode::Rebase, "hub", &ada()), |
| 163 | Err(Error::NotOnBranch { .. }) |
| 164 | )); |
| 165 | assert_eq!(f.head("main"), Some(seed)); |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn a_workspace_lands_in_two_steps() { |
| 170 | let f = Fixture::new(); |
| 171 | let seed = f.commit(&[], vec![("a", f.file("a\n"))]); |
| 172 | f.set_branch("main", seed); |
| 173 | let dir = f.path("ws"); |
| 174 | let (ws, _) = Workspace::create(&f.repo, "ws", &dir, Some(seed), "main").unwrap(); |
| 175 | fs::write(dir.join("b"), "b\n").unwrap(); |
| 176 | let first = commit_all(&ws, "first"); |
| 177 | fs::write(dir.join("c"), "c\n").unwrap(); |
| 178 | let second = commit_all(&ws, "second"); |
| 179 | |
| 180 | let stack = ws.stack(None).unwrap(); |
| 181 | assert_eq!(stack.branch, "main"); |
| 182 | assert_eq!(stack.head, Some(seed)); |
| 183 | assert_eq!(stack.base, Some(second)); |
| 184 | assert_eq!(stack.changes, [first, second]); |
| 185 | |
| 186 | // Elsewhere, main moves and the stack lands on it, rebased. |
| 187 | let theirs = f.commit(&[seed], vec![("t", f.file("t\n"))]); |
| 188 | f.set_branch("main", theirs); |
| 189 | let report = land(&f, &stack.changes, StackMode::Rebase); |
| 190 | let Landed::Rebased(tip) = report.landed else { |
| 191 | panic!("expected a rebase, got {:?}", report.landed); |
| 192 | }; |
| 193 | |
| 194 | // Not while the working copy has changes, nor from another base. |
| 195 | fs::write(dir.join("b"), "b, edited\n").unwrap(); |
| 196 | assert!(matches!( |
| 197 | ws.adopt(stack.base, tip), |
| 198 | Err(Error::Uncommitted { .. }) |
| 199 | )); |
| 200 | assert!(matches!(ws.stack(None), Err(Error::Uncommitted { .. }))); |
| 201 | fs::write(dir.join("b"), "b\n").unwrap(); |
| 202 | assert!(matches!( |
| 203 | ws.adopt(Some(first), tip), |
| 204 | Err(Error::BaseMoved { .. }) |
| 205 | )); |
| 206 | |
| 207 | let written = ws.adopt(stack.base, tip).unwrap(); |
| 208 | assert_eq!(written, [b"t".to_vec()]); |
| 209 | assert_eq!(fs::read(dir.join("t")).unwrap(), b"t\n"); |
| 210 | let state = ws.state().unwrap(); |
| 211 | assert_eq!(state.base, Some(tip)); |
| 212 | assert_eq!(state.upstream, Some(tip)); |
| 213 | let status = ws.status().unwrap(); |
| 214 | assert!(status.status.is_clean()); |
| 215 | assert!(status.unlanded.is_empty()); |
| 216 | assert!(!status.behind()); |
| 217 | // Adopting the head it is on does nothing. |
| 218 | assert!(ws.adopt(Some(tip), tip).unwrap().is_empty()); |
| 219 | } |