We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pre-commit autoupdate fails when config is empty
Running `pre-commit autoupdate` with an empty `.pre-commit-config.yaml` results in the below error:
```An unexpected error has occurred: IndexError: list index out of range
Traceback (most recent call last):
  File "/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/error_handler.py", line 46, in error_handler
    yield
  File "/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/main.py", line 286, in main
    repos=args.repos,
  File "/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/commands/autoupdate.py", line 117, in autoupdate
    migrate_config(config_file, quiet=True)
  File "/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/commands/migrate_config.py", line 52, in migrate_config
    contents = _migrate_map(contents)
  File "/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/commands/migrate_config.py", line 24, in _migrate_map
    while _is_header_line(lines[i]):
IndexError: list index out of range
```

--- 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 from __future__ import print_function
2 from __future__ import unicode_literals
3 
4 import io
5 import re
6 
7 import yaml
8 from aspy.yaml import ordered_load
9 
10 
11 def _indent(s):
12     lines = s.splitlines(True)
13     return ''.join(' ' * 4 + line if line.strip() else line for line in lines)
14 
15 
16 def _is_header_line(line):
17     return (line.startswith(('#', '---')) or not line.strip())
18 
19 
20 def _migrate_map(contents):
21     # Find the first non-header line
22     lines = contents.splitlines(True)
23     i = 0
24     while _is_header_line(lines[i]):
25         i += 1
26 
27     header = ''.join(lines[:i])
28     rest = ''.join(lines[i:])
29 
30     if isinstance(ordered_load(contents), list):
31         # If they are using the "default" flow style of yaml, this operation
32         # will yield a valid configuration
33         try:
34             trial_contents = header + 'repos:\n' + rest
35             ordered_load(trial_contents)
36             contents = trial_contents
37         except yaml.YAMLError:
38             contents = header + 'repos:\n' + _indent(rest)
39 
40     return contents
41 
42 
43 def _migrate_sha_to_rev(contents):
44     reg = re.compile(r'(\n\s+)sha:')
45     return reg.sub(r'\1rev:', contents)
46 
47 
48 def migrate_config(config_file, quiet=False):
49     with io.open(config_file) as f:
50         orig_contents = contents = f.read()
51 
52     contents = _migrate_map(contents)
53     contents = _migrate_sha_to_rev(contents)
54 
55     if contents != orig_contents:
56         with io.open(config_file, 'w') as f:
57             f.write(contents)
58 
59         print('Configuration has been migrated.')
60     elif not quiet:
61         print('Configuration is already migrated.')
62
```
--- 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
@@ -21,7 +21,8 @@
     # Find the first non-header line
     lines = contents.splitlines(True)
     i = 0
-    while _is_header_line(lines[i]):
+    # Only loop on non empty configuration file
+    while i < len(lines) and _is_header_line(lines[i]):
         i += 1
 
     header = ''.join(lines[:i])

{"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@@ -21,7 +21,8 @@\n     # Find the first non-header line\n     lines = contents.splitlines(True)\n     i = 0\n-    while _is_header_line(lines[i]):\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", "issue": "pre-commit autoupdate fails when config is empty\nRunning `pre-commit autoupdate` with an empty `.pre-commit-config.yaml` results in the below error:\r\n```An unexpected error has occurred: IndexError: list index out of range\r\nTraceback (most recent call last):\r\n  File \"/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/error_handler.py\", line 46, in error_handler\r\n    yield\r\n  File \"/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/main.py\", line 286, in main\r\n    repos=args.repos,\r\n  File \"/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/commands/autoupdate.py\", line 117, in autoupdate\r\n    migrate_config(config_file, quiet=True)\r\n  File \"/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/commands/migrate_config.py\", line 52, in migrate_config\r\n    contents = _migrate_map(contents)\r\n  File \"/usr/local/Cellar/pre-commit/1.14.2/libexec/lib/python3.7/site-packages/pre_commit/commands/migrate_config.py\", line 24, in _migrate_map\r\n    while _is_header_line(lines[i]):\r\nIndexError: list index out of range\r\n```\n", "before_files": [{"content": "from __future__ import print_function\nfrom __future__ import unicode_literals\n\nimport io\nimport re\n\nimport yaml\nfrom aspy.yaml import ordered_load\n\n\ndef _indent(s):\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):\n    return (line.startswith(('#', '---')) or not line.strip())\n\n\ndef _migrate_map(contents):\n    # Find the first non-header line\n    lines = contents.splitlines(True)\n    i = 0\n    while _is_header_line(lines[i]):\n        i += 1\n\n    header = ''.join(lines[:i])\n    rest = ''.join(lines[i:])\n\n    if isinstance(ordered_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 = header + 'repos:\\n' + rest\n            ordered_load(trial_contents)\n            contents = trial_contents\n        except yaml.YAMLError:\n            contents = header + 'repos:\\n' + _indent(rest)\n\n    return contents\n\n\ndef _migrate_sha_to_rev(contents):\n    reg = re.compile(r'(\\n\\s+)sha:')\n    return reg.sub(r'\\1rev:', contents)\n\n\ndef migrate_config(config_file, quiet=False):\n    with io.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 io.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", "path": "pre_commit/commands/migrate_config.py"}], "after_files": [{"content": "from __future__ import print_function\nfrom __future__ import unicode_literals\n\nimport io\nimport re\n\nimport yaml\nfrom aspy.yaml import ordered_load\n\n\ndef _indent(s):\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):\n    return (line.startswith(('#', '---')) or not line.strip())\n\n\ndef _migrate_map(contents):\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(ordered_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 = header + 'repos:\\n' + rest\n            ordered_load(trial_contents)\n            contents = trial_contents\n        except yaml.YAMLError:\n            contents = header + 'repos:\\n' + _indent(rest)\n\n    return contents\n\n\ndef _migrate_sha_to_rev(contents):\n    reg = re.compile(r'(\\n\\s+)sha:')\n    return reg.sub(r'\\1rev:', contents)\n\n\ndef migrate_config(config_file, quiet=False):\n    with io.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 io.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", "path": "pre_commit/commands/migrate_config.py"}]}