utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | a93ee013835a453dadbf0030c33358407bbcdb9fc8768c8b117e62a3d6ea5e4d |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 12:09 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 12:09 +02:00 |
| Parents | 4d4f9e6cf2e4 |
7 files changed
modified crates/cli/src/hub/land.rs +1 −1
| @@ -705,7 +705,7 @@ | ||
| 705 | 705 | s.local.repo.store(), |
| 706 | 706 | &braid_wc::RevertRequest { |
| 707 | 707 | culprit, |
| 708 | - allow_merge: false, | |
| 708 | + mainline: false, | |
| 709 | 709 | author: ada(), |
| 710 | 710 | agent: "verifier".to_owned(), |
| 711 | 711 | }, |
modified crates/cli/src/revert.rs +19 −6
| @@ -32,7 +32,9 @@ | ||
| 32 | 32 | is 2: undo the change by hand in a workspace instead. |
| 33 | 33 | |
| 34 | 34 | A changeset without parents cannot be reverted, nor a merge unless |
| 35 | ---mainline 1 says to undo what it brought in against its first parent. | |
| 35 | +--mainline 1 says to undo what it brought in against its first parent; | |
| 36 | +--mainline is refused for a changeset that is not a merge. --branch must | |
| 37 | +name a branch, not a ref in one of braid's own namespaces. | |
| 36 | 38 | --no-land only makes the changeset and prints its id. In a repository |
| 37 | 39 | linked to a hub, the revert lands on the hub, then fetches; --local, or |
| 38 | 40 | BRAID_HUB=off, lands here. |
| @@ -64,12 +66,19 @@ | ||
| 64 | 66 | } |
| 65 | 67 | |
| 66 | 68 | /// The branch a command acts on: the one given, or else the repository's |
| 67 | -/// main branch: `main`, `master`, or its only branch. | |
| 69 | +/// main branch: `main`, `master`, or its only branch. A name in one of | |
| 70 | +/// braid's own namespaces, such as a green ref, is refused. | |
| 68 | 71 | pub(crate) fn branch_or_main(repo: &Repo, given: Option<&str>) -> Result<String> { |
| 69 | - match given { | |
| 70 | - Some(branch) => Ok(branch.to_owned()), | |
| 71 | - None => crate::agents::main_branch(&branches(&repo.store().refs()?)), | |
| 72 | + let branch = match given { | |
| 73 | + Some(branch) => branch.to_owned(), | |
| 74 | + None => crate::agents::main_branch(&branches(&repo.store().refs()?))?, | |
| 75 | + }; | |
| 76 | + if !braid_repo::is_branch(&branch) { | |
| 77 | + bail!( | |
| 78 | + "{branch:?} is in one of braid's own namespaces (braid/, git/, remote/, tags/), not a branch" | |
| 79 | + ); | |
| 72 | 80 | } |
| 81 | + Ok(branch) | |
| 73 | 82 | } |
| 74 | 83 | |
| 75 | 84 | /// Refuses to revert `culprit` on `branch` unless it is on the branch's |
| @@ -133,7 +142,7 @@ | ||
| 133 | 142 | repo.store(), |
| 134 | 143 | &RevertRequest { |
| 135 | 144 | culprit, |
| 136 | - allow_merge: args.mainline.is_some(), | |
| 145 | + mainline: args.mainline.is_some(), | |
| 137 | 146 | author: author.clone(), |
| 138 | 147 | agent: cli.agent.clone(), |
| 139 | 148 | }, |
| @@ -174,6 +183,10 @@ | ||
| 174 | 183 | print_conflicts(&report.conflicts); |
| 175 | 184 | println!("Undo the change by hand in a workspace instead."); |
| 176 | 185 | } |
| 186 | + Some(report) if report.outcome == "already-applied" => println!( | |
| 187 | + "{branch} has what the revert of {} would change already: nothing landed", | |
| 188 | + culprit.short() | |
| 189 | + ), | |
| 177 | 190 | Some(report) => { |
| 178 | 191 | println!("Reverted {} with {}", culprit.short(), change.short()); |
| 179 | 192 | report.print_landing(cli); |
modified crates/cli/src/verify.rs +1 −4
| @@ -605,7 +605,7 @@ | ||
| 605 | 605 | self.repo.store(), |
| 606 | 606 | &RevertRequest { |
| 607 | 607 | culprit, |
| 608 | - allow_merge: false, | |
| 608 | + mainline: false, | |
| 609 | 609 | author: author.clone(), |
| 610 | 610 | agent: AGENT.to_owned(), |
| 611 | 611 | }, |
| @@ -656,9 +656,6 @@ | ||
| 656 | 656 | pub(crate) fn verify(cli: &Cli, args: &VerifyArgs) -> Result<ExitCode> { |
| 657 | 657 | let repo = open_repo(cli)?; |
| 658 | 658 | let branch = branch_or_main(&repo, args.branch.as_deref())?; |
| 659 | - if !braid_repo::is_branch(&branch) { | |
| 660 | - bail!("{branch:?} is in one of braid's own namespaces, not a branch"); | |
| 661 | - } | |
| 662 | 659 | let link = hub::linked(&repo, args.local)?; |
| 663 | 660 | if let Some(link) = &link |
| 664 | 661 | && !args.dry_run |
modified crates/cli/tests/revert.rs +105 −0
| @@ -208,6 +208,95 @@ | ||
| 208 | 208 | assert_eq!(out.status.code(), Some(2)); |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | +#[test] | |
| 212 | +fn a_revert_lands_only_on_a_branch() { | |
| 213 | + let r = Repo::new(); | |
| 214 | + r.land(&[("a", "1\n")], "Seed"); | |
| 215 | + let culprit = r.land(&[("a", "2\n")], "Change"); | |
| 216 | + // A ref of braid's own, as verify's green ref is, is no branch. | |
| 217 | + run(&r.dir, &["ref", "set", "braid/green/main", &culprit], 0); | |
| 218 | + let out = braid( | |
| 219 | + &r.dir, | |
| 220 | + &["revert", &culprit, "--branch", "braid/green/main"], | |
| 221 | + ); | |
| 222 | + assert_eq!(out.status.code(), Some(1)); | |
| 223 | + let err = String::from_utf8_lossy(&out.stderr); | |
| 224 | + assert!(err.contains("braid's own namespaces"), "{err}"); | |
| 225 | + let refs = run(&r.dir, &["refs"], 0); | |
| 226 | + assert_eq!(refs["refs"]["braid/green/main"], culprit.as_str()); | |
| 227 | + assert_eq!(r.head().unwrap(), culprit); | |
| 228 | +} | |
| 229 | + | |
| 230 | +/// Git without the user's or the system's configuration. | |
| 231 | +const GIT_ENV: [(&str, &str); 6] = [ | |
| 232 | + ("GIT_CONFIG_GLOBAL", "/dev/null"), | |
| 233 | + ("GIT_CONFIG_NOSYSTEM", "1"), | |
| 234 | + ("GIT_AUTHOR_NAME", "Ada Lovelace"), | |
| 235 | + ("GIT_AUTHOR_EMAIL", "ada@example.org"), | |
| 236 | + ("GIT_COMMITTER_NAME", "Ada Lovelace"), | |
| 237 | + ("GIT_COMMITTER_EMAIL", "ada@example.org"), | |
| 238 | +]; | |
| 239 | + | |
| 240 | +fn git(dir: &Path, args: &[&str]) { | |
| 241 | + let out = Command::new("git") | |
| 242 | + .current_dir(dir) | |
| 243 | + .args(args) | |
| 244 | + .envs(GIT_ENV) | |
| 245 | + .output() | |
| 246 | + .expect("git must be installed to run this test"); | |
| 247 | + assert!( | |
| 248 | + out.status.success(), | |
| 249 | + "git {args:?}: {}", | |
| 250 | + String::from_utf8_lossy(&out.stderr) | |
| 251 | + ); | |
| 252 | +} | |
| 253 | + | |
| 254 | +#[test] | |
| 255 | +fn a_merge_is_reverted_against_its_first_parent_with_mainline_1() { | |
| 256 | + let temp = tempfile::tempdir().unwrap(); | |
| 257 | + let base = std::fs::canonicalize(temp.path()).unwrap(); | |
| 258 | + let project = base.join("project"); | |
| 259 | + std::fs::create_dir(&project).unwrap(); | |
| 260 | + git(&project, &["init", "-q", "-b", "main"]); | |
| 261 | + std::fs::write(project.join("a"), "a\n").unwrap(); | |
| 262 | + git(&project, &["add", "a"]); | |
| 263 | + git(&project, &["commit", "-q", "-m", "Seed"]); | |
| 264 | + git(&project, &["checkout", "-q", "-b", "side"]); | |
| 265 | + std::fs::write(project.join("side"), "s\n").unwrap(); | |
| 266 | + git(&project, &["add", "side"]); | |
| 267 | + git(&project, &["commit", "-q", "-m", "Side"]); | |
| 268 | + git(&project, &["checkout", "-q", "main"]); | |
| 269 | + std::fs::write(project.join("main"), "m\n").unwrap(); | |
| 270 | + git(&project, &["add", "main"]); | |
| 271 | + git(&project, &["commit", "-q", "-m", "Main"]); | |
| 272 | + git( | |
| 273 | + &project, | |
| 274 | + &["merge", "-q", "--no-ff", "-m", "Merge side", "side"], | |
| 275 | + ); | |
| 276 | + | |
| 277 | + let dir = base.join("repo"); | |
| 278 | + run(&base, &["init", dir.to_str().unwrap()], 0); | |
| 279 | + run(&dir, &["git", "import", project.to_str().unwrap()], 0); | |
| 280 | + let merge = id(&run(&dir, &["show", "main"], 0)["id"]); | |
| 281 | + let parent = id(&run(&dir, &["show", "main"], 0)["parents"][0]); | |
| 282 | + | |
| 283 | + // Refused without --mainline, and --mainline on a changeset that is not | |
| 284 | + // a merge. | |
| 285 | + let out = braid(&dir, &["revert", &merge]); | |
| 286 | + assert_eq!(out.status.code(), Some(1)); | |
| 287 | + assert!(String::from_utf8_lossy(&out.stderr).contains("--mainline 1")); | |
| 288 | + let out = braid(&dir, &["revert", &parent, "--mainline", "1"]); | |
| 289 | + assert_eq!(out.status.code(), Some(1)); | |
| 290 | + assert!(String::from_utf8_lossy(&out.stderr).contains("is not a merge")); | |
| 291 | + | |
| 292 | + let out = run(&dir, &["revert", &merge, "--mainline", "1"], 0); | |
| 293 | + assert_eq!(out["land"]["outcome"], "fast-forward"); | |
| 294 | + let back = run(&dir, &["diff", &parent, "main"], 0); | |
| 295 | + assert_eq!(back["changes"], serde_json::json!([])); | |
| 296 | + let gone = braid(&dir, &["cat-object", "main:side"]); | |
| 297 | + assert!(!gone.status.success()); | |
| 298 | +} | |
| 299 | + | |
| 211 | 300 | #[cfg(unix)] |
| 212 | 301 | #[test] |
| 213 | 302 | fn a_revert_puts_back_modes_and_removes_what_was_added() { |
| @@ -242,3 +331,19 @@ | ||
| 242 | 331 | ] |
| 243 | 332 | ); |
| 244 | 333 | } |
| 334 | + | |
| 335 | +#[test] | |
| 336 | +fn a_revert_the_branch_has_already_says_nothing_landed() { | |
| 337 | + let r = Repo::new(); | |
| 338 | + r.land(&[("a", "1\n"), ("b", "b\n")], "Seed"); | |
| 339 | + let culprit = r.land(&[("a", "2\n")], "Change"); | |
| 340 | + let undone = r.land(&[("a", "1\n")], "Undo by hand"); | |
| 341 | + let out = run(&r.dir, &["revert", &culprit], 0); | |
| 342 | + assert_eq!(out["land"]["outcome"], "already-applied"); | |
| 343 | + let text = braid(&r.dir, &["revert", &culprit]); | |
| 344 | + assert!(text.status.success()); | |
| 345 | + let text = String::from_utf8(text.stdout).unwrap(); | |
| 346 | + assert!(text.contains("nothing landed"), "{text}"); | |
| 347 | + assert!(!text.contains("Reverted"), "{text}"); | |
| 348 | + assert_eq!(r.head().unwrap(), undone); | |
| 349 | +} |
modified crates/wc/src/change.rs +8 −5
| @@ -133,9 +133,10 @@ | ||
| 133 | 133 | pub struct RevertRequest { |
| 134 | 134 | /// The changeset to undo. |
| 135 | 135 | pub culprit: ObjectId, |
| 136 | - /// Whether a merge may be reverted. Its revert undoes what it brought | |
| 137 | - /// in against its first parent. | |
| 138 | - pub allow_merge: bool, | |
| 136 | + /// Whether the changeset is a merge to revert against its first parent: | |
| 137 | + /// the revert undoes what the merge brought in. A merge is refused | |
| 138 | + /// without it, and anything but a merge with it. | |
| 139 | + pub mainline: bool, | |
| 139 | 140 | /// The author, who also commits it. |
| 140 | 141 | pub author: Identity, |
| 141 | 142 | /// The label of the agent making it, recorded as its |
| @@ -155,14 +156,16 @@ | ||
| 155 | 156 | /// conflict. |
| 156 | 157 | /// |
| 157 | 158 | /// A changeset without parents, one that changes nothing, and a merge |
| 158 | -/// unless [`RevertRequest::allow_merge`] says so, are refused. | |
| 159 | +/// unless [`RevertRequest::mainline`] says so, are refused, as is | |
| 160 | +/// [`RevertRequest::mainline`] for a changeset that is not a merge. | |
| 159 | 161 | pub fn create_revert(store: &Store, request: &RevertRequest) -> Result<ObjectId, Error> { |
| 160 | 162 | let culprit = request.culprit; |
| 161 | 163 | let changeset = read_changeset(store, culprit)?; |
| 162 | 164 | let parent = match changeset.parents.as_slice() { |
| 163 | 165 | [] => return Err(Error::RevertRoot(culprit)), |
| 166 | + [_] if request.mainline => return Err(Error::NotAMerge(culprit)), | |
| 164 | 167 | [parent] => *parent, |
| 165 | - [parent, ..] if request.allow_merge => *parent, | |
| 168 | + [parent, ..] if request.mainline => *parent, | |
| 166 | 169 | parents => { |
| 167 | 170 | return Err(Error::RevertMerge { |
| 168 | 171 | changeset: culprit, |
modified crates/wc/src/error.rs +4 −0
| @@ -250,6 +250,10 @@ | ||
| 250 | 250 | parents: usize, |
| 251 | 251 | }, |
| 252 | 252 | |
| 253 | + /// `--mainline` was given for a changeset that is not a merge. | |
| 254 | + #[error("changeset {} is not a merge: --mainline is only for reverting a merge", .0.short())] | |
| 255 | + NotAMerge(ObjectId), | |
| 256 | + | |
| 253 | 257 | /// The changeset changes no file, so its revert would change none. |
| 254 | 258 | #[error("changeset {} changes no file against its first parent: there is nothing to revert", .0.short())] |
| 255 | 259 | NothingToRevert(ObjectId), |
modified crates/wc/tests/revert.rs +7 −2
| @@ -12,12 +12,12 @@ | ||
| 12 | 12 | use braid_wc::{Error, Landed, RevertRequest, create_revert, land_change}; |
| 13 | 13 | use common::{Fixture, ada}; |
| 14 | 14 | |
| 15 | -fn revert(f: &Fixture, culprit: ObjectId, allow_merge: bool) -> Result<ObjectId, Error> { | |
| 15 | +fn revert(f: &Fixture, culprit: ObjectId, mainline: bool) -> Result<ObjectId, Error> { | |
| 16 | 16 | create_revert( |
| 17 | 17 | f.repo.store(), |
| 18 | 18 | &RevertRequest { |
| 19 | 19 | culprit, |
| 20 | - allow_merge, | |
| 20 | + mainline, | |
| 21 | 21 | author: ada(), |
| 22 | 22 | agent: "reverter".to_owned(), |
| 23 | 23 | }, |
| @@ -185,4 +185,9 @@ | ||
| 185 | 185 | // parent. |
| 186 | 186 | let id = revert(&f, merge, true).unwrap(); |
| 187 | 187 | assert_eq!(root(&f, id), root(&f, base)); |
| 188 | + // And a changeset that is not a merge is no mainline's. | |
| 189 | + assert!(matches!( | |
| 190 | + revert(&f, side, true), | |
| 191 | + Err(Error::NotAMerge(id)) if id == side | |
| 192 | + )); | |
| 188 | 193 | } |