We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Unhandled yaml.scanner.ScannerError when trying autoupdate with a malformed pre-commit config
In migrate_config.py we catch `yaml.YAMLError` on [lines 31-36](https://github.com/pre-commit/pre-commit/blob/master/pre_commit/commands/migrate_config.py#L31-L36) (of which `yaml.scanner.ScannerError` is a subclass), but when the exception is raised on line 28, it is unhandled.

```console
$ pre-commit autoupdate
An unexpected error has occurred: ScannerError: mapping values are not allowed in this context
  in "<unicode string>", line 2, column 6
Check the log at /home/ryan/.cache/pre-commit/pre-commit.log
```

### version information

```
pre-commit version: 2.3.0
sys.version:
    3.8.2 (default, Apr  8 2020, 14:31:25) 
    [GCC 9.3.0]
sys.executable: /home/ryan/.local/pipx/venvs/pre-commit/bin/python
os.name: posix
sys.platform: linux
```

### error information

```
An unexpected error has occurred: ScannerError: mapping values are not allowed in this context
  in "<unicode string>", line 2, column 6
```

```
Traceback (most recent call last):
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/error_handler.py", line 56, in error_handler
    yield
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/main.py", line 354, in main
    return autoupdate(
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/autoupdate.py", line 141, in autoupdate
    migrate_config(config_file, quiet=True)
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/migrate_config.py", line 49, in migrate_config
    contents = _migrate_map(contents)
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/migrate_config.py", line 28, in _migrate_map
    if isinstance(yaml_load(contents), list):
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/yaml/__init__.py", line 114, in load
    return loader.get_single_data()
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/yaml/constructor.py", line 49, in get_single_data
    node = self.get_single_node()
  File "ext/_yaml.pyx", line 707, in _yaml.CParser.get_single_node
  File "ext/_yaml.pyx", line 726, in _yaml.CParser._compose_document
  File "ext/_yaml.pyx", line 905, in _yaml.CParser._parse_next_event
yaml.scanner.ScannerError: mapping values are not allowed in this context
  in "<unicode string>", line 2, column 6
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pre_commit/commands/migrate_config.py`
Content:
```
1 import re
2 
3 import yaml
4 
5 from pre_commit.util import yaml_load
6 
7 
8 def _indent(s: str) -> str:
9     lines = s.splitlines(True)
10     return ''.join(' ' * 4 + line if line.strip() else line for line in lines)
11 
12 
13 def _is_header_line(line: str) -> bool:
14     return line.startswith(('#', '---')) or not line.strip()
15 
16 
17 def _migrate_map(contents: str) -> str:
18     # Find the first non-header line
19     lines = contents.splitlines(True)
20     i = 0
21     # Only loop on non empty configuration file
22     while i < len(lines) and _is_header_line(lines[i]):
23         i += 1
24 
25     header = ''.join(lines[:i])
26     rest = ''.join(lines[i:])
27 
28     if isinstance(yaml_load(contents), list):
29         # If they are using the "default" flow style of yaml, this operation
30         # will yield a valid configuration
31         try:
32             trial_contents = f'{header}repos:\n{rest}'
33             yaml_load(trial_contents)
34             contents = trial_contents
35         except yaml.YAMLError:
36             contents = f'{header}repos:\n{_indent(rest)}'
37 
38     return contents
39 
40 
41 def _migrate_sha_to_rev(contents: str) -> str:
42     return re.sub(r'(\n\s+)sha:', r'\1rev:', contents)
43 
44 
45 def migrate_config(config_file: str, quiet: bool = False) -> int:
46     with open(config_file) as f:
47         orig_contents = contents = f.read()
48 
49     contents = _migrate_map(contents)
50     contents = _migrate_sha_to_rev(contents)
51 
52     if contents != orig_contents:
53         with open(config_file, 'w') as f:
54             f.write(contents)
55 
56         print('Configuration has been migrated.')
57     elif not quiet:
58         print('Configuration is already migrated.')
59     return 0
60
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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
@@ -2,6 +2,7 @@
 
 import yaml
 
+from pre_commit.clientlib import load_config
 from pre_commit.util import yaml_load
 
 
@@ -43,6 +44,9 @@
 
 
 def migrate_config(config_file: str, quiet: bool = False) -> int:
+    # ensure that the configuration is a valid pre-commit configuration
+    load_config(config_file)
+
     with open(config_file) as f:
         orig_contents = contents = f.read()

{"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@@ -2,6 +2,7 @@\n \n import yaml\n \n+from pre_commit.clientlib import load_config\n from pre_commit.util import yaml_load\n \n \n@@ -43,6 +44,9 @@\n \n \n def migrate_config(config_file: str, quiet: bool = False) -> int:\n+    # ensure that the configuration is a valid pre-commit configuration\n+    load_config(config_file)\n+\n     with open(config_file) as f:\n         orig_contents = contents = f.read()\n", "issue": "Unhandled yaml.scanner.ScannerError when trying autoupdate with a malformed pre-commit config\nIn migrate_config.py we catch `yaml.YAMLError` on [lines 31-36](https://github.com/pre-commit/pre-commit/blob/master/pre_commit/commands/migrate_config.py#L31-L36) (of which `yaml.scanner.ScannerError` is a subclass), but when the exception is raised on line 28, it is unhandled.\r\n\r\n```console\r\n$ pre-commit autoupdate\r\nAn unexpected error has occurred: ScannerError: mapping values are not allowed in this context\r\n  in \"<unicode string>\", line 2, column 6\r\nCheck the log at /home/ryan/.cache/pre-commit/pre-commit.log\r\n```\r\n\r\n### version information\r\n\r\n```\r\npre-commit version: 2.3.0\r\nsys.version:\r\n    3.8.2 (default, Apr  8 2020, 14:31:25) \r\n    [GCC 9.3.0]\r\nsys.executable: /home/ryan/.local/pipx/venvs/pre-commit/bin/python\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: ScannerError: mapping values are not allowed in this context\r\n  in \"<unicode string>\", line 2, column 6\r\n```\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/error_handler.py\", line 56, in error_handler\r\n    yield\r\n  File \"/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/main.py\", line 354, in main\r\n    return autoupdate(\r\n  File \"/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/autoupdate.py\", line 141, in autoupdate\r\n    migrate_config(config_file, quiet=True)\r\n  File \"/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/migrate_config.py\", line 49, in migrate_config\r\n    contents = _migrate_map(contents)\r\n  File \"/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/migrate_config.py\", line 28, in _migrate_map\r\n    if isinstance(yaml_load(contents), list):\r\n  File \"/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/yaml/__init__.py\", line 114, in load\r\n    return loader.get_single_data()\r\n  File \"/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/yaml/constructor.py\", line 49, in get_single_data\r\n    node = self.get_single_node()\r\n  File \"ext/_yaml.pyx\", line 707, in _yaml.CParser.get_single_node\r\n  File \"ext/_yaml.pyx\", line 726, in _yaml.CParser._compose_document\r\n  File \"ext/_yaml.pyx\", line 905, in _yaml.CParser._parse_next_event\r\nyaml.scanner.ScannerError: mapping values are not allowed in this context\r\n  in \"<unicode string>\", line 2, column 6\r\n```\r\n\n", "before_files": [{"content": "import re\n\nimport yaml\n\nfrom pre_commit.util import yaml_load\n\n\ndef _indent(s: str) -> str:\n    lines = s.splitlines(True)\n    return ''.join(' ' * 4 + line if line.strip() else line for line in lines)\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    # 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 isinstance(yaml_load(contents), list):\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{_indent(rest)}'\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"}], "after_files": [{"content": "import re\n\nimport yaml\n\nfrom pre_commit.clientlib import load_config\nfrom pre_commit.util import yaml_load\n\n\ndef _indent(s: str) -> str:\n    lines = s.splitlines(True)\n    return ''.join(' ' * 4 + line if line.strip() else line for line in lines)\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    # 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 isinstance(yaml_load(contents), list):\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{_indent(rest)}'\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    # ensure that the configuration is a valid pre-commit configuration\n    load_config(config_file)\n\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"}]}