diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-27 16:04:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2021-01-27 16:04:26 +0000 |
commit | 7ad3acc8faa151e307c82b659f884205ef47012b (patch) | |
tree | da74df356ea350b3ee9fb0bf71937e0bdb7896a6 /pre_commit/commands/migrate_config.py | |
parent | Releasing debian version 2.8.2-1. (diff) | |
download | pre-commit-7ad3acc8faa151e307c82b659f884205ef47012b.tar.xz pre-commit-7ad3acc8faa151e307c82b659f884205ef47012b.zip |
Merging upstream version 2.9.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pre_commit/commands/migrate_config.py')
-rw-r--r-- | pre_commit/commands/migrate_config.py | 26 |
1 files changed, 11 insertions, 15 deletions
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 |