Skip to content

Commit

Permalink
Obtain parent git grep command line options
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Nov 22, 2021
1 parent 44dbc08 commit ce6258d
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/utils/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub fn git_blame_filename_extension() -> Option<String> {
calling_process_cmdline(ProcInfo::new(), blame::guess_git_blame_filename_extension)
}

pub fn git_grep_command_options() -> Option<(HashSet<String>, HashSet<String>)> {
calling_process_cmdline(ProcInfo::new(), grep::get_grep_options)
}

mod blame {
use super::*;

Expand Down Expand Up @@ -75,6 +79,34 @@ mod blame {
}
} // mod blame

mod grep {
use super::*;

// Given `... grep --aa val -bc -d val e f -- ...` return
// ({"--aa"}, {"-b", "-c", "-d"})
pub fn get_grep_options(args: &[String]) -> ProcessArgs<(HashSet<String>, HashSet<String>)> {
let mut args = args.iter().map(|s| s.as_str()).skip_while(|s| *s != "grep");
match args.next() {
None => ProcessArgs::OtherProcess,
_ => {
let mut longs = HashSet::new();
let mut shorts = HashSet::new();

for s in args {
if s == "--" {
break;
} else if s.starts_with("--") {
longs.insert(s.split('=').next().unwrap().to_owned());
} else if let Some(suffix) = s.strip_prefix('-') {
shorts.extend(suffix.chars().map(|c| format!("-{}", c)));
}
}
ProcessArgs::Args((longs, shorts))
}
}
}
}

struct ProcInfo {
info: sysinfo::System,
}
Expand Down Expand Up @@ -555,6 +587,58 @@ pub mod tests {
);
}

#[test]
fn test_get_grep_options() {
let no_processes = MockProcInfo::with(&[]);
assert_eq!(
calling_process_cmdline(no_processes, grep::get_grep_options),
None
);

let parent = MockProcInfo::with(&[
(2, 100, "-shell", None),
(3, 100, "git grep pattern hello.txt", Some(2)),
(4, 100, "delta", Some(3)),
]);
assert_eq!(
calling_process_cmdline(parent, grep::get_grep_options),
Some(([].into(), [].into()))
);

fn set(arg1: &[&str]) -> HashSet<String> {
arg1.iter().map(|&s| s.to_owned()).collect()
}

let git_grep_command =
"git grep -ab --function-context -n --show-function -W --foo=val pattern hello.txt";

let expected_result = Some((
set(&["--function-context", "--show-function", "--foo"]),
set(&["-a", "-b", "-n", "-W"]),
));

let parent = MockProcInfo::with(&[
(2, 100, "-shell", None),
(3, 100, git_grep_command, Some(2)),
(4, 100, "delta", Some(3)),
]);
assert_eq!(
calling_process_cmdline(parent, grep::get_grep_options),
expected_result
);

let grandparent = MockProcInfo::with(&[
(2, 100, "-shell", None),
(3, 100, git_grep_command, Some(2)),
(4, 100, "call_delta.sh", Some(3)),
(5, 100, "delta", Some(4)),
]);
assert_eq!(
calling_process_cmdline(grandparent, grep::get_grep_options),
expected_result
);
}

#[test]
fn test_process_calling_cmdline() {
// Github runs CI tests for arm under qemu where where sysinfo can not find the parent processr.
Expand Down

0 comments on commit ce6258d

Please sign in to comment.