]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Improve performance by skipping unnecessary normalisation (#3751)
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>
Sun, 9 Jul 2023 22:24:01 +0000 (15:24 -0700)
committerGitHub <noreply@github.com>
Sun, 9 Jul 2023 22:24:01 +0000 (15:24 -0700)
This speeds up black by about 40% when the cache is full

CHANGES.md
src/black/files.py
tests/test_black.py

index 15027afbf0b24f3f33f96460d35f08bbcf2c7de2..93d8ee1921ac3e8c79a3c4e9cf8fc8c8bd1e8ab1 100644 (file)
@@ -55,6 +55,7 @@
 
 <!-- Changes that improve Black's performance. -->
 
+- Speed up _Black_ significantly when the cache is full (#3751)
 - Avoid importing `IPython` in a case where we wouldn't need it (#3748)
 
 ### Output
index 4e2209e557d2cc9c26d60db5d471c79e80a8f7da..ef6895ee3af5341c6e32b9ff0521426dd9107c59 100644 (file)
@@ -276,15 +276,24 @@ def normalize_path_maybe_ignore(
     return root_relative_path
 
 
-def path_is_ignored(
-    path: Path, gitignore_dict: Dict[Path, PathSpec], report: Report
+def _path_is_ignored(
+    root_relative_path: str,
+    root: Path,
+    gitignore_dict: Dict[Path, PathSpec],
+    report: Report,
 ) -> bool:
+    path = root / root_relative_path
+    # Note that this logic is sensitive to the ordering of gitignore_dict. Callers must
+    # ensure that gitignore_dict is ordered from least specific to most specific.
     for gitignore_path, pattern in gitignore_dict.items():
-        relative_path = normalize_path_maybe_ignore(path, gitignore_path, report)
-        if relative_path is None:
+        try:
+            relative_path = path.relative_to(gitignore_path).as_posix()
+        except ValueError:
             break
         if pattern.match_file(relative_path):
-            report.path_ignored(path, "matches a .gitignore file content")
+            report.path_ignored(
+                path.relative_to(root), "matches a .gitignore file content"
+            )
             return True
     return False
 
@@ -326,7 +335,9 @@ def gen_python_files(
             continue
 
         # First ignore files matching .gitignore, if passed
-        if gitignore_dict and path_is_ignored(child, gitignore_dict, report):
+        if gitignore_dict and _path_is_ignored(
+            normalized_path, root, gitignore_dict, report
+        ):
             continue
 
         # Then ignore with `--exclude` `--extend-exclude` and `--force-exclude` options.
index dd21d0a7ae6a00d7614e3c4a995ce96d9f7c44c0..3b3ab721c5f21d1671057e07fa7c0a1f43de18ac 100644 (file)
@@ -508,6 +508,8 @@ class BlackTestCase(BlackBaseTestCase):
             "pathlib.Path.cwd", return_value=working_directory
         ), patch("pathlib.Path.is_dir", side_effect=mock_n_calls([True])):
             ctx = FakeContext()
+            # Note that the root folder (project_root) isn't the folder
+            # named "root" (aka working_directory)
             ctx.obj["root"] = project_root
             report = MagicMock(verbose=True)
             black.get_sources(
@@ -527,7 +529,7 @@ class BlackTestCase(BlackBaseTestCase):
             for _, mock_args, _ in report.path_ignored.mock_calls
         ), "A symbolic link was reported."
         report.path_ignored.assert_called_once_with(
-            Path("child", "b.py"), "matches a .gitignore file content"
+            Path("root", "child", "b.py"), "matches a .gitignore file content"
         )
 
     def test_report_verbose(self) -> None: