utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | f42277d3e849257e8494481f415a9f4724c3a81fd5312cf1c0ffd014aee8a10f |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:02 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:02 +02:00 |
| Parents | 290ce4f0990f |
4 files changed
modified crates/cli/src/work.rs +1 −1
| @@ -170,7 +170,7 @@ | ||
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | 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)?) | |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | /// Paths given on the command line, as paths of the workspace. |
modified crates/wc/src/error.rs +1 −1
| @@ -159,7 +159,7 @@ | ||
| 159 | 159 | |
| 160 | 160 | /// No author is configured. |
| 161 | 161 | #[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" | |
| 163 | 163 | )] |
| 164 | 164 | NoAuthor, |
| 165 | 165 |
modified crates/wc/src/identity.rs +37 −58
| @@ -1,32 +1,20 @@ | ||
| 1 | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | 3 | |
| 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`]). | |
| 14 | 7 | //! |
| 15 | 8 | //! Signatures record the current time with an offset of zero: braid does |
| 16 | 9 | //! not work out the local time zone yet, so times read as UTC. |
| 17 | 10 | |
| 18 | -use std::fs; | |
| 19 | -use std::io; | |
| 20 | -use std::path::Path; | |
| 21 | 11 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 22 | 12 | |
| 23 | 13 | use braid_format::Signature; |
| 14 | +use braid_repo::Repo; | |
| 24 | 15 | |
| 25 | 16 | use crate::error::Error; |
| 26 | 17 | |
| 27 | -/// The repository's settings file, inside `.braid`. | |
| 28 | -pub const CONFIG_FILE: &str = "config"; | |
| 29 | - | |
| 30 | 18 | /// A name and an e-mail address. |
| 31 | 19 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 32 | 20 | pub struct Identity { |
| @@ -71,47 +59,29 @@ | ||
| 71 | 59 | }) |
| 72 | 60 | } |
| 73 | 61 | |
| 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); | |
| 82 | 68 | }; |
| 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 | ||
| 75 | + .ok_or_else(|| invalid("the config sets a name but no email"))?; | |
| 76 | + Identity::new(&name, &email).map(Some).map_err(invalid) | |
| 107 | 77 | } |
| 108 | 78 | |
| 109 | 79 | /// The identity given (by `--author` or `BRAID_AUTHOR`), or else the |
| 110 | 80 | /// 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> { | |
| 112 | 82 | match given { |
| 113 | 83 | 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), | |
| 115 | 85 | } |
| 116 | 86 | } |
| 117 | 87 | |
| @@ -155,21 +125,30 @@ | ||
| 155 | 125 | #[test] |
| 156 | 126 | fn config() { |
| 157 | 127 | 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); | |
| 159 | 130 | assert!(matches!( |
| 160 | - Identity::resolve(None, dir.path()), | |
| 131 | + Identity::resolve(None, &repo), | |
| 161 | 132 | Err(Error::NoAuthor) |
| 162 | 133 | )); |
| 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", | |
| 167 | 146 | ) |
| 168 | 147 | .unwrap(); |
| 169 | - let grace = Identity::resolve(None, dir.path()).unwrap(); | |
| 148 | + let grace = Identity::resolve(None, &repo).unwrap(); | |
| 170 | 149 | assert_eq!(grace.name, "Grace Hopper"); |
| 171 | 150 | 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(); | |
| 173 | 152 | assert_eq!(given.name, "Ada"); |
| 174 | 153 | } |
| 175 | 154 | } |
modified crates/wc/src/lib.rs +1 −1
| @@ -69,7 +69,7 @@ | ||
| 69 | 69 | pub use change::{ChangeRequest, Expect, create_change, land_change}; |
| 70 | 70 | pub use error::Error; |
| 71 | 71 | pub use fsprobe::FileSystem; |
| 72 | -pub use identity::{CONFIG_FILE, Identity}; | |
| 72 | +pub use identity::Identity; | |
| 73 | 73 | pub use paths::workspace_path; |
| 74 | 74 | pub use state::{Checkout, Rebase, RecordedConflict, State}; |
| 75 | 75 | pub use status::{ChangeKind, GUARD_SIZE, PathChange, Status, is_guarded}; |