From 1196090e7c2fb85424da94c7643dee370ec9ae2a Mon Sep 17 00:00:00 2001 From: Mayank Dhingra Date: Sun, 1 Nov 2026 16:46:53 -0500 Subject: [PATCH] Normalize redundant interior path components before matching Match equivalent relative paths consistently while preserving parent components, directory markers and original input values in results. Add public matching API regressions and update the source changelog. Fixes #176. Signed-off-by: Mayank Dhingra --- CHANGES_1.in.rst | 2 + pathspec/util.py | 10 +++- tests/test_10_path_normalization.py | 87 +++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 tests/test_10_path_normalization.py diff --git a/CHANGES_1.in.rst b/CHANGES_1.in.rst index 9da2b23..0a44867 100644 --- a/CHANGES_1.in.rst +++ b/CHANGES_1.in.rst @@ -23,6 +23,7 @@ Bug fixes: - Match escaped internal path separators in gitignore patterns without treating them as trailing backslashes. - Hyperscan backend no longer fails on multibyte UTF-8 characters. +- `Issue #176`_: Normalize redundant interior separators and current-directory components in matching paths, preserving original paths in matching results. - `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`. - `Pull #128`_: Support POSIX character classes (e.g. `[[:alpha:]]`) in gitignore bracket expressions. - `Issue #129`_ / `Pull #132`_: Fix GitIgnoreSpec re-including files under an excluded directory @@ -83,6 +84,7 @@ Bug fixes: .. _`Pull #165`: https://github.com/cpburnz/python-pathspec/pull/165 .. _`Pull #166`: https://github.com/cpburnz/python-pathspec/pull/166 .. _`Pull #167`: https://github.com/cpburnz/python-pathspec/pull/167 +.. _`Issue #176`: https://github.com/cpburnz/python-pathspec/issues/176 1.1.1 (2026-04-26) diff --git a/pathspec/util.py b/pathspec/util.py index bd2ae6d..0f2f1da 100644 --- a/pathspec/util.py +++ b/pathspec/util.py @@ -7,6 +7,7 @@ import os.path import pathlib import posixpath +import re import stat import warnings from collections.abc import ( @@ -62,6 +63,8 @@ :data:`os.altsep`. """ +_NORMALIZE_INTERIOR_PATH = re.compile(r'/(?:/|\./)+') + _registered_patterns: dict[str, Callable[[Union[str, bytes]], Pattern]] = {} """ *_registered_patterns* (:class:`dict`) maps a name (:class:`str`) to the @@ -585,6 +588,8 @@ def normalize_file( """ Normalizes the file path to use the POSIX path separator (i.e., ``"/"``), and make the paths relative (remove leading ``"/"``). + Redundant interior separators and current-directory components are removed. + Parent-directory components and a trailing separator are preserved. *file* (:class:`str` or :class:`os.PathLike`) is the file path. @@ -592,7 +597,7 @@ def normalize_file( :data:`None`) optionally contains the path separators to normalize. This does not need to include the POSIX path separator (``"/"``), but including it will not affect the results. Default is ``None`` for :data:`.NORMALIZE_PATH_SEPS`. - To prevent normalization, pass an empty container (e.g., an empty tuple + To prevent separator replacement, pass an empty container (e.g., an empty tuple ``()``). Returns the normalized file path (:class:`str`). @@ -617,6 +622,9 @@ def normalize_file( # Remove current directory prefix. norm_file = norm_file[2:] + if '//' in norm_file or '/./' in norm_file: + norm_file = _NORMALIZE_INTERIOR_PATH.sub('/', norm_file) + return norm_file diff --git a/tests/test_10_path_normalization.py b/tests/test_10_path_normalization.py new file mode 100644 index 0000000..cb78ebd --- /dev/null +++ b/tests/test_10_path_normalization.py @@ -0,0 +1,87 @@ +"""Regression coverage for redundant interior components in matching paths.""" + +from unittest import TestCase + +from pathspec import GitIgnoreSpec, PathSpec +from pathspec.util import normalize_file + +from .util import require_backend + + +class InteriorPathNormalizationTest(TestCase): + + def test_normalize_components(self): + cases = { + 'data//private.txt': 'data/private.txt', + 'data/./private.txt': 'data/private.txt', + 'data///././/private.txt': 'data/private.txt', + 'data//child/./private.txt': 'data/child/private.txt', + 'data//./': 'data/', + 'data/././': 'data/', + 'data/private.txt': 'data/private.txt', + 'data/.hidden': 'data/.hidden', + 'data/..hidden/file': 'data/..hidden/file', + 'data/../private.txt': 'data/../private.txt', + 'data/.': 'data/.', + 'data/': 'data/', + '': '', + } + for path, expected in cases.items(): + with self.subTest(path=path): + self.assertEqual(normalize_file(path, separators=()), expected) + + def test_public_matching_apis_preserve_input_spelling(self): + paths = [ + 'data/private.txt', + 'data//private.txt', + 'data/./private.txt', + 'data///././private.txt', + 'data/keep.txt', + 'data//./keep.txt', + 'data/public.txt', + 'data/./public.txt', + ] + expected = [(True, 0)] * 4 + [(False, 2)] * 2 + [(None, None)] * 2 + lines = ['/data/private.txt', '/data/keep.txt', '!/data/keep.txt'] + for cls in (PathSpec, GitIgnoreSpec): + for backend in ('simple', 're2', 'hyperscan'): + with self.subTest(cls=cls.__name__, backend=backend): + require_backend(backend) + if cls is PathSpec: + spec = cls.from_lines('gitignore', lines, backend=backend) + else: + spec = cls.from_lines(lines, backend=backend) + self.assertEqual(list(spec.match_files(paths)), paths[:4]) + self.assertEqual(list(spec.match_files(paths, negate=True)), paths[4:]) + results = list(spec.check_files(paths)) + self.assertEqual([result.file for result in results], paths) + self.assertEqual( + [(result.include, result.index) for result in results], expected) + for path, (include, index) in zip(paths, expected): + self.assertEqual(spec.match_file(path), bool(include)) + result = spec.check_file(path) + self.assertEqual((result.file, result.include, result.index), (path, include, index)) + + def test_custom_separator_and_literal_backslash(self): + path = r'data\\.\private.txt' + self.assertEqual(normalize_file(path, separators=('\\',)), 'data/private.txt') + self.assertEqual(normalize_file(path, separators=()), path) + for cls in (PathSpec, GitIgnoreSpec): + with self.subTest(cls=cls.__name__): + if cls is PathSpec: + spec = cls.from_lines('gitignore', ['/data/private.txt'], backend='simple') + else: + spec = cls.from_lines(['/data/private.txt'], backend='simple') + self.assertTrue(spec.match_file(path, separators=('\\',))) + self.assertFalse(spec.match_file(path, separators=())) + + def test_directory_markers_and_parent_components(self): + for cls in (PathSpec, GitIgnoreSpec): + with self.subTest(cls=cls.__name__): + if cls is PathSpec: + spec = cls.from_lines('gitignore', ['/data/'], backend='simple') + else: + spec = cls.from_lines(['/data/'], backend='simple') + self.assertTrue(spec.match_file('data//./')) + self.assertFalse(spec.match_file('data')) + self.assertEqual(normalize_file('data//../file.txt'), 'data/../file.txt')