Elffuss BraidHub

utopia-ia/elffuss-braid public

wc::materialize: write a changeset's files into a plain directory

The workspace's safe checkout, without the workspace: no state, no .braid
file and no .git guard, for a command that only needs the files, such as a
test run.
Changeset2da7deeea818945b9d4cc4498aa81e415044d5b26d0913dac593514fd61ba7b3
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 11:42 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 11:42 +02:00
Parents8f30159883ea

Browse the files at this changeset

3 files changed

modified crates/wc/src/lib.rs +4 −0
@@ -43,6 +43,8 @@
4343 //! without any working copy; [`create_revert`] makes one that undoes
4444 //! another; [`land_stack`] lands a stack of them made elsewhere, as a hub
4545 //! 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.
4648 //! - [`Workspace::stack`] and [`Workspace::adopt`] split a landing in two,
4749 //! for a repository whose branches live on a hub: what to land there,
4850 //! and moving onto the head that holds it afterwards.
@@ -73,6 +75,7 @@
7375 mod identity;
7476 mod ignores;
7577 mod markers;
78+mod materialize;
7679 mod paths;
7780 mod rebase;
7881 mod safe;
@@ -88,6 +91,7 @@
8891 pub use error::Error;
8992 pub use fsprobe::FileSystem;
9093 pub use identity::Identity;
94+pub use materialize::materialize;
9195 pub use paths::workspace_path;
9296 pub use stack::{StackMode, land_stack};
9397 pub use state::{Checkout, Rebase, RecordedConflict, State};
added crates/wc/src/materialize.rs +47 −0
@@ -0,0 +1,47 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// Copyright (c) 2026 UtopiaIA
3+
4+//! Writing a changeset's files into a plain directory: no workspace, no
5+//! state, no `.braid` file and no `.git` guard.
6+
7+use std::fs;
8+use std::io;
9+use std::path::Path;
10+
11+use braid_engine::{EMPTY_TREE, read_changeset};
12+use braid_format::ObjectId;
13+use braid_store::Store;
14+
15+use crate::checkout::{self, Mode};
16+use crate::error::Error;
17+use crate::fsprobe::FileSystem;
18+use crate::safe::Root;
19+
20+/// Writes the files of `changeset` into `dir`, which must be empty or not
21+/// exist yet (its parent must), and returns how many it wrote.
22+///
23+/// The checkout is a workspace's, with the same safety: every write goes
24+/// through directory handles that never follow a symlink, regular files get
25+/// mode 0644 or 0755 only, gitlinks are empty directories, and names this
26+/// platform cannot hold, or that would collide on its file system, are
27+/// refused before anything is written. Nothing else goes into `dir`: it is
28+/// not a workspace, braid keeps no state for it, and git run inside it finds
29+/// whatever repository is around it, as anywhere else.
30+pub fn materialize(store: &Store, changeset: ObjectId, dir: &Path) -> Result<usize, Error> {
31+ match fs::read_dir(dir) {
32+ Ok(mut entries) => {
33+ if entries.next().is_some() {
34+ return Err(Error::DirectoryNotEmpty(dir.to_path_buf()));
35+ }
36+ }
37+ Err(err) if err.kind() == io::ErrorKind::NotFound => {
38+ fs::create_dir(dir).map_err(Error::io("create", dir))?;
39+ }
40+ Err(err) => return Err(Error::io("read", dir)(err)),
41+ }
42+ let to = read_changeset(store, changeset)?.root;
43+ let root = Root::open(dir)?;
44+ let fs = FileSystem::probe(root.dir());
45+ let plan = checkout::plan(store, &root, fs, EMPTY_TREE, to, Mode::Checked)?;
46+ checkout::apply(store, &root, &plan)
47+}
added crates/wc/tests/materialize.rs +75 −0
@@ -0,0 +1,75 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// Copyright (c) 2026 UtopiaIA
3+
4+//! Writing a changeset into a plain directory: the files only, with the
5+//! checkout's modes and its refusal to follow symlinks.
6+
7+mod common;
8+
9+use braid_format::{ChangeOp, GitId};
10+use braid_wc::{Error, materialize};
11+use common::{Fixture, listing};
12+
13+#[test]
14+fn a_changeset_is_written_with_its_files_only() {
15+ let f = Fixture::new();
16+ let id = f.commit(
17+ &[],
18+ vec![
19+ ("bin/run", f.exec("#!/bin/sh\n")),
20+ ("docs/a.md", f.file("a\n")),
21+ ("link", f.symlink(b"docs/a.md")),
22+ (
23+ "sub",
24+ ChangeOp::Gitlink {
25+ commit: GitId::Sha1([7; 20]),
26+ },
27+ ),
28+ ],
29+ );
30+ let dir = f.path("tree");
31+ assert_eq!(materialize(f.repo.store(), id, &dir).unwrap(), 4);
32+ assert_eq!(
33+ listing(&dir),
34+ ["bin", "bin/run", "docs", "docs/a.md", "link", "sub"]
35+ );
36+ assert_eq!(
37+ std::fs::read_to_string(dir.join("docs/a.md")).unwrap(),
38+ "a\n"
39+ );
40+ let link = std::fs::symlink_metadata(dir.join("link")).unwrap();
41+ assert!(link.file_type().is_symlink());
42+ assert_eq!(
43+ std::fs::read_link(dir.join("link")).unwrap(),
44+ std::path::Path::new("docs/a.md")
45+ );
46+ #[cfg(unix)]
47+ {
48+ use std::os::unix::fs::PermissionsExt;
49+ let mode = |path: &str| {
50+ std::fs::metadata(dir.join(path))
51+ .unwrap()
52+ .permissions()
53+ .mode()
54+ & 0o777
55+ };
56+ assert_eq!(mode("bin/run") & 0o111, 0o111);
57+ assert_eq!(mode("docs/a.md") & 0o111, 0);
58+ }
59+}
60+
61+#[test]
62+fn only_an_empty_or_missing_directory_is_written() {
63+ let f = Fixture::new();
64+ let id = f.commit(&[], vec![("a", f.file("a\n"))]);
65+ let empty = f.path("empty");
66+ std::fs::create_dir(&empty).unwrap();
67+ assert_eq!(materialize(f.repo.store(), id, &empty).unwrap(), 1);
68+ // Not into one that holds anything, such as what was just written.
69+ assert!(matches!(
70+ materialize(f.repo.store(), id, &empty),
71+ Err(Error::DirectoryNotEmpty(_))
72+ ));
73+ // The directory's parent must exist.
74+ assert!(materialize(f.repo.store(), id, &f.path("no/such")).is_err());
75+}