Skip to main content

packed_repository/
packed_repository.rs

1//! Read exact bytes from an existing loose or packed repository without modifying it.
2//!
3//! Run `cargo run --example packed_repository -- /path/to/repo <object-id>` with a full SHA-1 or
4//! SHA-256 identity matching the repository's format.
5//! To prepare a disposable packed repository, create commits and run `git repack -ad` there.
6use std::io::{self, Write};
7use std::path::PathBuf;
8
9use clap::Parser;
10use girt::{ObjectId, Repository};
11
12#[derive(Parser)]
13#[command(about, long_about = None)]
14struct Args {
15    /// Repository to open.
16    path: PathBuf,
17    /// Full object identity matching the repository's object format.
18    id: ObjectId,
19}
20
21fn main() -> Result<(), Box<dyn std::error::Error>> {
22    let args = Args::parse();
23    let repository = Repository::open(args.path)?;
24    let objects = repository.objects()?;
25    let object = objects.read(args.id)?.ok_or("object absent")?;
26
27    eprintln!(
28        "{} {} ({} bytes)",
29        object.kind().as_str(),
30        object.id(),
31        object.data().len()
32    );
33    io::stdout().write_all(object.data())?;
34    Ok(())
35}