Elffuss BraidHub

utopia-ia/elffuss-braid public

wc: materialize behind a feature, and a revert with a message of the caller's

- The materialize feature, on by default, holds wc::materialize: a build
  that never writes files outside a workspace, such as a server's, can
  leave it out. The braid command keeps it, with wc's default features.
- create_revert_with_message makes the revert create_revert makes, with
  the same parent, changes, extra fields and refusals, but the message
  given; create_revert builds its usual message and calls it.
Changeset3297f228f20ca79257c1fd5b81c8efca42719228523fbf53ca837f25d52b02e1
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 17:30 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 17:30 +02:00
Parents52e22f93c9da

Browse the files at this changeset

5 files changed

modified crates/wc/Cargo.toml +7 −0
@@ -25,6 +25,13 @@
2525 thiserror = "2.0"
2626 unicode-normalization = "0.1.25"
2727
28+[features]
29+default = ["materialize"]
30+# `materialize`: write a changeset's files into a plain directory, as
31+# `braid verify` does. A build that never writes files outside a workspace,
32+# such as a server's, can leave it out.
33+materialize = []
34+
2835 [target.'cfg(unix)'.dependencies]
2936 rustix = { version = "1.1.5", features = ["fs"] }
3037
modified crates/wc/src/change.rs +16 −3
@@ -159,6 +159,21 @@
159159 /// unless [`RevertRequest::mainline`] says so, are refused, as is
160160 /// [`RevertRequest::mainline`] for a changeset that is not a merge.
161161 pub fn create_revert(store: &Store, request: &RevertRequest) -> Result<ObjectId, Error> {
162+ let culprit = request.culprit;
163+ let changeset = read_changeset(store, culprit)?;
164+ let mut message = format!("Revert \"{}\"", subject(&changeset.message)).into_bytes();
165+ message.extend_from_slice(format!("\n\nThis reverts changeset {culprit}.\n").as_bytes());
166+ create_revert_with_message(store, request, &message)
167+}
168+
169+/// Makes the revert of `request.culprit` as [`create_revert`] does, with the
170+/// same parent, changes, extra fields and refusals, but with `message` for
171+/// its message.
172+pub fn create_revert_with_message(
173+ store: &Store,
174+ request: &RevertRequest,
175+ message: &[u8],
176+) -> Result<ObjectId, Error> {
162177 let culprit = request.culprit;
163178 let changeset = read_changeset(store, culprit)?;
164179 let parent = match changeset.parents.as_slice() {
@@ -178,8 +193,6 @@
178193 if changes.is_empty() {
179194 return Err(Error::NothingToRevert(culprit));
180195 }
181- let mut message = format!("Revert \"{}\"", subject(&changeset.message)).into_bytes();
182- message.extend_from_slice(format!("\n\nThis reverts changeset {culprit}.\n").as_bytes());
183196 let mut extras = crate::rebase::agent_extras(&request.agent);
184197 extras.push((REVERTS.to_vec(), culprit.to_hex().into_bytes()));
185198 let signature = request.author.now();
@@ -189,7 +202,7 @@
189202 changes,
190203 signature.clone(),
191204 signature,
192- message,
205+ message.to_vec(),
193206 extras,
194207 )?;
195208 store.sync()?;
modified crates/wc/src/lib.rs +13 −5
@@ -41,10 +41,13 @@
4141 //! rebasing its own changesets; conflicts are written with markers.
4242 //! - [`create_change`] and [`land_change`] make and land a changeset
4343 //! without any working copy; [`create_revert`] makes one that undoes
44-//! another; [`land_stack`] lands a stack of them made elsewhere, as a hub
45-//! does.
46-//! - [`materialize`] writes a changeset's files into a plain directory,
47-//! with the same safe checkout, for a command that only needs the files.
44+//! another, and [`create_revert_with_message`] the same with a message of
45+//! the caller's; [`land_stack`] lands a stack of them made elsewhere, as a
46+//! hub does.
47+//! - `materialize` writes a changeset's files into a plain directory, with
48+//! the same safe checkout, for a command that only needs the files. It
49+//! comes with the `materialize` feature, on by default; a build that never
50+//! writes files outside a workspace, such as a server's, can leave it out.
4851 //! - [`Workspace::stack`] and [`Workspace::adopt`] split a landing in two,
4952 //! for a repository whose branches live on a hub: what to land there,
5053 //! and moving onto the head that holds it afterwards.
@@ -75,6 +78,7 @@
7578 mod identity;
7679 mod ignores;
7780 mod markers;
81+#[cfg(feature = "materialize")]
7882 mod materialize;
7983 mod paths;
8084 mod rebase;
@@ -87,10 +91,14 @@
8791 mod workspace;
8892
8993 pub use blocker::Blocker;
90-pub use change::{ChangeRequest, Expect, RevertRequest, create_change, create_revert, land_change};
94+pub use change::{
95+ ChangeRequest, Expect, RevertRequest, create_change, create_revert, create_revert_with_message,
96+ land_change,
97+};
9198 pub use error::Error;
9299 pub use fsprobe::FileSystem;
93100 pub use identity::Identity;
101+#[cfg(feature = "materialize")]
94102 pub use materialize::materialize;
95103 pub use paths::workspace_path;
96104 pub use stack::{StackMode, land_stack};
modified crates/wc/tests/materialize.rs +2 −0
@@ -4,6 +4,8 @@
44 //! Writing a changeset into a plain directory: the files only, with the
55 //! checkout's modes and its refusal to follow symlinks.
66
7+#![cfg(feature = "materialize")]
8+
79 mod common;
810
911 use braid_format::{ChangeOp, GitId};
modified crates/wc/tests/revert.rs +59 −1
@@ -9,7 +9,9 @@
99
1010 use braid_engine::{AGENT, REBASED_FROM, REVERTS, read_changeset};
1111 use braid_format::{ChangeOp, GitId, ObjectId};
12-use braid_wc::{Error, Landed, RevertRequest, create_revert, land_change};
12+use braid_wc::{
13+ Error, Landed, RevertRequest, create_revert, create_revert_with_message, land_change,
14+};
1315 use common::{Fixture, ada};
1416
1517 fn revert(f: &Fixture, culprit: ObjectId, mainline: bool) -> Result<ObjectId, Error> {
@@ -191,3 +193,59 @@
191193 Err(Error::NotAMerge(id)) if id == side
192194 ));
193195 }
196+
197+#[test]
198+fn a_revert_can_carry_a_message_of_its_own() {
199+ let f = Fixture::new();
200+ let base = f.commit(&[], vec![("a", f.file("1\n")), ("b", f.file("b\n"))]);
201+ let culprit = f.commit(
202+ &[base],
203+ vec![("a", f.file("2\n")), ("run", f.exec("echo\n"))],
204+ );
205+ let request = |culprit, mainline| RevertRequest {
206+ culprit,
207+ mainline,
208+ author: ada(),
209+ agent: "reverter".to_owned(),
210+ };
211+ let message = b"Undo the change to a\n\nIt broke the tests.\n";
212+ let own =
213+ create_revert_with_message(f.repo.store(), &request(culprit, false), message).unwrap();
214+ let usual = revert(&f, culprit, false).unwrap();
215+ let (own, usual) = (
216+ read_changeset(f.repo.store(), own).unwrap(),
217+ read_changeset(f.repo.store(), usual).unwrap(),
218+ );
219+ assert_eq!(own.message, message);
220+ assert_ne!(usual.message, own.message);
221+ assert_eq!(own.root, usual.root);
222+ assert_eq!(own.root, root(&f, base));
223+ assert_eq!(own.parents, usual.parents);
224+ assert_eq!(own.parents, [culprit]);
225+ assert_eq!(own.extras, usual.extras);
226+ assert_eq!(own.changes, usual.changes);
227+
228+ // And the same refusals.
229+ let with_message = |culprit, mainline| {
230+ create_revert_with_message(f.repo.store(), &request(culprit, mainline), message)
231+ };
232+ assert!(matches!(
233+ with_message(base, false),
234+ Err(Error::RevertRoot(_))
235+ ));
236+ assert!(matches!(
237+ with_message(culprit, true),
238+ Err(Error::NotAMerge(_))
239+ ));
240+ let empty = f.commit(&[culprit], Vec::new());
241+ assert!(matches!(
242+ with_message(empty, false),
243+ Err(Error::NothingToRevert(_))
244+ ));
245+ let side = f.commit(&[base], vec![("s", f.file("s\n"))]);
246+ let merge = f.commit(&[culprit, side], vec![("s", f.file("s\n"))]);
247+ assert!(matches!(
248+ with_message(merge, false),
249+ Err(Error::RevertMerge { parents: 2, .. })
250+ ));
251+}