add support for streaming stdin changes
This commit is contained in:
parent
31902a9df3
commit
22e3f70987
3 changed files with 66 additions and 23 deletions
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -2,6 +2,15 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "1.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstream"
|
name = "anstream"
|
||||||
version = "0.6.13"
|
version = "0.6.13"
|
||||||
|
@ -126,6 +135,7 @@ dependencies = [
|
||||||
name = "gerrit-filter-branch"
|
name = "gerrit-filter-branch"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
"clap",
|
"clap",
|
||||||
"git2",
|
"git2",
|
||||||
]
|
]
|
||||||
|
@ -222,6 +232,12 @@ version = "0.4.21"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.7.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.19.0"
|
version = "1.19.0"
|
||||||
|
|
|
@ -6,5 +6,6 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
aho-corasick = "1.1.3"
|
||||||
clap = { version = "4.5.4", features = ["derive"] }
|
clap = { version = "4.5.4", features = ["derive"] }
|
||||||
git2 = "0.18.3"
|
git2 = "0.18.3"
|
||||||
|
|
72
src/main.rs
72
src/main.rs
|
@ -1,5 +1,9 @@
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::{
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
io::{stdin, stdout},
|
||||||
|
};
|
||||||
|
|
||||||
|
use aho_corasick::AhoCorasick;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use git2::{Oid, Repository, Signature, Time};
|
use git2::{Oid, Repository, Signature, Time};
|
||||||
|
|
||||||
|
@ -25,6 +29,10 @@ struct Args {
|
||||||
#[arg(short, long)]
|
#[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.
|
/// 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,
|
cutoff: String,
|
||||||
|
|
||||||
|
#[arg(short, long)]
|
||||||
|
/// If the list of rewritten OIDs should be rewritten in the stdin stream, rather than the Git repo.
|
||||||
|
rewrite_stdin: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
|
@ -232,33 +240,51 @@ fn main() {
|
||||||
|
|
||||||
let refoid = state.repo.find_reference(&gref).unwrap().target().unwrap();
|
let refoid = state.repo.find_reference(&gref).unwrap().target().unwrap();
|
||||||
let newoid = state.rewrite(refoid);
|
let newoid = state.rewrite(refoid);
|
||||||
if refoid != newoid {
|
|
||||||
println!("{:?}: {:?} -> {:?}", gref, refoid, newoid);
|
if !state.args.rewrite_stdin {
|
||||||
state
|
if refoid != newoid {
|
||||||
.repo
|
println!("{:?}: {:?} -> {:?}", gref, refoid, newoid);
|
||||||
.reference(&gref, newoid, true, "filter-branch rewrite")
|
state
|
||||||
.unwrap();
|
.repo
|
||||||
} else {
|
.reference(&gref, newoid, true, "filter-branch rewrite")
|
||||||
println!("{:?}: {:?} (unchanged)", gref, refoid);
|
.unwrap();
|
||||||
|
} else {
|
||||||
|
println!("{:?}: {:?} (unchanged)", gref, refoid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for gref in &refs {
|
if state.args.rewrite_stdin {
|
||||||
if !gref.ends_with("/meta") {
|
let data: Vec<_> = state
|
||||||
continue;
|
.rewritten
|
||||||
}
|
.iter()
|
||||||
|
.map(|f| (f.0.to_string(), f.1.to_string()))
|
||||||
|
.collect();
|
||||||
|
|
||||||
let refoid = state.repo.find_reference(&gref).unwrap().target().unwrap();
|
let to_replace: Vec<&[u8]> = data.iter().map(|f| f.1.as_bytes()).collect();
|
||||||
let newoid = state.rewrite_meta(refoid);
|
let matcher = AhoCorasick::new(data.iter().map(|f| f.0.as_bytes())).unwrap();
|
||||||
|
|
||||||
if refoid != newoid {
|
matcher
|
||||||
println!("{:?}: {:?} -> {:?}", gref, refoid, newoid);
|
.try_stream_replace_all(stdin().lock(), stdout().lock(), &to_replace[..])
|
||||||
state
|
.unwrap();
|
||||||
.repo
|
} else {
|
||||||
.reference(&gref, newoid, true, "filter-branch rewrite")
|
for gref in &refs {
|
||||||
.unwrap();
|
if !gref.ends_with("/meta") {
|
||||||
} else {
|
continue;
|
||||||
println!("{:?}: {:?} (unchanged)", gref, refoid);
|
}
|
||||||
|
|
||||||
|
let refoid = state.repo.find_reference(&gref).unwrap().target().unwrap();
|
||||||
|
let newoid = state.rewrite_meta(refoid);
|
||||||
|
|
||||||
|
if refoid != newoid {
|
||||||
|
println!("{:?}: {:?} -> {:?}", gref, refoid, newoid);
|
||||||
|
state
|
||||||
|
.repo
|
||||||
|
.reference(&gref, newoid, true, "filter-branch rewrite")
|
||||||
|
.unwrap();
|
||||||
|
} else {
|
||||||
|
println!("{:?}: {:?} (unchanged)", gref, refoid);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue