1
0
Fork 0

Adding upstream version 2.9.3.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-09 21:22:51 +01:00
parent e8d3ba475e
commit 5f54aad01b
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
32 changed files with 252 additions and 75 deletions

View file

@ -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
header = ''.join(lines[:i])
rest = ''.join(lines[i:])
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:])
# 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