utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | c03d76685fb33bcdc652e8b553500edf4696228b9524c668dccfe7557dbbfa6b |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:38 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:38 +02:00 |
| Parents | ab37fe03cdad |
8 files changed
modified Cargo.lock +1 −0
| @@ -530,6 +530,7 @@ | ||
| 530 | 530 | "elffuss-braid-format", |
| 531 | 531 | "elffuss-braid-git", |
| 532 | 532 | "elffuss-braid-store", |
| 533 | + "jiff", | |
| 533 | 534 | "tempfile", |
| 534 | 535 | "thiserror", |
| 535 | 536 | ] |
modified crates/cli/tests/cli.rs +50 −0
| @@ -247,6 +247,56 @@ | ||
| 247 | 247 | assert!(err.contains("is a blob, not a changeset"), "{err}"); |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | +#[test] | |
| 251 | +fn signatures_carry_the_local_offset() { | |
| 252 | + let dir = tempfile::tempdir().unwrap(); | |
| 253 | + assert!( | |
| 254 | + braid() | |
| 255 | + .arg("init") | |
| 256 | + .arg(dir.path()) | |
| 257 | + .output() | |
| 258 | + .unwrap() | |
| 259 | + .status | |
| 260 | + .success() | |
| 261 | + ); | |
| 262 | + let file = dir.path().join("a.txt"); | |
| 263 | + std::fs::write(&file, b"a\n").unwrap(); | |
| 264 | + let set = format!("a.txt=@{}", file.display()); | |
| 265 | + // POSIX rules, UTC, and a zone that cannot be found, which reads as UTC. | |
| 266 | + for (zone, minutes, shown) in [ | |
| 267 | + ("<+0530>-05:30", 330, "+0530"), | |
| 268 | + ("<-03>3", -180, "-0300"), | |
| 269 | + ("UTC0", 0, "+0000"), | |
| 270 | + ("Nowhere/Nothing", 0, "+0000"), | |
| 271 | + ] { | |
| 272 | + let made = run_json( | |
| 273 | + braid_in(dir.path()) | |
| 274 | + .env("TZ", zone) | |
| 275 | + .env("BRAID_AUTHOR", "Ada <ada@example.org>") | |
| 276 | + .args(["change", "create", "--base", "none", "--set", &set]) | |
| 277 | + .args(["-m", zone]), | |
| 278 | + ); | |
| 279 | + let id = made["change"].as_str().unwrap(); | |
| 280 | + // JSON keeps the seconds and the offset. | |
| 281 | + let show = run_json(braid_in(dir.path()).args(["show", id])); | |
| 282 | + for who in ["author", "committer"] { | |
| 283 | + assert_eq!(show[who]["offset_minutes"], minutes, "{zone}"); | |
| 284 | + assert!(show[who]["seconds"].as_i64().unwrap() > 1_700_000_000); | |
| 285 | + let time = show[who]["time"].as_str().unwrap(); | |
| 286 | + assert!(time.ends_with(shown), "{zone}: {time}"); | |
| 287 | + } | |
| 288 | + // The log shows minutes, in the signature's own offset. | |
| 289 | + let log = braid_in(dir.path()) | |
| 290 | + .args(["log", id, "-n", "1"]) | |
| 291 | + .output() | |
| 292 | + .unwrap(); | |
| 293 | + let text = String::from_utf8(log.stdout).unwrap(); | |
| 294 | + let time = text.split(" ").nth(1).unwrap(); | |
| 295 | + assert_eq!(time.len(), "2026-10-15 21:41 +0530".len(), "{text}"); | |
| 296 | + assert!(time.ends_with(shown), "{zone}: {text}"); | |
| 297 | + } | |
| 298 | +} | |
| 299 | + | |
| 250 | 300 | #[test] |
| 251 | 301 | fn commands_outside_a_repository_fail_clearly() { |
| 252 | 302 | let dir = tempfile::tempdir().unwrap(); |
modified crates/repo/Cargo.toml +7 −0
| @@ -18,6 +18,13 @@ | ||
| 18 | 18 | braid-git.workspace = true |
| 19 | 19 | braid-engine.workspace = true |
| 20 | 20 | blake3.workspace = true |
| 21 | +# The local time zone, read without calling into the C library. Only the | |
| 22 | +# system's own time zone database: nothing bundled. | |
| 23 | +jiff = { version = "0.2.38", default-features = false, features = [ | |
| 24 | + "std", | |
| 25 | + "tz-system", | |
| 26 | + "tzdb-zoneinfo", | |
| 27 | +] } | |
| 21 | 28 | thiserror = "2" |
| 22 | 29 | |
| 23 | 30 | [dev-dependencies] |
added crates/repo/src/clock.rs +50 −0
| @@ -0,0 +1,50 @@ | ||
| 1 | +// SPDX-License-Identifier: Apache-2.0 | |
| 2 | +// Copyright (c) 2026 UtopiaIA | |
| 3 | + | |
| 4 | +//! The time a changeset is made, in the local time zone. | |
| 5 | +//! | |
| 6 | +//! The time zone is the system's, as [jiff](https://docs.rs/jiff) finds it | |
| 7 | +//! without calling into the C library: the `TZ` environment variable, a | |
| 8 | +//! POSIX rule or a name in the system's time zone database, or else the | |
| 9 | +//! system setting (on Unix, `/etc/localtime`). When it cannot be determined, | |
| 10 | +//! the offset is zero. | |
| 11 | + | |
| 12 | +use braid_format::Signature; | |
| 13 | +use jiff::Timestamp; | |
| 14 | +use jiff::tz::TimeZone; | |
| 15 | + | |
| 16 | +/// The current time: seconds since the Unix epoch, and the offset of the | |
| 17 | +/// local time zone from UTC at that instant, in minutes. | |
| 18 | +pub fn local_now() -> (i64, i64) { | |
| 19 | + let now = Timestamp::now(); | |
| 20 | + let offset = | |
| 21 | + TimeZone::try_system().map_or(0, |zone| i64::from(zone.to_offset(now).seconds()) / 60); | |
| 22 | + (now.as_second(), offset) | |
| 23 | +} | |
| 24 | + | |
| 25 | +/// A signature by `name` and `email`, made now, in local time. | |
| 26 | +pub fn signature_now(name: &[u8], email: &[u8]) -> Signature { | |
| 27 | + let (seconds, offset_minutes) = local_now(); | |
| 28 | + Signature { | |
| 29 | + name: name.to_vec(), | |
| 30 | + email: email.to_vec(), | |
| 31 | + seconds, | |
| 32 | + offset_minutes, | |
| 33 | + } | |
| 34 | +} | |
| 35 | + | |
| 36 | +#[cfg(test)] | |
| 37 | +mod tests { | |
| 38 | + use super::*; | |
| 39 | + | |
| 40 | + #[test] | |
| 41 | + fn now_has_a_real_offset() { | |
| 42 | + let (seconds, offset) = local_now(); | |
| 43 | + assert!(seconds > 1_700_000_000); | |
| 44 | + // Every time zone in use is within a day of UTC. | |
| 45 | + assert!(offset.abs() < 24 * 60, "{offset}"); | |
| 46 | + let signature = signature_now(b"Ada", b"ada@example.org"); | |
| 47 | + assert_eq!(signature.name, b"Ada"); | |
| 48 | + assert!(signature.seconds >= seconds); | |
| 49 | + } | |
| 50 | +} |
modified crates/repo/src/lib.rs +2 −0
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | |
| 21 | 21 | #![forbid(unsafe_code)] |
| 22 | 22 | |
| 23 | +mod clock; | |
| 23 | 24 | mod config; |
| 24 | 25 | mod git; |
| 25 | 26 | mod link; |
| @@ -35,6 +36,7 @@ | ||
| 35 | 36 | use braid_store::{Options, Store}; |
| 36 | 37 | |
| 37 | 38 | pub use braid_engine::{Conflict, ConflictHunk, ConflictKind, HunkSide}; |
| 39 | +pub use clock::{local_now, signature_now}; | |
| 38 | 40 | pub use config::{CONFIG_FILE, Config}; |
| 39 | 41 | pub use git::{ |
| 40 | 42 | GIT_EXPORT_AGENT, GitImport, MAP_REF, REMOTE_PREFIX, braid_ref_for_git, git_ref_for_braid, |
modified crates/repo/src/sync.rs +1 −10
| @@ -8,7 +8,6 @@ | ||
| 8 | 8 | use std::ffi::OsString; |
| 9 | 9 | use std::fs::{self, File, OpenOptions}; |
| 10 | 10 | use std::path::{Path, PathBuf}; |
| 11 | -use std::time::{SystemTime, UNIX_EPOCH}; | |
| 12 | 11 | |
| 13 | 12 | use braid_engine::{Conflict, MergeOutcome, is_ancestor, merge}; |
| 14 | 13 | use braid_format::{Kind, MemoryObjects, ObjectId, Objects, Signature}; |
| @@ -267,15 +266,7 @@ | ||
| 267 | 266 | } |
| 268 | 267 | None => format!("{}@example.org", email_name(agent)), |
| 269 | 268 | }; |
| 270 | - let seconds = SystemTime::now() | |
| 271 | - .duration_since(UNIX_EPOCH) | |
| 272 | - .map_or(0, |elapsed| i64::try_from(elapsed.as_secs()).unwrap_or(0)); | |
| 273 | - Ok(Signature { | |
| 274 | - name: agent.as_bytes().to_vec(), | |
| 275 | - email: email.into_bytes(), | |
| 276 | - seconds, | |
| 277 | - offset_minutes: 0, | |
| 278 | - }) | |
| 269 | + Ok(crate::signature_now(agent.as_bytes(), email.as_bytes())) | |
| 279 | 270 | } |
| 280 | 271 | } |
| 281 | 272 |
modified crates/wc/src/identity.rs +7 −15
| @@ -5,10 +5,9 @@ | ||
| 5 | 5 | //! `name` and `email` of the repository's `.braid/config` (see |
| 6 | 6 | //! [`braid_repo::Config`]). |
| 7 | 7 | //! |
| 8 | -//! Signatures record the current time with an offset of zero: braid does | |
| 9 | -//! not work out the local time zone yet, so times read as UTC. | |
| 10 | - | |
| 11 | -use std::time::{SystemTime, UNIX_EPOCH}; | |
| 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`]). | |
| 12 | 11 | |
| 13 | 12 | use braid_format::Signature; |
| 14 | 13 | use braid_repo::Repo; |
| @@ -85,17 +84,9 @@ | ||
| 85 | 84 | } |
| 86 | 85 | } |
| 87 | 86 | |
| 88 | - /// A signature by this identity, now, with an offset of zero. | |
| 87 | + /// A signature by this identity, now, in local time. | |
| 89 | 88 | pub fn now(&self) -> Signature { |
| 90 | - let seconds = SystemTime::now() | |
| 91 | - .duration_since(UNIX_EPOCH) | |
| 92 | - .map_or(0, |since| since.as_secs() as i64); | |
| 93 | - Signature { | |
| 94 | - name: self.name.as_bytes().to_vec(), | |
| 95 | - email: self.email.as_bytes().to_vec(), | |
| 96 | - seconds, | |
| 97 | - offset_minutes: 0, | |
| 98 | - } | |
| 89 | + braid_repo::signature_now(self.name.as_bytes(), self.email.as_bytes()) | |
| 99 | 90 | } |
| 100 | 91 | } |
| 101 | 92 | |
| @@ -118,8 +109,9 @@ | ||
| 118 | 109 | assert!(Identity::parse(bad).is_err(), "{bad:?}"); |
| 119 | 110 | } |
| 120 | 111 | let signature = ada.now(); |
| 121 | - assert_eq!(signature.offset_minutes, 0); | |
| 112 | + assert_eq!(signature.email, b"ada@example.org"); | |
| 122 | 113 | assert!(signature.seconds > 1_700_000_000); |
| 114 | + assert!(signature.offset_minutes.abs() < 24 * 60); | |
| 123 | 115 | } |
| 124 | 116 | |
| 125 | 117 | #[test] |
modified docs/design.md +4 −2
| @@ -145,8 +145,10 @@ | ||
| 145 | 145 | create` takes the base, the new contents and the deletions, and can require |
| 146 | 146 | what the base holds at each path first, so that an agent never overwrites |
| 147 | 147 | an edit it did not see. |
| 148 | -- Signatures record the time in UTC for now: the local time zone offset is not | |
| 149 | - computed yet. | |
| 148 | +- Signatures record the time with the offset of the local time zone, which | |
| 149 | + [jiff](https://github.com/BurntSushi/jiff) reads from `TZ` or the system's | |
| 150 | + setting without calling into the C library; when it cannot be determined, | |
| 151 | + the offset is zero. | |
| 150 | 152 | |
| 151 | 153 | ## Git bridge |
| 152 | 154 |