Elffuss BraidHub

utopia-ia/elffuss-braid public

revert: only on a branch, --mainline only for a merge, and no false claim

--branch refused a name in braid's own namespaces only in verify: revert
moved braid/green/main when asked to. --mainline 1 on a changeset that is
not a merge is refused, as git refuses it. A revert the branch already has
no longer prints "Reverted".
Changeseta93ee013835a453dadbf0030c33358407bbcdb9fc8768c8b117e62a3d6ea5e4d
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 12:09 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 12:09 +02:00
Parents4d4f9e6cf2e4

Browse the files at this changeset

7 files changed

modified crates/cli/src/hub/land.rs +1 −1
@@ -705,7 +705,7 @@
705705 s.local.repo.store(),
706706 &braid_wc::RevertRequest {
707707 culprit,
708- allow_merge: false,
708+ mainline: false,
709709 author: ada(),
710710 agent: "verifier".to_owned(),
711711 },
modified crates/cli/src/revert.rs +19 −6
@@ -32,7 +32,9 @@
3232 is 2: undo the change by hand in a workspace instead.
3333
3434 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.
3638 --no-land only makes the changeset and prints its id. In a repository
3739 linked to a hub, the revert lands on the hub, then fetches; --local, or
3840 BRAID_HUB=off, lands here.
@@ -64,12 +66,19 @@
6466 }
6567
6668 /// 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.
6871 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+ );
7280 }
81+ Ok(branch)
7382 }
7483
7584 /// Refuses to revert `culprit` on `branch` unless it is on the branch's
@@ -133,7 +142,7 @@
133142 repo.store(),
134143 &RevertRequest {
135144 culprit,
136- allow_merge: args.mainline.is_some(),
145+ mainline: args.mainline.is_some(),
137146 author: author.clone(),
138147 agent: cli.agent.clone(),
139148 },
@@ -174,6 +183,10 @@
174183 print_conflicts(&report.conflicts);
175184 println!("Undo the change by hand in a workspace instead.");
176185 }
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+ ),
177190 Some(report) => {
178191 println!("Reverted {} with {}", culprit.short(), change.short());
179192 report.print_landing(cli);
modified crates/cli/src/verify.rs +1 −4
@@ -605,7 +605,7 @@
605605 self.repo.store(),
606606 &RevertRequest {
607607 culprit,
608- allow_merge: false,
608+ mainline: false,
609609 author: author.clone(),
610610 agent: AGENT.to_owned(),
611611 },
@@ -656,9 +656,6 @@
656656 pub(crate) fn verify(cli: &Cli, args: &VerifyArgs) -> Result<ExitCode> {
657657 let repo = open_repo(cli)?;
658658 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- }
662659 let link = hub::linked(&repo, args.local)?;
663660 if let Some(link) = &link
664661 && !args.dry_run
modified crates/cli/tests/revert.rs +105 −0
@@ -208,6 +208,95 @@
208208 assert_eq!(out.status.code(), Some(2));
209209 }
210210
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+
211300 #[cfg(unix)]
212301 #[test]
213302 fn a_revert_puts_back_modes_and_removes_what_was_added() {
@@ -242,3 +331,19 @@
242331 ]
243332 );
244333 }
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 @@
133133 pub struct RevertRequest {
134134 /// The changeset to undo.
135135 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,
139140 /// The author, who also commits it.
140141 pub author: Identity,
141142 /// The label of the agent making it, recorded as its
@@ -155,14 +156,16 @@
155156 /// conflict.
156157 ///
157158 /// 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.
159161 pub fn create_revert(store: &Store, request: &RevertRequest) -> Result<ObjectId, Error> {
160162 let culprit = request.culprit;
161163 let changeset = read_changeset(store, culprit)?;
162164 let parent = match changeset.parents.as_slice() {
163165 [] => return Err(Error::RevertRoot(culprit)),
166+ [_] if request.mainline => return Err(Error::NotAMerge(culprit)),
164167 [parent] => *parent,
165- [parent, ..] if request.allow_merge => *parent,
168+ [parent, ..] if request.mainline => *parent,
166169 parents => {
167170 return Err(Error::RevertMerge {
168171 changeset: culprit,
modified crates/wc/src/error.rs +4 −0
@@ -250,6 +250,10 @@
250250 parents: usize,
251251 },
252252
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+
253257 /// The changeset changes no file, so its revert would change none.
254258 #[error("changeset {} changes no file against its first parent: there is nothing to revert", .0.short())]
255259 NothingToRevert(ObjectId),
modified crates/wc/tests/revert.rs +7 −2
@@ -12,12 +12,12 @@
1212 use braid_wc::{Error, Landed, RevertRequest, create_revert, land_change};
1313 use common::{Fixture, ada};
1414
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> {
1616 create_revert(
1717 f.repo.store(),
1818 &RevertRequest {
1919 culprit,
20- allow_merge,
20+ mainline,
2121 author: ada(),
2222 agent: "reverter".to_owned(),
2323 },
@@ -185,4 +185,9 @@
185185 // parent.
186186 let id = revert(&f, merge, true).unwrap();
187187 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+ ));
188193 }