Add cutoff time

This commit is contained in:
puck 2024-05-02 18:26:20 +00:00
parent 5c96beea07
commit 9fda734c7b

View file

@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use clap::Parser; use clap::Parser;
use git2::{Oid, Repository, Signature}; use git2::{Oid, Repository, Signature, Time};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@ -21,12 +21,17 @@ struct Args {
#[arg(short, long)] #[arg(short, long)]
/// The author/committer email to replace the old one with. /// The author/committer email to replace the old one with.
new_email: String, new_email: String,
#[arg(short, long)]
/// A timestamp (in seconds since the epoch) at which point commits should be considered unchanged, even if it or its parents have aforementioned email.
cutoff: String,
} }
struct State { struct State {
repo: git2::Repository, repo: git2::Repository,
rewritten: HashMap<Oid, Oid>, rewritten: HashMap<Oid, Oid>,
unrewritten: HashSet<Oid>, unrewritten: HashSet<Oid>,
cutoff_time: Time,
args: Args, args: Args,
} }
@ -42,6 +47,11 @@ impl State {
} }
let mut commit = self.repo.find_commit(oid).unwrap(); let mut commit = self.repo.find_commit(oid).unwrap();
if commit.committer().when() < self.cutoff_time {
self.unrewritten.insert(oid);
return oid;
}
// If the email address matches, we will have to rewrite this commit either way, even if parent IDs match. // If the email address matches, we will have to rewrite this commit either way, even if parent IDs match.
let rewrite_author = commit.author().email().unwrap() == &self.args.rewrite_email; let rewrite_author = commit.author().email().unwrap() == &self.args.rewrite_email;
@ -195,11 +205,13 @@ impl State {
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
let repo = args.repo.clone(); let repo = args.repo.clone();
let cutoff = Time::new(i64::from_str_radix(&args.cutoff, 10).unwrap(), 0);
let mut state = State { let mut state = State {
repo: Repository::open_bare(repo).unwrap(), repo: Repository::open_bare(repo).unwrap(),
rewritten: HashMap::new(), rewritten: HashMap::new(),
unrewritten: HashSet::new(), unrewritten: HashSet::new(),
cutoff_time: cutoff,
args, args,
}; };