Skip to main content

rewrites/
rewrites.rs

1//! Emit inferred relationships for two stored trees using the default rename policy.
2//! Run `cargo run --example rewrites -- REPOSITORY OLD_TREE NEW_TREE`.
3//! Each NUL-delimited record contains an `R` or `C` marker, a three-digit similarity score, and
4//! source and target byte paths.
5use std::io::{self, Write};
6use std::path::PathBuf;
7use std::sync::atomic::AtomicBool;
8
9use clap::Parser;
10use girt::rewrites::{Limits, Options};
11use girt::{ObjectId, Repository};
12
13#[derive(Parser)]
14#[command(about, long_about = None)]
15struct Args {
16    /// Repository to open.
17    repository: PathBuf,
18    /// ID of the old tree.
19    old_tree: ObjectId,
20    /// ID of the new tree.
21    new_tree: ObjectId,
22}
23
24fn main() -> girt::Result<()> {
25    let args = Args::parse();
26    let repo = Repository::open(args.repository)?;
27    let objects = repo.objects()?;
28
29    let records = objects.detect_rewrites(
30        Some(args.old_tree),
31        Some(args.new_tree),
32        Options::default(),
33        Limits::default(),
34        None,
35        &AtomicBool::new(false),
36    )?;
37
38    let mut output = io::stdout().lock();
39    for record in records {
40        write!(output, "{}{:03}\0", record.kind.letter(), record.similarity)?;
41        output.write_all(&record.source)?;
42        output.write_all(&[0])?;
43        output.write_all(&record.target)?;
44        output.write_all(&[0])?;
45    }
46    Ok(())
47}