mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-04-19 09:02:15 +02:00
Fix leading slash bug when used with !
.
When writing paths like `!/foo` in gitignore files (or when using the -g/--glob flag), the presence of `!` would prevent the gitignore builder from noticing the leading slash, which causes absolute path matching to fail. Fixes #405
This commit is contained in:
parent
d570f78144
commit
80e91a1f1d
@ -368,9 +368,10 @@ impl GitignoreBuilder {
|
|||||||
};
|
};
|
||||||
let mut literal_separator = false;
|
let mut literal_separator = false;
|
||||||
let has_slash = line.chars().any(|c| c == '/');
|
let has_slash = line.chars().any(|c| c == '/');
|
||||||
let is_absolute = line.chars().nth(0).unwrap() == '/';
|
let mut is_absolute = false;
|
||||||
if line.starts_with("\\!") || line.starts_with("\\#") {
|
if line.starts_with("\\!") || line.starts_with("\\#") {
|
||||||
line = &line[1..];
|
line = &line[1..];
|
||||||
|
is_absolute = line.chars().nth(0) == Some('/');
|
||||||
} else {
|
} else {
|
||||||
if line.starts_with("!") {
|
if line.starts_with("!") {
|
||||||
glob.is_whitelist = true;
|
glob.is_whitelist = true;
|
||||||
@ -383,6 +384,7 @@ impl GitignoreBuilder {
|
|||||||
// simply banning wildcards from matching /.
|
// simply banning wildcards from matching /.
|
||||||
literal_separator = true;
|
literal_separator = true;
|
||||||
line = &line[1..];
|
line = &line[1..];
|
||||||
|
is_absolute = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If it ends with a slash, then this should only match directories,
|
// If it ends with a slash, then this should only match directories,
|
||||||
@ -570,6 +572,7 @@ mod tests {
|
|||||||
not_ignored!(
|
not_ignored!(
|
||||||
ignot14, "./third_party/protobuf", "m4/ltoptions.m4",
|
ignot14, "./third_party/protobuf", "m4/ltoptions.m4",
|
||||||
"./third_party/protobuf/csharp/src/packages/repositories.config");
|
"./third_party/protobuf/csharp/src/packages/repositories.config");
|
||||||
|
not_ignored!(ignot15, ROOT, "!/bar", "foo/bar");
|
||||||
|
|
||||||
fn bytes(s: &str) -> Vec<u8> {
|
fn bytes(s: &str) -> Vec<u8> {
|
||||||
s.to_string().into_bytes()
|
s.to_string().into_bytes()
|
||||||
|
@ -214,4 +214,10 @@ mod tests {
|
|||||||
assert!(ov.matched("src/foo", false).is_ignore());
|
assert!(ov.matched("src/foo", false).is_ignore());
|
||||||
assert!(ov.matched("src/foo", true).is_none());
|
assert!(ov.matched("src/foo", true).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn absolute_path() {
|
||||||
|
let ov = ov(&["!/bar"]);
|
||||||
|
assert!(ov.matched("./foo/bar", false).is_none());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1045,6 +1045,18 @@ clean!(regression_279, "test", ".", |wd: WorkDir, mut cmd: Command| {
|
|||||||
assert_eq!(lines, "");
|
assert_eq!(lines, "");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// See: https://github.com/BurntSushi/ripgrep/issues/405
|
||||||
|
clean!(regression_405, "test", ".", |wd: WorkDir, mut cmd: Command| {
|
||||||
|
wd.create_dir("foo/bar");
|
||||||
|
wd.create_dir("bar/foo");
|
||||||
|
wd.create("foo/bar/file1.txt", "test");
|
||||||
|
wd.create("bar/foo/file2.txt", "test");
|
||||||
|
cmd.arg("-g").arg("!/foo/**");
|
||||||
|
|
||||||
|
let lines: String = wd.stdout(&mut cmd);
|
||||||
|
assert_eq!(lines, "bar/foo/file2.txt:test\n");
|
||||||
|
});
|
||||||
|
|
||||||
// See: https://github.com/BurntSushi/ripgrep/issues/7
|
// See: https://github.com/BurntSushi/ripgrep/issues/7
|
||||||
sherlock!(feature_7, "-fpat", "sherlock", |wd: WorkDir, mut cmd: Command| {
|
sherlock!(feature_7, "-fpat", "sherlock", |wd: WorkDir, mut cmd: Command| {
|
||||||
wd.create("pat", "Sherlock\nHolmes");
|
wd.create("pat", "Sherlock\nHolmes");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user