Skip to content

Commit 80e91a1

Browse files
committed
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
1 parent d570f78 commit 80e91a1

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

ignore/src/gitignore.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,10 @@ impl GitignoreBuilder {
368368
};
369369
let mut literal_separator = false;
370370
let has_slash = line.chars().any(|c| c == '/');
371-
let is_absolute = line.chars().nth(0).unwrap() == '/';
371+
let mut is_absolute = false;
372372
if line.starts_with("\\!") || line.starts_with("\\#") {
373373
line = &line[1..];
374+
is_absolute = line.chars().nth(0) == Some('/');
374375
} else {
375376
if line.starts_with("!") {
376377
glob.is_whitelist = true;
@@ -383,6 +384,7 @@ impl GitignoreBuilder {
383384
// simply banning wildcards from matching /.
384385
literal_separator = true;
385386
line = &line[1..];
387+
is_absolute = true;
386388
}
387389
}
388390
// If it ends with a slash, then this should only match directories,
@@ -570,6 +572,7 @@ mod tests {
570572
not_ignored!(
571573
ignot14, "./third_party/protobuf", "m4/ltoptions.m4",
572574
"./third_party/protobuf/csharp/src/packages/repositories.config");
575+
not_ignored!(ignot15, ROOT, "!/bar", "foo/bar");
573576

574577
fn bytes(s: &str) -> Vec<u8> {
575578
s.to_string().into_bytes()

ignore/src/overrides.rs

+6
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,10 @@ mod tests {
214214
assert!(ov.matched("src/foo", false).is_ignore());
215215
assert!(ov.matched("src/foo", true).is_none());
216216
}
217+
218+
#[test]
219+
fn absolute_path() {
220+
let ov = ov(&["!/bar"]);
221+
assert!(ov.matched("./foo/bar", false).is_none());
222+
}
217223
}

tests/tests.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,18 @@ clean!(regression_279, "test", ".", |wd: WorkDir, mut cmd: Command| {
10451045
assert_eq!(lines, "");
10461046
});
10471047

1048+
// See: https://github.com/BurntSushi/ripgrep/issues/405
1049+
clean!(regression_405, "test", ".", |wd: WorkDir, mut cmd: Command| {
1050+
wd.create_dir("foo/bar");
1051+
wd.create_dir("bar/foo");
1052+
wd.create("foo/bar/file1.txt", "test");
1053+
wd.create("bar/foo/file2.txt", "test");
1054+
cmd.arg("-g").arg("!/foo/**");
1055+
1056+
let lines: String = wd.stdout(&mut cmd);
1057+
assert_eq!(lines, "bar/foo/file2.txt:test\n");
1058+
});
1059+
10481060
// See: https://github.com/BurntSushi/ripgrep/issues/7
10491061
sherlock!(feature_7, "-fpat", "sherlock", |wd: WorkDir, mut cmd: Command| {
10501062
wd.create("pat", "Sherlock\nHolmes");

0 commit comments

Comments
 (0)