use std::{process, process::Command, str};
const COMMAND_ARGUMENTS: &'static [&'static str] = &[
"log",
"--pretty=tformat:",
"--numstat",
"--ignore-space-change",
"--ignore-all-space",
"--ignore-submodules",
"--no-color",
"--diff-filter=ACDM",
];
pub fn run(find_renames_and_copies: bool) {
let mut extra_command_arguments: &'static [&'static str] = &[];
if find_renames_and_copies {
extra_command_arguments = &["--find-renames", "--find-copies-harder"];
}
let output = Command::new("git")
.args([COMMAND_ARGUMENTS, extra_command_arguments].concat())
.output()
.expect("git command failed");
if output.status.success() {
let mut total: usize = 0;
let mut num: Vec<u8> = vec![];
let mut in_word = false;
for c in output.stdout.iter() {
match c {
b'\t' => {
if !num.is_empty() {
total += integer_from_slice(num.as_slice()) as usize;
num.clear();
}
}
b'\n' => in_word = false,
b'0'...b'9' => {
if !in_word {
num.push(*c);
}
}
_ => in_word = true,
}
}
println!("{}", total);
} else {
if let Some(code) = output.status.code() {
eprintln!("git command exited with status {}", code)
} else {
eprintln!("git command killed with a signal")
}
process::exit(1);
}
}
fn integer_from_slice(slice: &[u8]) -> usize {
let n: usize = str::from_utf8(slice)
.expect("not a string")
.parse()
.expect("not a number");
n
}