| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 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`]). |
| 7 | //! |
| 8 | //! Signatures record the current time with the offset of the local time |
| 9 | //! zone, or of UTC when it cannot be determined (see |
| 10 | //! [`braid_repo::local_now`]). |
| 11 | |
| 12 | use braid_format::Signature; |
| 13 | use braid_repo::Repo; |
| 14 | |
| 15 | use crate::error::Error; |
| 16 | |
| 17 | /// A name and an e-mail address. |
| 18 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 19 | pub struct Identity { |
| 20 | /// The name. |
| 21 | pub name: String, |
| 22 | /// The e-mail address. |
| 23 | pub email: String, |
| 24 | } |
| 25 | |
| 26 | impl Identity { |
| 27 | /// Reads `Name <email>`. |
| 28 | pub fn parse(text: &str) -> Result<Identity, Error> { |
| 29 | let invalid = |reason| Error::InvalidAuthor { |
| 30 | text: text.to_owned(), |
| 31 | reason, |
| 32 | }; |
| 33 | let (name, rest) = text |
| 34 | .split_once('<') |
| 35 | .ok_or_else(|| invalid("expected \"Name <email>\""))?; |
| 36 | let (email, after) = rest |
| 37 | .split_once('>') |
| 38 | .ok_or_else(|| invalid("expected \"Name <email>\""))?; |
| 39 | if !after.trim().is_empty() { |
| 40 | return Err(invalid("nothing may follow the e-mail address")); |
| 41 | } |
| 42 | Identity::new(name.trim(), email.trim()).map_err(invalid) |
| 43 | } |
| 44 | |
| 45 | /// An identity from its parts, which must not hold `<`, `>`, a newline |
| 46 | /// or NUL, so that it can be written as a git signature too. |
| 47 | pub fn new(name: &str, email: &str) -> Result<Identity, &'static str> { |
| 48 | let bad = |text: &str| text.contains(['<', '>', '\n', '\r', '\0']); |
| 49 | if name.is_empty() { |
| 50 | return Err("the name is empty"); |
| 51 | } |
| 52 | if bad(name) || bad(email) { |
| 53 | return Err("a signature cannot hold '<', '>', a newline or NUL"); |
| 54 | } |
| 55 | Ok(Identity { |
| 56 | name: name.to_owned(), |
| 57 | email: email.to_owned(), |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | /// The identity that the repository's `.braid/config` sets, if it sets |
| 62 | /// a name. It must set an email address too. |
| 63 | pub fn from_config(repo: &Repo) -> Result<Option<Identity>, Error> { |
| 64 | let config = repo.config()?; |
| 65 | let Some(name) = config.name else { |
| 66 | return Ok(None); |
| 67 | }; |
| 68 | let invalid = |reason| Error::InvalidAuthor { |
| 69 | text: format!("name = {name} in .braid/config"), |
| 70 | reason, |
| 71 | }; |
| 72 | let email = config |
| 73 | .email |
| 74 | .ok_or_else(|| invalid("the config sets a name but no email"))?; |
| 75 | Identity::new(&name, &email).map(Some).map_err(invalid) |
| 76 | } |
| 77 | |
| 78 | /// The identity given (by `--author` or `BRAID_AUTHOR`), or else the |
| 79 | /// one in the repository's config. |
| 80 | pub fn resolve(given: Option<&str>, repo: &Repo) -> Result<Identity, Error> { |
| 81 | match given { |
| 82 | Some(text) => Identity::parse(text), |
| 83 | None => Identity::from_config(repo)?.ok_or(Error::NoAuthor), |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// A signature by this identity, now, in local time. |
| 88 | pub fn now(&self) -> Signature { |
| 89 | braid_repo::signature_now(self.name.as_bytes(), self.email.as_bytes()) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use super::*; |
| 96 | |
| 97 | #[test] |
| 98 | fn parsing() { |
| 99 | let ada = Identity::parse("Ada Lovelace <ada@example.org>").unwrap(); |
| 100 | assert_eq!(ada.name, "Ada Lovelace"); |
| 101 | assert_eq!(ada.email, "ada@example.org"); |
| 102 | for bad in [ |
| 103 | "Ada", |
| 104 | "<ada@example.org>", |
| 105 | "Ada <a>b>", |
| 106 | "Ada <ada> extra", |
| 107 | "A\nB <x>", |
| 108 | ] { |
| 109 | assert!(Identity::parse(bad).is_err(), "{bad:?}"); |
| 110 | } |
| 111 | let signature = ada.now(); |
| 112 | assert_eq!(signature.email, b"ada@example.org"); |
| 113 | assert!(signature.seconds > 1_700_000_000); |
| 114 | assert!(signature.offset_minutes.abs() < 24 * 60); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn config() { |
| 119 | let dir = tempfile::tempdir().unwrap(); |
| 120 | let repo = Repo::init(dir.path()).unwrap(); |
| 121 | assert_eq!(Identity::from_config(&repo).unwrap(), None); |
| 122 | assert!(matches!( |
| 123 | Identity::resolve(None, &repo), |
| 124 | Err(Error::NoAuthor) |
| 125 | )); |
| 126 | let config = dir |
| 127 | .path() |
| 128 | .join(braid_repo::DIR) |
| 129 | .join(braid_repo::CONFIG_FILE); |
| 130 | std::fs::write(&config, "# who commits\nname = Grace Hopper\n").unwrap(); |
| 131 | assert!(matches!( |
| 132 | Identity::resolve(None, &repo), |
| 133 | Err(Error::InvalidAuthor { .. }) |
| 134 | )); |
| 135 | std::fs::write( |
| 136 | &config, |
| 137 | "# who commits\nname = Grace Hopper\nemail = grace@example.org\n", |
| 138 | ) |
| 139 | .unwrap(); |
| 140 | let grace = Identity::resolve(None, &repo).unwrap(); |
| 141 | assert_eq!(grace.name, "Grace Hopper"); |
| 142 | assert_eq!(grace.email, "grace@example.org"); |
| 143 | let given = Identity::resolve(Some("Ada <ada@example.org>"), &repo).unwrap(); |
| 144 | assert_eq!(given.name, "Ada"); |
| 145 | } |
| 146 | } |