use crate::test_support::{REAL_GIT, executable, isolated_overlay};
use crate::transaction_git::GitContext;
use std::path::{Path, PathBuf};
#[cfg(unix)]
pub(crate) fn recording_context(root: &Path) -> (GitContext, PathBuf) {
let log: PathBuf = root.join("git-starts.log");
let shim: PathBuf = root.join("recording-git");
let script: String = format!("#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{}'\nexec {REAL_GIT} \"$@\"\n", log.display());
executable(shim.as_path(), script.as_bytes());
let context: GitContext = GitContext {
real_git: shim,
overlay: isolated_overlay().into_iter().chain(crate::repo_read_hook::switch_off()).collect(),
global_prefix: Vec::new(),
common_dir: None,
};
return (context, log);
}
pub(crate) fn recorded_starts(log: &Path) -> Vec<String> {
let text: String = std::fs::read_to_string(log).unwrap_or_default();
let mut starts: Vec<String> = Vec::new();
for line in text.lines() {
starts.push(String::from(line));
}
return starts;
}
const RESULT_CHANGING_COMMANDS: [&str; 4] = ["rev-parse", "symbolic-ref", "diff-index", "diff-files"];
pub(crate) fn quiet_starts(log: &Path) -> Vec<String> {
let mut quiet: Vec<String> = Vec::new();
for start in recorded_starts(log) {
let words: Vec<&str> = start.split(' ').collect();
let carries: bool = words.iter().any(|word| { return *word == "--quiet" || *word == "-q"; });
let probe: bool = words.iter().any(|word| { return RESULT_CHANGING_COMMANDS.contains(word); });
if carries && !probe {
quiet.push(start);
}
}
return quiet;
}