.4144493732572704:a585ba8cf3f6cbc82531595b167d56c8_69e4e8fa053027e1e0449672.69e4e96c053027e1e04496af.69e4e96b5cc9bdc0a4ab5e31:Trae CN.T(2026/4/19 22:40:44) by Sonder528 · Pull Request #1 · Sonder528/pre-commit · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions pre_commit/clientlib.py
24 changes: 19 additions & 5 deletions pre_commit/commands/validate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
from collections.abc import Sequence

from pre_commit import clientlib
from pre_commit.errors import AggregateError


def validate_config(filenames: Sequence[str]) -> int:
ret = 0
errors: list[tuple[str, Exception]] = []

for filename in filenames:
try:
clientlib.load_config(filename)
except clientlib.InvalidConfigError as e:
print(e)
ret = 1
except (clientlib.InvalidConfigError, Exception) as e:
errors.append((filename, e))

return ret
if not errors:
return 0

if len(errors) == 1:
filename, exc = errors[0]
print(f'Error in config file {filename}:')
print(exc)
else:
agg = AggregateError.from_errors(
f'Found {len(errors)} config file error(s):',
errors,
)
print(agg)

return 1
24 changes: 19 additions & 5 deletions pre_commit/commands/validate_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
from collections.abc import Sequence

from pre_commit import clientlib
from pre_commit.errors import AggregateError


def validate_manifest(filenames: Sequence[str]) -> int:
ret = 0
errors: list[tuple[str, Exception]] = []

for filename in filenames:
try:
clientlib.load_manifest(filename)
except clientlib.InvalidManifestError as e:
print(e)
ret = 1
except (clientlib.InvalidManifestError, Exception) as e:
errors.append((filename, e))

return ret
if not errors:
return 0

if len(errors) == 1:
filename, exc = errors[0]
print(f'Error in manifest file {filename}:')
print(exc)
else:
agg = AggregateError.from_errors(
f'Found {len(errors)} manifest file error(s):',
errors,
)
print(agg)

return 1
70 changes: 70 additions & 0 deletions pre_commit/errors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
from __future__ import annotations

import os.path
from typing import Any


class FatalError(RuntimeError):
pass


class ConfigFileNotFoundError(FatalError):
def __init__(self, config_file: str) -> None:
self.config_file = config_file
abs_path = os.path.abspath(config_file)
super().__init__(
f'Config file not found: {abs_path}\n'
f'Hint: Make sure you are in the correct directory or use '
f'`--config` to specify an alternate path.\n'
f'To create a sample config file, run: `pre-commit sample-config`'
)


class ManifestFileNotFoundError(FatalError):
def __init__(self, manifest_file: str) -> None:
self.manifest_file = manifest_file
abs_path = os.path.abspath(manifest_file)
super().__init__(
f'Manifest file not found: {abs_path}'
)


class InvalidGitRepositoryError(FatalError):
def __init__(self, path: str) -> None:
self.path = path
abs_path = os.path.abspath(path)
super().__init__(
f'Not a git repository (or any of the parent directories): {abs_path}\n'
f'Hint: Run `git init` to initialize a git repository, or '
f'change to a directory that is already a git repository.'
)


class InsideGitDirectoryError(FatalError):
def __init__(self, git_dir: str) -> None:
self.git_dir = git_dir
super().__init__(
f'Cannot operate inside the .git directory: {git_dir}\n'
f'Hint: Change to the working tree directory instead of the .git directory.'
)


class AggregateError(FatalError):
def __init__(self, message: str, errors: list[Any]) -> None:
self.errors = errors
super().__init__(message)

@classmethod
def from_errors(
cls,
header: str,
errors: list[tuple[str, Exception]],
) -> 'AggregateError':
if len(errors) == 1:
filename, exc = errors[0]
return cls(f'Error in {filename}:\n{exc}', errors)

lines = [header, '']
for i, (filename, exc) in enumerate(errors, 1):
lines.append(f'{i}. {filename}:')
for line in str(exc).splitlines():
lines.append(f' {line}')
if i < len(errors):
lines.append('')

return cls('\n'.join(lines), errors)
12 changes: 4 additions & 8 deletions pre_commit/git.py
Loading