utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | bf223502faa3da1262b2783e6f6914e5a1676e6fa0e699a9ce0faee2bc8e829d |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 15:54 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-21 15:54 +02:00 |
| Parents | 9598fdbd3dec |
3 files changed
modified Cargo.lock +1 −0
| @@ -503,6 +503,7 @@ | ||
| 503 | 503 | version = "0.1.1" |
| 504 | 504 | dependencies = [ |
| 505 | 505 | "anyhow", |
| 506 | + "blake3", | |
| 506 | 507 | "clap", |
| 507 | 508 | "elffuss-braid-engine", |
| 508 | 509 | "elffuss-braid-format", |
modified crates/cli/Cargo.toml +1 −0
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | braid-store.workspace = true |
| 23 | 23 | braid-wc.workspace = true |
| 24 | 24 | anyhow.workspace = true |
| 25 | +blake3.workspace = true | |
| 25 | 26 | clap.workspace = true |
| 26 | 27 | serde.workspace = true |
| 27 | 28 | serde_json.workspace = true |
modified crates/cli/src/verify.rs +40 −2
| @@ -69,6 +69,10 @@ | ||
| 69 | 69 | /// than this long, in seconds, before now: a day. |
| 70 | 70 | const HISTORY_AGE: i64 = 86_400; |
| 71 | 71 | |
| 72 | +/// The longest record file name made of the branch's name in hex; a longer | |
| 73 | +/// name is hashed instead. | |
| 74 | +const MAX_HEX_NAME: usize = 200; | |
| 75 | + | |
| 72 | 76 | /// What the watchdog says once it runs, before braid starts any test. The |
| 73 | 77 | /// number changes with the protocol, so that another braid's watchdog is |
| 74 | 78 | /// never taken for this one's. |
| @@ -273,9 +277,16 @@ | ||
| 273 | 277 | |
| 274 | 278 | impl Record { |
| 275 | 279 | /// The file of `branch`: its name in hex, so that no two branches share |
| 276 | - /// a file, even on a file system that ignores case. | |
| 280 | + /// a file, even on a file system that ignores case, or for a name whose | |
| 281 | + /// hex would be longer than [`MAX_HEX_NAME`], `b3-` and the BLAKE3 hash | |
| 282 | + /// of the name, which no name in hex can look like. | |
| 277 | 283 | fn path(repo: &Repo, branch: &str) -> PathBuf { |
| 278 | - let name: String = branch.bytes().map(|byte| format!("{byte:02x}")).collect(); | |
| 284 | + let hex: String = branch.bytes().map(|byte| format!("{byte:02x}")).collect(); | |
| 285 | + let name = if hex.len() <= MAX_HEX_NAME { | |
| 286 | + hex | |
| 287 | + } else { | |
| 288 | + format!("b3-{}", blake3::hash(branch.as_bytes()).to_hex()) | |
| 289 | + }; | |
| 279 | 290 | repo.root() |
| 280 | 291 | .join(braid_repo::DIR) |
| 281 | 292 | .join("verify") |
| @@ -1322,6 +1333,33 @@ | ||
| 1322 | 1333 | assert!(err.contains("delete it"), "{err}"); |
| 1323 | 1334 | } |
| 1324 | 1335 | |
| 1336 | + #[test] | |
| 1337 | + fn long_branch_names_are_hashed() { | |
| 1338 | + let temp = tempfile::tempdir().unwrap(); | |
| 1339 | + let repo = Repo::init(temp.path()).unwrap(); | |
| 1340 | + let name = |branch: &str| { | |
| 1341 | + Record::path(&repo, branch) | |
| 1342 | + .file_name() | |
| 1343 | + .unwrap() | |
| 1344 | + .to_string_lossy() | |
| 1345 | + .into_owned() | |
| 1346 | + }; | |
| 1347 | + let longest = "b".repeat(MAX_HEX_NAME / 2); | |
| 1348 | + assert_eq!( | |
| 1349 | + name(&longest), | |
| 1350 | + format!("{}.json", "62".repeat(longest.len())) | |
| 1351 | + ); | |
| 1352 | + let longer = format!("{longest}x"); | |
| 1353 | + let hashed = name(&longer); | |
| 1354 | + assert_eq!( | |
| 1355 | + hashed, | |
| 1356 | + format!("b3-{}.json", blake3::hash(longer.as_bytes()).to_hex()) | |
| 1357 | + ); | |
| 1358 | + assert_ne!(name(&format!("{longest}y")), hashed); | |
| 1359 | + // Even the longest branch name a ref can have fits a file name. | |
| 1360 | + assert!(name(&"z".repeat(4096)).len() < 100); | |
| 1361 | + } | |
| 1362 | + | |
| 1325 | 1363 | #[test] |
| 1326 | 1364 | fn the_hourly_cap_counts_by_each_reverts_own_time() { |
| 1327 | 1365 | let temp = tempfile::tempdir().unwrap(); |