Skip to content

Commit d7ff656

Browse files
committed
fix: assure that .. patterns don't end up being . that matches nothing
...if the current-dir is one level in. Now such `.` is special and means to match everything.
1 parent 3438bc9 commit d7ff656

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

gix-pathspec/src/pattern.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl Pattern {
110110
};
111111

112112
self.path = if path == Path::new(".") {
113+
self.nil = true;
113114
BString::from(".")
114115
} else {
115116
let cleaned = PathBuf::from_iter(path.components().filter(|c| !matches!(c, Component::CurDir)));

gix-pathspec/tests/normalize/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
use std::path::Path;
22

3+
#[test]
4+
fn consuming_the_entire_prefix_does_not_lead_to_a_single_dot() -> crate::Result {
5+
let spec = normalized_spec("..", "a", "")?;
6+
assert_eq!(
7+
spec.path(),
8+
".",
9+
"the top-level of the worktree can take the special value '.' to mean 'everything'"
10+
);
11+
assert!(
12+
spec.is_nil(),
13+
"single is for the worktree top-level, and since it wouldn't match anything we make it nil so it does"
14+
);
15+
assert_eq!(spec.prefix_directory(), "", "there is no prefix left");
16+
Ok(())
17+
}
18+
319
#[test]
420
fn removes_relative_path_components() -> crate::Result {
521
for (input_path, expected_path, expected_prefix) in [
22+
("..", "a", ""),
623
("c", "a/b/c", "a/b"),
724
("../c", "a/c", "a"),
825
("../b/c", "a/b/c", "a"), // this is a feature - prefix components once consumed by .. are lost. Important as paths can contain globs
@@ -33,6 +50,7 @@ fn single_dot_is_special_and_directory_is_implied_without_trailing_slash() -> cr
3350
for (input_path, expected) in [(".", "."), ("./", ".")] {
3451
let spec = normalized_spec(input_path, "", "/repo")?;
3552
assert_eq!(spec.path(), expected);
53+
assert!(spec.is_nil(), "such a spec has to match everything");
3654
assert_eq!(spec.prefix_directory(), "");
3755
}
3856
Ok(())

0 commit comments

Comments
 (0)