summaryrefslogtreecommitdiffstats
path: root/pre_commit/commands
diff options
context:
space:
mode:
Diffstat (limited to 'pre_commit/commands')
-rw-r--r--pre_commit/commands/autoupdate.py8
-rw-r--r--pre_commit/commands/migrate_config.py26
-rw-r--r--pre_commit/commands/run.py22
3 files changed, 32 insertions, 24 deletions
diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py
index 87f6d53..33a3473 100644
--- a/pre_commit/commands/autoupdate.py
+++ b/pre_commit/commands/autoupdate.py
@@ -79,14 +79,12 @@ def _check_hooks_still_exist_at_rev(
hooks_missing = hooks - {hook['id'] for hook in manifest}
if hooks_missing:
raise RepositoryCannotBeUpdatedError(
- f'Cannot update because the tip of HEAD is missing these hooks:\n'
- f'{", ".join(sorted(hooks_missing))}',
+ f'Cannot update because the update target is missing these '
+ f'hooks:\n{", ".join(sorted(hooks_missing))}',
)
-REV_LINE_RE = re.compile(
- r'^(\s+)rev:(\s*)([\'"]?)([^\s#]+)(.*)(\r?\n)$', re.DOTALL,
-)
+REV_LINE_RE = re.compile(r'^(\s+)rev:(\s*)([\'"]?)([^\s#]+)(.*)(\r?\n)$')
def _original_lines(
diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py
index d580ff1..a155f6b 100644
--- a/pre_commit/commands/migrate_config.py
+++ b/pre_commit/commands/migrate_config.py
@@ -1,4 +1,5 @@
import re
+import textwrap
import yaml
@@ -6,27 +7,22 @@ from pre_commit.clientlib import load_config
from pre_commit.util import yaml_load
-def _indent(s: str) -> str:
- lines = s.splitlines(True)
- return ''.join(' ' * 4 + line if line.strip() else line for line in lines)
-
-
def _is_header_line(line: str) -> bool:
return line.startswith(('#', '---')) or not line.strip()
def _migrate_map(contents: str) -> str:
- # Find the first non-header line
- lines = contents.splitlines(True)
- i = 0
- # Only loop on non empty configuration file
- while i < len(lines) and _is_header_line(lines[i]):
- i += 1
+ if isinstance(yaml_load(contents), list):
+ # Find the first non-header line
+ lines = contents.splitlines(True)
+ i = 0
+ # Only loop on non empty configuration file
+ while i < len(lines) and _is_header_line(lines[i]):
+ i += 1
- header = ''.join(lines[:i])
- rest = ''.join(lines[i:])
+ header = ''.join(lines[:i])
+ rest = ''.join(lines[i:])
- if isinstance(yaml_load(contents), list):
# If they are using the "default" flow style of yaml, this operation
# will yield a valid configuration
try:
@@ -34,7 +30,7 @@ def _migrate_map(contents: str) -> str:
yaml_load(trial_contents)
contents = trial_contents
except yaml.YAMLError:
- contents = f'{header}repos:\n{_indent(rest)}'
+ contents = f'{header}repos:\n{textwrap.indent(rest, " " * 4)}'
return contents
diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py
index 0d335e2..1e8fad2 100644
--- a/pre_commit/commands/run.py
+++ b/pre_commit/commands/run.py
@@ -83,20 +83,32 @@ class Classifier:
self,
names: Sequence[str],
types: Collection[str],
+ types_or: Collection[str],
exclude_types: Collection[str],
) -> List[str]:
- types, exclude_types = frozenset(types), frozenset(exclude_types)
+ types = frozenset(types)
+ types_or = frozenset(types_or)
+ exclude_types = frozenset(exclude_types)
ret = []
for filename in names:
tags = self._types_for_file(filename)
- if tags >= types and not tags & exclude_types:
+ if (
+ tags >= types and
+ (not types_or or tags & types_or) and
+ not tags & exclude_types
+ ):
ret.append(filename)
return ret
def filenames_for_hook(self, hook: Hook) -> Tuple[str, ...]:
names = self.filenames
names = filter_by_include_exclude(names, hook.files, hook.exclude)
- names = self.by_types(names, hook.types, hook.exclude_types)
+ names = self.by_types(
+ names,
+ hook.types,
+ hook.types_or,
+ hook.exclude_types,
+ )
return tuple(names)
@classmethod
@@ -250,7 +262,9 @@ def _all_filenames(args: argparse.Namespace) -> Collection[str]:
def _get_diff() -> bytes:
- _, out, _ = cmd_output_b('git', 'diff', '--no-ext-diff', retcode=None)
+ _, out, _ = cmd_output_b(
+ 'git', 'diff', '--no-ext-diff', '--ignore-submodules', retcode=None,
+ )
return out