| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (c) 2026 UtopiaIA |
| 3 | |
| 4 | //! `braid`, the command-line tool. |
| 5 | |
| 6 | use std::fs::File; |
| 7 | use std::io::{self, Read, Write}; |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | use std::process::ExitCode; |
| 10 | |
| 11 | use anyhow::{Context, Result}; |
| 12 | use braid_format::{IdHasher, Kind, ObjectId}; |
| 13 | use clap::{Parser, Subcommand}; |
| 14 | |
| 15 | /// Version control for many agents landing changes at once. |
| 16 | #[derive(Debug, Parser)] |
| 17 | #[command(name = "braid", version, about, long_about = None)] |
| 18 | struct Cli { |
| 19 | /// Print machine-readable JSON instead of text. |
| 20 | #[arg(long, global = true)] |
| 21 | json: bool, |
| 22 | |
| 23 | #[command(subcommand)] |
| 24 | command: Command, |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug, Subcommand)] |
| 28 | enum Command { |
| 29 | /// Print the braid blob id of each file ("-" reads standard input). |
| 30 | HashObject { |
| 31 | /// Files to hash. |
| 32 | #[arg(required = true)] |
| 33 | paths: Vec<PathBuf>, |
| 34 | }, |
| 35 | } |
| 36 | |
| 37 | fn main() -> ExitCode { |
| 38 | let cli = Cli::parse(); |
| 39 | match run(cli) { |
| 40 | Ok(()) => ExitCode::SUCCESS, |
| 41 | Err(err) => { |
| 42 | eprintln!("braid: error: {err:#}"); |
| 43 | ExitCode::FAILURE |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn run(cli: Cli) -> Result<()> { |
| 49 | match cli.command { |
| 50 | Command::HashObject { paths } => hash_object(&paths, cli.json), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | struct Hashed<'a> { |
| 55 | path: &'a Path, |
| 56 | id: ObjectId, |
| 57 | size: u64, |
| 58 | } |
| 59 | |
| 60 | fn hash_object(paths: &[PathBuf], json: bool) -> Result<()> { |
| 61 | let mut hashed = Vec::with_capacity(paths.len()); |
| 62 | for path in paths { |
| 63 | let (id, size) = if path.as_os_str() == "-" { |
| 64 | hash_blob(io::stdin().lock()).context("cannot read standard input")? |
| 65 | } else { |
| 66 | let file = |
| 67 | File::open(path).with_context(|| format!("cannot open {}", path.display()))?; |
| 68 | hash_blob(file).with_context(|| format!("cannot read {}", path.display()))? |
| 69 | }; |
| 70 | hashed.push(Hashed { path, id, size }); |
| 71 | } |
| 72 | |
| 73 | let mut out = io::stdout().lock(); |
| 74 | if json { |
| 75 | let items: Vec<serde_json::Value> = hashed |
| 76 | .iter() |
| 77 | .map(|h| { |
| 78 | serde_json::json!({ |
| 79 | "path": h.path.to_string_lossy(), |
| 80 | "kind": Kind::Blob.name(), |
| 81 | "id": h.id.to_hex(), |
| 82 | "size": h.size, |
| 83 | }) |
| 84 | }) |
| 85 | .collect(); |
| 86 | serde_json::to_writer(&mut out, &items)?; |
| 87 | writeln!(out)?; |
| 88 | } else { |
| 89 | for h in &hashed { |
| 90 | writeln!(out, "{} {}", h.id, h.path.display())?; |
| 91 | } |
| 92 | } |
| 93 | Ok(()) |
| 94 | } |
| 95 | |
| 96 | /// Hashes a stream as a blob without holding it in memory. |
| 97 | fn hash_blob(mut reader: impl Read) -> io::Result<(ObjectId, u64)> { |
| 98 | let mut hasher = IdHasher::new(Kind::Blob); |
| 99 | let mut buf = vec![0u8; 256 * 1024]; |
| 100 | let mut size = 0u64; |
| 101 | loop { |
| 102 | let n = match reader.read(&mut buf) { |
| 103 | Ok(0) => break, |
| 104 | Ok(n) => n, |
| 105 | Err(err) if err.kind() == io::ErrorKind::Interrupted => continue, |
| 106 | Err(err) => return Err(err), |
| 107 | }; |
| 108 | hasher.update(&buf[..n]); |
| 109 | size += n as u64; |
| 110 | } |
| 111 | Ok((hasher.finalize(), size)) |
| 112 | } |