Repository navigation
Expand file tree
/
Copy pathgrep.rs
More file actions
47 lines (45 loc) · 1.76 KB
/
Copy pathgrep.rs
File metadata and controls
47 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::env;
use git_rs::{GrepBinaryMode, GrepOptions, GrepTarget, HostFileSystem, Repository, Result};
fn main() -> Result<()> {
let mut arguments = env::args().skip(1);
let repository_path = arguments.next().ok_or_else(|| {
git_rs::Error::InvalidRevision(
"usage: grep <repository> <fixed-pattern> [--cached|--treeish=<rev>] [-i] [-v] [-w] [-a|-I] [path...]".into(),
)
})?;
let pattern = arguments
.next()
.ok_or_else(|| git_rs::Error::InvalidRevision("grep requires a fixed pattern".into()))?;
let mut options = GrepOptions::default();
for argument in arguments {
match argument.as_str() {
"--cached" => options.target = GrepTarget::Index,
"-i" => options.ignore_case = true,
"-v" => options.invert_match = true,
"-w" => options.word_regexp = true,
"-a" => options.binary = GrepBinaryMode::Text,
"-I" => options.binary = GrepBinaryMode::WithoutMatch,
_ if argument.starts_with("--treeish=") => {
options.target = GrepTarget::Treeish(argument[10..].to_owned());
}
_ => options.paths.push(argument.into_bytes()),
}
}
let repository = Repository::open(HostFileSystem::new(repository_path)?, ".")?;
for hit in repository.grep_fixed(&[pattern.into_bytes()], &options)? {
if hit.is_binary() {
println!(
"Binary file {} matches",
String::from_utf8_lossy(hit.path())
);
} else {
println!(
"{}:{}:{}",
String::from_utf8_lossy(hit.path()),
hit.line_number(),
String::from_utf8_lossy(hit.line())
);
}
}
Ok(())
}