prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  ParserError exception raised for invalid configuration
  ### search you tried in the issue tracker
  
  ParserError and unicode
  
  ### describe your issue
  
  I executed `pre-commit autoupdate` with an invalid configuration file (the second `- repo` is indented incorrectly) and got this error message:
  
  ````
  $ pre-commit autoupdate
  An unexpected error has occurred: ParserError: while parsing a block mapping
    in "<unicode string>", line 1, column 1
  did not find expected key
    in "<unicode string>", line 7, column 1
  Check the log at /home/carsten/.cache/pre-commit/pre-commit.log
  ````
  
  This is an expected error and I would expect an error message like `Your configuration file "..." is wrongly formatted at <pos>. Please review the format of the content.'.
  
  Thank you,
  Carsten
  
  ### pre-commit --version
  
  pre-commit 2.21.0
  
  ### .pre-commit-config.yaml
  
  ```yaml
  repos:
    - repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v4.4.0
      hooks:
        - id: check-executables-have-shebangs
  
  - repo: https://github.com/shellcheck-py/shellcheck-py
      rev: v0.9.0.2
      hooks:
      -   id: shellcheck
  ```
  
  
  ### ~/.cache/pre-commit/pre-commit.log (if present)
  
  ### version information
  
  ```
  pre-commit version: 2.21.0
  git --version: git version 2.35.3
  sys.version:
      3.10.8 (main, Oct 28 2022, 17:28:32) [GCC]
  sys.executable: /home/carsten/virtualenv/bin/python3.10
  os.name: posix
  sys.platform: linux
  ```
  
  ### error information
  
  ```
  An unexpected error has occurred: ParserError: while parsing a block mapping
    in "<unicode string>", line 1, column 1
  did not find expected key
    in "<unicode string>", line 7, column 1
  ```
  
  ```
  Traceback (most recent call last):
    File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/error_handler.py", line 73, in error_handler
      yield
    File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/main.py", line 355, in main
      return autoupdate(
    File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/autoupdate.py", line 154, in autoupdate
      migrate_config(config_file, quiet=True)
    File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/migrate_config.py", line 47, in migrate_config
      contents = _migrate_map(contents)
    File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/migrate_config.py", line 16, in _migrate_map
      if isinstance(yaml_load(contents), list):
    File "/home/carsten/virtualenv/lib64/python3.10/site-packages/yaml/__init__.py", line 81, in load
      return loader.get_single_data()
    File "/home/carsten/virtualenv/lib64/python3.10/site-packages/yaml/constructor.py", line 49, in get_single_data
      node = self.get_single_node()
    File "yaml/_yaml.pyx", line 673, in yaml._yaml.CParser.get_single_node
    File "yaml/_yaml.pyx", line 687, in yaml._yaml.CParser._compose_document
    File "yaml/_yaml.pyx", line 731, in yaml._yaml.CParser._compose_node
    File "yaml/_yaml.pyx", line 847, in yaml._yaml.CParser._compose_mapping_node
    File "yaml/_yaml.pyx", line 860, in yaml._yaml.CParser._parse_next_event
  yaml.parser.ParserError: while parsing a block mapping
    in "<unicode string>", line 1, column 1
  did not find expected key
    in "<unicode string>", line 7, column 1
  ```
  
  </issue>
  <code>
  [start of pre_commit/commands/migrate_config.py]
  1 from __future__ import annotations
  2 
  3 import re
  4 import textwrap
  5 
  6 import yaml
  7 
  8 from pre_commit.yaml import yaml_load
  9 
  10 
  11 def _is_header_line(line: str) -> bool:
  12     return line.startswith(('#', '---')) or not line.strip()
  13 
  14 
  15 def _migrate_map(contents: str) -> str:
  16     if isinstance(yaml_load(contents), list):
  17         # Find the first non-header line
  18         lines = contents.splitlines(True)
  19         i = 0
  20         # Only loop on non empty configuration file
  21         while i < len(lines) and _is_header_line(lines[i]):
  22             i += 1
  23 
  24         header = ''.join(lines[:i])
  25         rest = ''.join(lines[i:])
  26 
  27         # If they are using the "default" flow style of yaml, this operation
  28         # will yield a valid configuration
  29         try:
  30             trial_contents = f'{header}repos:\n{rest}'
  31             yaml_load(trial_contents)
  32             contents = trial_contents
  33         except yaml.YAMLError:
  34             contents = f'{header}repos:\n{textwrap.indent(rest, " " * 4)}'
  35 
  36     return contents
  37 
  38 
  39 def _migrate_sha_to_rev(contents: str) -> str:
  40     return re.sub(r'(\n\s+)sha:', r'\1rev:', contents)
  41 
  42 
  43 def migrate_config(config_file: str, quiet: bool = False) -> int:
  44     with open(config_file) as f:
  45         orig_contents = contents = f.read()
  46 
  47     contents = _migrate_map(contents)
  48     contents = _migrate_sha_to_rev(contents)
  49 
  50     if contents != orig_contents:
  51         with open(config_file, 'w') as f:
  52             f.write(contents)
  53 
  54         print('Configuration has been migrated.')
  55     elif not quiet:
  56         print('Configuration is already migrated.')
  57     return 0
  58
  [end of pre_commit/commands/migrate_config.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py
  --- a/pre_commit/commands/migrate_config.py
  +++ b/pre_commit/commands/migrate_config.py
  @@ -3,8 +3,10 @@
   import re
   import textwrap
   
  +import cfgv
   import yaml
   
  +from pre_commit.clientlib import InvalidConfigError
   from pre_commit.yaml import yaml_load
   
   
  @@ -44,6 +46,13 @@
       with open(config_file) as f:
           orig_contents = contents = f.read()
   
  +    with cfgv.reraise_as(InvalidConfigError):
  +        with cfgv.validate_context(f'File {config_file}'):
  +            try:
  +                yaml_load(orig_contents)
  +            except Exception as e:
  +                raise cfgv.ValidationError(str(e))
  +
       contents = _migrate_map(contents)
       contents = _migrate_sha_to_rev(contents)
  
verification_info: {"golden_diff": "diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py\n--- a/pre_commit/commands/migrate_config.py\n+++ b/pre_commit/commands/migrate_config.py\n@@ -3,8 +3,10 @@\n import re\n import textwrap\n \n+import cfgv\n import yaml\n \n+from pre_commit.clientlib import InvalidConfigError\n from pre_commit.yaml import yaml_load\n \n \n@@ -44,6 +46,13 @@\n     with open(config_file) as f:\n         orig_contents = contents = f.read()\n \n+    with cfgv.reraise_as(InvalidConfigError):\n+        with cfgv.validate_context(f'File {config_file}'):\n+            try:\n+                yaml_load(orig_contents)\n+            except Exception as e:\n+                raise cfgv.ValidationError(str(e))\n+\n     contents = _migrate_map(contents)\n     contents = _migrate_sha_to_rev(contents)\n", "issue": "ParserError exception raised for invalid configuration\n### search you tried in the issue tracker\r\n\r\nParserError and unicode\r\n\r\n### describe your issue\r\n\r\nI executed `pre-commit autoupdate` with an invalid configuration file (the second `- repo` is indented incorrectly) and got this error message:\r\n\r\n````\r\n$ pre-commit autoupdate\r\nAn unexpected error has occurred: ParserError: while parsing a block mapping\r\n  in \"<unicode string>\", line 1, column 1\r\ndid not find expected key\r\n  in \"<unicode string>\", line 7, column 1\r\nCheck the log at /home/carsten/.cache/pre-commit/pre-commit.log\r\n````\r\n\r\nThis is an expected error and I would expect an error message like `Your configuration file \"...\" is wrongly formatted at <pos>. Please review the format of the content.'.\r\n\r\nThank you,\r\nCarsten\r\n\r\n### pre-commit --version\r\n\r\npre-commit 2.21.0\r\n\r\n### .pre-commit-config.yaml\r\n\r\n```yaml\r\nrepos:\r\n  - repo: https://github.com/pre-commit/pre-commit-hooks\r\n    rev: v4.4.0\r\n    hooks:\r\n      - id: check-executables-have-shebangs\r\n\r\n- repo: https://github.com/shellcheck-py/shellcheck-py\r\n    rev: v0.9.0.2\r\n    hooks:\r\n    -   id: shellcheck\r\n```\r\n\r\n\r\n### ~/.cache/pre-commit/pre-commit.log (if present)\r\n\r\n### version information\r\n\r\n```\r\npre-commit version: 2.21.0\r\ngit --version: git version 2.35.3\r\nsys.version:\r\n    3.10.8 (main, Oct 28 2022, 17:28:32) [GCC]\r\nsys.executable: /home/carsten/virtualenv/bin/python3.10\r\nos.name: posix\r\nsys.platform: linux\r\n```\r\n\r\n### error information\r\n\r\n```\r\nAn unexpected error has occurred: ParserError: while parsing a block mapping\r\n  in \"<unicode string>\", line 1, column 1\r\ndid not find expected key\r\n  in \"<unicode string>\", line 7, column 1\r\n```\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/error_handler.py\", line 73, in error_handler\r\n    yield\r\n  File \"/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/main.py\", line 355, in main\r\n    return autoupdate(\r\n  File \"/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/autoupdate.py\", line 154, in autoupdate\r\n    migrate_config(config_file, quiet=True)\r\n  File \"/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/migrate_config.py\", line 47, in migrate_config\r\n    contents = _migrate_map(contents)\r\n  File \"/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/migrate_config.py\", line 16, in _migrate_map\r\n    if isinstance(yaml_load(contents), list):\r\n  File \"/home/carsten/virtualenv/lib64/python3.10/site-packages/yaml/__init__.py\", line 81, in load\r\n    return loader.get_single_data()\r\n  File \"/home/carsten/virtualenv/lib64/python3.10/site-packages/yaml/constructor.py\", line 49, in get_single_data\r\n    node = self.get_single_node()\r\n  File \"yaml/_yaml.pyx\", line 673, in yaml._yaml.CParser.get_single_node\r\n  File \"yaml/_yaml.pyx\", line 687, in yaml._yaml.CParser._compose_document\r\n  File \"yaml/_yaml.pyx\", line 731, in yaml._yaml.CParser._compose_node\r\n  File \"yaml/_yaml.pyx\", line 847, in yaml._yaml.CParser._compose_mapping_node\r\n  File \"yaml/_yaml.pyx\", line 860, in yaml._yaml.CParser._parse_next_event\r\nyaml.parser.ParserError: while parsing a block mapping\r\n  in \"<unicode string>\", line 1, column 1\r\ndid not find expected key\r\n  in \"<unicode string>\", line 7, column 1\r\n```\n", "before_files": [{"content": "from __future__ import annotations\n\nimport re\nimport textwrap\n\nimport yaml\n\nfrom pre_commit.yaml import yaml_load\n\n\ndef _is_header_line(line: str) -> bool:\n    return line.startswith(('#', '---')) or not line.strip()\n\n\ndef _migrate_map(contents: str) -> str:\n    if isinstance(yaml_load(contents), list):\n        # Find the first non-header line\n        lines = contents.splitlines(True)\n        i = 0\n        # Only loop on non empty configuration file\n        while i < len(lines) and _is_header_line(lines[i]):\n            i += 1\n\n        header = ''.join(lines[:i])\n        rest = ''.join(lines[i:])\n\n        # If they are using the \"default\" flow style of yaml, this operation\n        # will yield a valid configuration\n        try:\n            trial_contents = f'{header}repos:\\n{rest}'\n            yaml_load(trial_contents)\n            contents = trial_contents\n        except yaml.YAMLError:\n            contents = f'{header}repos:\\n{textwrap.indent(rest, \" \" * 4)}'\n\n    return contents\n\n\ndef _migrate_sha_to_rev(contents: str) -> str:\n    return re.sub(r'(\\n\\s+)sha:', r'\\1rev:', contents)\n\n\ndef migrate_config(config_file: str, quiet: bool = False) -> int:\n    with open(config_file) as f:\n        orig_contents = contents = f.read()\n\n    contents = _migrate_map(contents)\n    contents = _migrate_sha_to_rev(contents)\n\n    if contents != orig_contents:\n        with open(config_file, 'w') as f:\n            f.write(contents)\n\n        print('Configuration has been migrated.')\n    elif not quiet:\n        print('Configuration is already migrated.')\n    return 0\n", "path": "pre_commit/commands/migrate_config.py"}]}