Elffuss BraidHub

utopia-ia/elffuss-braid public

Workspaces: take the config identity from Repo::config

The author of a commit comes from --author, then BRAID_AUTHOR, then the
name and email that .braid/config sets, now read with the repository's
own config parser instead of a second one. A config with a name and no
email is refused with a clear error.
Changesetf42277d3e849257e8494481f415a9f4724c3a81fd5312cf1c0ffd014aee8a10f
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:02 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:02 +02:00
Parents290ce4f0990f

Browse the files at this changeset

4 files changed

modified crates/cli/src/work.rs +1 −1
@@ -170,7 +170,7 @@
170170 }
171171
172172 fn identity(args: &IdentityArgs, repo: &Repo) -> Result<Identity> {
173- Ok(Identity::resolve(args.author.as_deref(), repo.root())?)
173+ Ok(Identity::resolve(args.author.as_deref(), repo)?)
174174 }
175175
176176 /// Paths given on the command line, as paths of the workspace.
modified crates/wc/src/error.rs +1 −1
@@ -159,7 +159,7 @@
159159
160160 /// No author is configured.
161161 #[error(
162- "no author: pass --author \"Name <email>\", set BRAID_AUTHOR, or put name and email in .braid/config"
162+ "no author: pass --author \"Name <email>\", set BRAID_AUTHOR, or set name and email in .braid/config"
163163 )]
164164 NoAuthor,
165165
modified crates/wc/src/identity.rs +37 −58
@@ -1,32 +1,20 @@
11 // SPDX-License-Identifier: Apache-2.0
22 // Copyright (c) 2026 UtopiaIA
33
4-//! Who makes a change: from `--author`, `BRAID_AUTHOR`, or the repository's
5-//! `.braid/config`.
6-//!
7-//! The config is a small text file of `key = value` lines; `#` starts a
8-//! comment. Two keys are read:
9-//!
10-//! ```text
11-//! name = Ada Lovelace
12-//! email = ada@example.org
13-//! ```
4+//! Who makes a change: from `--author`, then `BRAID_AUTHOR`, then the
5+//! `name` and `email` of the repository's `.braid/config` (see
6+//! [`braid_repo::Config`]).
147 //!
158 //! Signatures record the current time with an offset of zero: braid does
169 //! not work out the local time zone yet, so times read as UTC.
1710
18-use std::fs;
19-use std::io;
20-use std::path::Path;
2111 use std::time::{SystemTime, UNIX_EPOCH};
2212
2313 use braid_format::Signature;
14+use braid_repo::Repo;
2415
2516 use crate::error::Error;
2617
27-/// The repository's settings file, inside `.braid`.
28-pub const CONFIG_FILE: &str = "config";
29-
3018 /// A name and an e-mail address.
3119 #[derive(Clone, Debug, PartialEq, Eq)]
3220 pub struct Identity {
@@ -71,47 +59,29 @@
7159 })
7260 }
7361
74- /// The identity in the config of the repository whose root is
75- /// `repo_root`, if it names one.
76- pub fn from_config(repo_root: &Path) -> Result<Option<Identity>, Error> {
77- let path = repo_root.join(braid_repo::DIR).join(CONFIG_FILE);
78- let text = match fs::read_to_string(&path) {
79- Ok(text) => text,
80- Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
81- Err(err) => return Err(Error::io("read", &path)(err)),
62+ /// The identity that the repository's `.braid/config` sets, if it sets
63+ /// a name. It must set an email address too.
64+ pub fn from_config(repo: &Repo) -> Result<Option<Identity>, Error> {
65+ let config = repo.config()?;
66+ let Some(name) = config.name else {
67+ return Ok(None);
8268 };
83- let mut name = None;
84- let mut email = None;
85- for line in text.lines() {
86- let line = line.split('#').next().unwrap_or("").trim();
87- let Some((key, value)) = line.split_once('=') else {
88- continue;
89- };
90- match key.trim() {
91- "name" => name = Some(value.trim().to_owned()),
92- "email" => email = Some(value.trim().to_owned()),
93- _ => {}
94- }
95- }
96- match (name, email) {
97- (Some(name), Some(email)) => {
98- Identity::new(&name, &email)
99- .map(Some)
100- .map_err(|reason| Error::InvalidAuthor {
101- text: format!("{name} <{email}>"),
102- reason,
103- })
104- }
105- _ => Ok(None),
106- }
69+ let invalid = |reason| Error::InvalidAuthor {
70+ text: format!("name = {name} in .braid/config"),
71+ reason,
72+ };
73+ let email = config
74+ .email
75+ .ok_or_else(|| invalid("the config sets a name but no email"))?;
76+ Identity::new(&name, &email).map(Some).map_err(invalid)
10777 }
10878
10979 /// The identity given (by `--author` or `BRAID_AUTHOR`), or else the
11080 /// one in the repository's config.
111- pub fn resolve(given: Option<&str>, repo_root: &Path) -> Result<Identity, Error> {
81+ pub fn resolve(given: Option<&str>, repo: &Repo) -> Result<Identity, Error> {
11282 match given {
11383 Some(text) => Identity::parse(text),
114- None => Identity::from_config(repo_root)?.ok_or(Error::NoAuthor),
84+ None => Identity::from_config(repo)?.ok_or(Error::NoAuthor),
11585 }
11686 }
11787
@@ -155,21 +125,30 @@
155125 #[test]
156126 fn config() {
157127 let dir = tempfile::tempdir().unwrap();
158- assert_eq!(Identity::from_config(dir.path()).unwrap(), None);
128+ let repo = Repo::init(dir.path()).unwrap();
129+ assert_eq!(Identity::from_config(&repo).unwrap(), None);
159130 assert!(matches!(
160- Identity::resolve(None, dir.path()),
131+ Identity::resolve(None, &repo),
161132 Err(Error::NoAuthor)
162133 ));
163- fs::create_dir(dir.path().join(braid_repo::DIR)).unwrap();
164- fs::write(
165- dir.path().join(braid_repo::DIR).join(CONFIG_FILE),
166- "# who commits\nname = Grace Hopper\nemail = grace@example.org # work\n",
134+ let config = dir
135+ .path()
136+ .join(braid_repo::DIR)
137+ .join(braid_repo::CONFIG_FILE);
138+ std::fs::write(&config, "# who commits\nname = Grace Hopper\n").unwrap();
139+ assert!(matches!(
140+ Identity::resolve(None, &repo),
141+ Err(Error::InvalidAuthor { .. })
142+ ));
143+ std::fs::write(
144+ &config,
145+ "# who commits\nname = Grace Hopper\nemail = grace@example.org\n",
167146 )
168147 .unwrap();
169- let grace = Identity::resolve(None, dir.path()).unwrap();
148+ let grace = Identity::resolve(None, &repo).unwrap();
170149 assert_eq!(grace.name, "Grace Hopper");
171150 assert_eq!(grace.email, "grace@example.org");
172- let given = Identity::resolve(Some("Ada <ada@example.org>"), dir.path()).unwrap();
151+ let given = Identity::resolve(Some("Ada <ada@example.org>"), &repo).unwrap();
173152 assert_eq!(given.name, "Ada");
174153 }
175154 }
modified crates/wc/src/lib.rs +1 −1
@@ -69,7 +69,7 @@
6969 pub use change::{ChangeRequest, Expect, create_change, land_change};
7070 pub use error::Error;
7171 pub use fsprobe::FileSystem;
72-pub use identity::{CONFIG_FILE, Identity};
72+pub use identity::Identity;
7373 pub use paths::workspace_path;
7474 pub use state::{Checkout, Rebase, RecordedConflict, State};
7575 pub use status::{ChangeKind, GUARD_SIZE, PathChange, Status, is_guarded};