Skip to content

Commit

Permalink
Attempt to fix [WinError 206]
Browse files Browse the repository at this point in the history
As per akaihola#490 when running darker on Windows with a large codebase
subprocess fails to run with a large list of files.

What I attempted to do in this commit is to only process changed files,
seeing as this is all we care about anyway. The filtering of the files
is moved earlier in the process and is passed the directory or paths set
on the commandline, instead of the list of files returned from the
`filter_python_files` call. The resulting set is then intersected with
all the files Black would process to generate a list of files that
should be processed.

While this alleviates the symptom, it could potentially still break if a
large amount of files are modified in between the current and previous
revisions.
  • Loading branch information
Svenito committed Feb 28, 2024
1 parent d90b545 commit 6f83a4d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
git_get_content_at_revision,
git_get_modified_python_files,
git_is_repository,
git_get_repository_root,
)
from darker.help import get_extra_instruction
from darker.highlighting import colorize, should_use_color
Expand Down Expand Up @@ -560,7 +561,22 @@ def main( # pylint: disable=too-many-locals,too-many-branches,too-many-statemen

# These paths are relative to `root`:
files_to_process = filter_python_files(paths, root, {})

changed_files_to_reformat = files_to_process
# Get a list of all files that have been modified since the revision
if git_is_repository(root):
# `git_get_modified_python_files` needs the repo root not just the common ancestor path
# if the paths argument is in the form of folder1/folder2 as the common ancestor will be `folder1`
# and not the repo root which will return an empty set in this case
root = git_get_repository_root(root)
changed_files_to_reformat = git_get_modified_python_files(
paths, revrange, root
)

# Only process files that have been changed and are in the filtered list
files_to_process = changed_files_to_reformat.intersection(files_to_process)
files_to_blacken = filter_python_files(paths, root, black_config)

# Now decide which files to reformat (Black & isort). Note that this doesn't apply
# to linting.
if output_mode == OutputMode.CONTENT or revrange.rev2 == STDIN:
Expand All @@ -570,13 +586,6 @@ def main( # pylint: disable=too-many-locals,too-many-branches,too-many-statemen
changed_files_to_reformat = files_to_process
black_exclude = set()
else:
# In other modes, only reformat files which have been modified.
if git_is_repository(root):
changed_files_to_reformat = git_get_modified_python_files(
files_to_process, revrange, root
)
else:
changed_files_to_reformat = files_to_process
black_exclude = {
str(path)
for path in changed_files_to_reformat
Expand Down
9 changes: 9 additions & 0 deletions src/darker/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,15 @@ def git_get_modified_python_files(
return {path for path in changed_paths if should_reformat_file(cwd / path)}


def git_get_repository_root(cwd: Path) -> Path:
git_root_cmd = [
"rev-parse",
"--show-toplevel"
]
lines =_git_check_output_lines(git_root_cmd, cwd)
return Path(lines[0])


@contextmanager
def git_clone_local(
source_repository: Path, revision: str, destination: Path
Expand Down

0 comments on commit 6f83a4d

Please sign in to comment.