We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
`pre-commit init-templatedir` should expanduser before checking value to warn on
https://github.com/pre-commit/pre-commit/issues/1084#issuecomment-513744333

--- 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/init_templatedir.py`
Content:
```
1 import logging
2 import os.path
3 
4 from pre_commit.commands.install_uninstall import install
5 from pre_commit.util import cmd_output
6 
7 logger = logging.getLogger('pre_commit')
8 
9 
10 def init_templatedir(config_file, store, directory, hook_type):
11     install(
12         config_file, store, overwrite=True, hook_type=hook_type,
13         skip_on_missing_config=True, git_dir=directory,
14     )
15     _, out, _ = cmd_output('git', 'config', 'init.templateDir', retcode=None)
16     dest = os.path.realpath(directory)
17     if os.path.realpath(out.strip()) != dest:
18         logger.warning('`init.templateDir` not set to the target directory')
19         logger.warning(
20             'maybe `git config --global init.templateDir {}`?'.format(dest),
21         )
22
```

Path: `pre_commit/main.py`
Content:
```
1 from __future__ import unicode_literals
2 
3 import argparse
4 import logging
5 import os
6 import sys
7 
8 import pre_commit.constants as C
9 from pre_commit import color
10 from pre_commit import five
11 from pre_commit import git
12 from pre_commit.commands.autoupdate import autoupdate
13 from pre_commit.commands.clean import clean
14 from pre_commit.commands.gc import gc
15 from pre_commit.commands.init_templatedir import init_templatedir
16 from pre_commit.commands.install_uninstall import install
17 from pre_commit.commands.install_uninstall import install_hooks
18 from pre_commit.commands.install_uninstall import uninstall
19 from pre_commit.commands.migrate_config import migrate_config
20 from pre_commit.commands.run import run
21 from pre_commit.commands.sample_config import sample_config
22 from pre_commit.commands.try_repo import try_repo
23 from pre_commit.error_handler import error_handler
24 from pre_commit.error_handler import FatalError
25 from pre_commit.logging_handler import logging_handler
26 from pre_commit.store import Store
27 from pre_commit.util import CalledProcessError
28 
29 
30 logger = logging.getLogger('pre_commit')
31 
32 # https://github.com/pre-commit/pre-commit/issues/217
33 # On OSX, making a virtualenv using pyvenv at . causes `virtualenv` and `pip`
34 # to install packages to the wrong place.  We don't want anything to deal with
35 # pyvenv
36 os.environ.pop('__PYVENV_LAUNCHER__', None)
37 
38 
39 def _add_color_option(parser):
40     parser.add_argument(
41         '--color', default=os.environ.get('PRE_COMMIT_COLOR', 'auto'),
42         type=color.use_color,
43         metavar='{' + ','.join(color.COLOR_CHOICES) + '}',
44         help='Whether to use color in output.  Defaults to `%(default)s`.',
45     )
46 
47 
48 def _add_config_option(parser):
49     parser.add_argument(
50         '-c', '--config', default=C.CONFIG_FILE,
51         help='Path to alternate config file',
52     )
53 
54 
55 def _add_hook_type_option(parser):
56     parser.add_argument(
57         '-t', '--hook-type', choices=(
58             'pre-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg',
59         ),
60         default='pre-commit',
61     )
62 
63 
64 def _add_run_options(parser):
65     parser.add_argument('hook', nargs='?', help='A single hook-id to run')
66     parser.add_argument('--verbose', '-v', action='store_true', default=False)
67     parser.add_argument(
68         '--origin', '-o',
69         help="The origin branch's commit_id when using `git push`.",
70     )
71     parser.add_argument(
72         '--source', '-s',
73         help="The remote branch's commit_id when using `git push`.",
74     )
75     parser.add_argument(
76         '--commit-msg-filename',
77         help='Filename to check when running during `commit-msg`',
78     )
79     parser.add_argument(
80         '--hook-stage', choices=C.STAGES, default='commit',
81         help='The stage during which the hook is fired.  One of %(choices)s',
82     )
83     parser.add_argument(
84         '--show-diff-on-failure', action='store_true',
85         help='When hooks fail, run `git diff` directly afterward.',
86     )
87     mutex_group = parser.add_mutually_exclusive_group(required=False)
88     mutex_group.add_argument(
89         '--all-files', '-a', action='store_true', default=False,
90         help='Run on all the files in the repo.',
91     )
92     mutex_group.add_argument(
93         '--files', nargs='*', default=[],
94         help='Specific filenames to run hooks on.',
95     )
96 
97 
98 def _adjust_args_and_chdir(args):
99     # `--config` was specified relative to the non-root working directory
100     if os.path.exists(args.config):
101         args.config = os.path.abspath(args.config)
102     if args.command in {'run', 'try-repo'}:
103         args.files = [os.path.abspath(filename) for filename in args.files]
104     if args.command == 'try-repo' and os.path.exists(args.repo):
105         args.repo = os.path.abspath(args.repo)
106 
107     try:
108         os.chdir(git.get_root())
109     except CalledProcessError:
110         raise FatalError(
111             'git failed. Is it installed, and are you in a Git repository '
112             'directory?',
113         )
114 
115     args.config = os.path.relpath(args.config)
116     if args.command in {'run', 'try-repo'}:
117         args.files = [os.path.relpath(filename) for filename in args.files]
118     if args.command == 'try-repo' and os.path.exists(args.repo):
119         args.repo = os.path.relpath(args.repo)
120 
121 
122 def main(argv=None):
123     argv = argv if argv is not None else sys.argv[1:]
124     argv = [five.to_text(arg) for arg in argv]
125     parser = argparse.ArgumentParser()
126 
127     # https://stackoverflow.com/a/8521644/812183
128     parser.add_argument(
129         '-V', '--version',
130         action='version',
131         version='%(prog)s {}'.format(C.VERSION),
132     )
133 
134     subparsers = parser.add_subparsers(dest='command')
135 
136     autoupdate_parser = subparsers.add_parser(
137         'autoupdate',
138         help="Auto-update pre-commit config to the latest repos' versions.",
139     )
140     _add_color_option(autoupdate_parser)
141     _add_config_option(autoupdate_parser)
142     autoupdate_parser.add_argument(
143         '--tags-only', action='store_true', help='LEGACY: for compatibility',
144     )
145     autoupdate_parser.add_argument(
146         '--bleeding-edge', action='store_true',
147         help=(
148             'Update to the bleeding edge of `master` instead of the latest '
149             'tagged version (the default behavior).'
150         ),
151     )
152     autoupdate_parser.add_argument(
153         '--repo', dest='repos', action='append', metavar='REPO',
154         help='Only update this repository -- may be specified multiple times.',
155     )
156 
157     clean_parser = subparsers.add_parser(
158         'clean', help='Clean out pre-commit files.',
159     )
160     _add_color_option(clean_parser)
161     _add_config_option(clean_parser)
162 
163     gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.')
164     _add_color_option(gc_parser)
165     _add_config_option(gc_parser)
166 
167     init_templatedir_parser = subparsers.add_parser(
168         'init-templatedir',
169         help=(
170             'Install hook script in a directory intended for use with '
171             '`git config init.templateDir`.'
172         ),
173     )
174     _add_color_option(init_templatedir_parser)
175     _add_config_option(init_templatedir_parser)
176     init_templatedir_parser.add_argument(
177         'directory', help='The directory in which to write the hook script.',
178     )
179     _add_hook_type_option(init_templatedir_parser)
180 
181     install_parser = subparsers.add_parser(
182         'install', help='Install the pre-commit script.',
183     )
184     _add_color_option(install_parser)
185     _add_config_option(install_parser)
186     install_parser.add_argument(
187         '-f', '--overwrite', action='store_true',
188         help='Overwrite existing hooks / remove migration mode.',
189     )
190     install_parser.add_argument(
191         '--install-hooks', action='store_true',
192         help=(
193             'Whether to install hook environments for all environments '
194             'in the config file.'
195         ),
196     )
197     _add_hook_type_option(install_parser)
198     install_parser.add_argument(
199         '--allow-missing-config', action='store_true', default=False,
200         help=(
201             'Whether to allow a missing `pre-commit` configuration file '
202             'or exit with a failure code.'
203         ),
204     )
205 
206     install_hooks_parser = subparsers.add_parser(
207         'install-hooks',
208         help=(
209             'Install hook environments for all environments in the config '
210             'file.  You may find `pre-commit install --install-hooks` more '
211             'useful.'
212         ),
213     )
214     _add_color_option(install_hooks_parser)
215     _add_config_option(install_hooks_parser)
216 
217     migrate_config_parser = subparsers.add_parser(
218         'migrate-config',
219         help='Migrate list configuration to new map configuration.',
220     )
221     _add_color_option(migrate_config_parser)
222     _add_config_option(migrate_config_parser)
223 
224     run_parser = subparsers.add_parser('run', help='Run hooks.')
225     _add_color_option(run_parser)
226     _add_config_option(run_parser)
227     _add_run_options(run_parser)
228 
229     sample_config_parser = subparsers.add_parser(
230         'sample-config', help='Produce a sample {} file'.format(C.CONFIG_FILE),
231     )
232     _add_color_option(sample_config_parser)
233     _add_config_option(sample_config_parser)
234 
235     try_repo_parser = subparsers.add_parser(
236         'try-repo',
237         help='Try the hooks in a repository, useful for developing new hooks.',
238     )
239     _add_color_option(try_repo_parser)
240     _add_config_option(try_repo_parser)
241     try_repo_parser.add_argument(
242         'repo', help='Repository to source hooks from.',
243     )
244     try_repo_parser.add_argument(
245         '--ref', '--rev',
246         help=(
247             'Manually select a rev to run against, otherwise the `HEAD` '
248             'revision will be used.'
249         ),
250     )
251     _add_run_options(try_repo_parser)
252 
253     uninstall_parser = subparsers.add_parser(
254         'uninstall', help='Uninstall the pre-commit script.',
255     )
256     _add_color_option(uninstall_parser)
257     _add_config_option(uninstall_parser)
258     _add_hook_type_option(uninstall_parser)
259 
260     help = subparsers.add_parser(
261         'help', help='Show help for a specific command.',
262     )
263     help.add_argument('help_cmd', nargs='?', help='Command to show help for.')
264 
265     # argparse doesn't really provide a way to use a `default` subparser
266     if len(argv) == 0:
267         argv = ['run']
268     args = parser.parse_args(argv)
269 
270     if args.command == 'help' and args.help_cmd:
271         parser.parse_args([args.help_cmd, '--help'])
272     elif args.command == 'help':
273         parser.parse_args(['--help'])
274 
275     with error_handler(), logging_handler(args.color):
276         if args.command not in {'clean', 'gc', 'sample-config'}:
277             _adjust_args_and_chdir(args)
278 
279         git.check_for_cygwin_mismatch()
280 
281         store = Store()
282         store.mark_config_used(args.config)
283 
284         if args.command == 'autoupdate':
285             if args.tags_only:
286                 logger.warning('--tags-only is the default')
287             return autoupdate(
288                 args.config, store,
289                 tags_only=not args.bleeding_edge,
290                 repos=args.repos,
291             )
292         elif args.command == 'clean':
293             return clean(store)
294         elif args.command == 'gc':
295             return gc(store)
296         elif args.command == 'install':
297             return install(
298                 args.config, store,
299                 overwrite=args.overwrite, hooks=args.install_hooks,
300                 hook_type=args.hook_type,
301                 skip_on_missing_config=args.allow_missing_config,
302             )
303         elif args.command == 'init-templatedir':
304             return init_templatedir(
305                 args.config, store,
306                 args.directory, hook_type=args.hook_type,
307             )
308         elif args.command == 'install-hooks':
309             return install_hooks(args.config, store)
310         elif args.command == 'migrate-config':
311             return migrate_config(args.config)
312         elif args.command == 'run':
313             return run(args.config, store, args)
314         elif args.command == 'sample-config':
315             return sample_config()
316         elif args.command == 'try-repo':
317             return try_repo(args)
318         elif args.command == 'uninstall':
319             return uninstall(hook_type=args.hook_type)
320         else:
321             raise NotImplementedError(
322                 'Command {} not implemented.'.format(args.command),
323             )
324 
325         raise AssertionError(
326             'Command {} failed to exit with a returncode'.format(args.command),
327         )
328 
329 
330 if __name__ == '__main__':
331     exit(main())
332
```
--- 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/init_templatedir.py b/pre_commit/commands/init_templatedir.py
--- a/pre_commit/commands/init_templatedir.py
+++ b/pre_commit/commands/init_templatedir.py
@@ -2,6 +2,7 @@
 import os.path
 
 from pre_commit.commands.install_uninstall import install
+from pre_commit.util import CalledProcessError
 from pre_commit.util import cmd_output
 
 logger = logging.getLogger('pre_commit')
@@ -12,9 +13,14 @@
         config_file, store, overwrite=True, hook_type=hook_type,
         skip_on_missing_config=True, git_dir=directory,
     )
-    _, out, _ = cmd_output('git', 'config', 'init.templateDir', retcode=None)
+    try:
+        _, out, _ = cmd_output('git', 'config', 'init.templateDir')
+    except CalledProcessError:
+        configured_path = None
+    else:
+        configured_path = os.path.realpath(os.path.expanduser(out.strip()))
     dest = os.path.realpath(directory)
-    if os.path.realpath(out.strip()) != dest:
+    if configured_path != dest:
         logger.warning('`init.templateDir` not set to the target directory')
         logger.warning(
             'maybe `git config --global init.templateDir {}`?'.format(dest),
diff --git a/pre_commit/main.py b/pre_commit/main.py
--- a/pre_commit/main.py
+++ b/pre_commit/main.py
@@ -36,6 +36,9 @@
 os.environ.pop('__PYVENV_LAUNCHER__', None)
 
 
+COMMANDS_NO_GIT = {'clean', 'gc', 'init-templatedir', 'sample-config'}
+
+
 def _add_color_option(parser):
     parser.add_argument(
         '--color', default=os.environ.get('PRE_COMMIT_COLOR', 'auto'),
@@ -273,7 +276,7 @@
         parser.parse_args(['--help'])
 
     with error_handler(), logging_handler(args.color):
-        if args.command not in {'clean', 'gc', 'sample-config'}:
+        if args.command not in COMMANDS_NO_GIT:
             _adjust_args_and_chdir(args)
 
         git.check_for_cygwin_mismatch()

{"golden_diff": "diff --git a/pre_commit/commands/init_templatedir.py b/pre_commit/commands/init_templatedir.py\n--- a/pre_commit/commands/init_templatedir.py\n+++ b/pre_commit/commands/init_templatedir.py\n@@ -2,6 +2,7 @@\n import os.path\n \n from pre_commit.commands.install_uninstall import install\n+from pre_commit.util import CalledProcessError\n from pre_commit.util import cmd_output\n \n logger = logging.getLogger('pre_commit')\n@@ -12,9 +13,14 @@\n         config_file, store, overwrite=True, hook_type=hook_type,\n         skip_on_missing_config=True, git_dir=directory,\n     )\n-    _, out, _ = cmd_output('git', 'config', 'init.templateDir', retcode=None)\n+    try:\n+        _, out, _ = cmd_output('git', 'config', 'init.templateDir')\n+    except CalledProcessError:\n+        configured_path = None\n+    else:\n+        configured_path = os.path.realpath(os.path.expanduser(out.strip()))\n     dest = os.path.realpath(directory)\n-    if os.path.realpath(out.strip()) != dest:\n+    if configured_path != dest:\n         logger.warning('`init.templateDir` not set to the target directory')\n         logger.warning(\n             'maybe `git config --global init.templateDir {}`?'.format(dest),\ndiff --git a/pre_commit/main.py b/pre_commit/main.py\n--- a/pre_commit/main.py\n+++ b/pre_commit/main.py\n@@ -36,6 +36,9 @@\n os.environ.pop('__PYVENV_LAUNCHER__', None)\n \n \n+COMMANDS_NO_GIT = {'clean', 'gc', 'init-templatedir', 'sample-config'}\n+\n+\n def _add_color_option(parser):\n     parser.add_argument(\n         '--color', default=os.environ.get('PRE_COMMIT_COLOR', 'auto'),\n@@ -273,7 +276,7 @@\n         parser.parse_args(['--help'])\n \n     with error_handler(), logging_handler(args.color):\n-        if args.command not in {'clean', 'gc', 'sample-config'}:\n+        if args.command not in COMMANDS_NO_GIT:\n             _adjust_args_and_chdir(args)\n \n         git.check_for_cygwin_mismatch()\n", "issue": "`pre-commit init-templatedir` should expanduser before checking value to warn on\nhttps://github.com/pre-commit/pre-commit/issues/1084#issuecomment-513744333\n", "before_files": [{"content": "import logging\nimport os.path\n\nfrom pre_commit.commands.install_uninstall import install\nfrom pre_commit.util import cmd_output\n\nlogger = logging.getLogger('pre_commit')\n\n\ndef init_templatedir(config_file, store, directory, hook_type):\n    install(\n        config_file, store, overwrite=True, hook_type=hook_type,\n        skip_on_missing_config=True, git_dir=directory,\n    )\n    _, out, _ = cmd_output('git', 'config', 'init.templateDir', retcode=None)\n    dest = os.path.realpath(directory)\n    if os.path.realpath(out.strip()) != dest:\n        logger.warning('`init.templateDir` not set to the target directory')\n        logger.warning(\n            'maybe `git config --global init.templateDir {}`?'.format(dest),\n        )\n", "path": "pre_commit/commands/init_templatedir.py"}, {"content": "from __future__ import unicode_literals\n\nimport argparse\nimport logging\nimport os\nimport sys\n\nimport pre_commit.constants as C\nfrom pre_commit import color\nfrom pre_commit import five\nfrom pre_commit import git\nfrom pre_commit.commands.autoupdate import autoupdate\nfrom pre_commit.commands.clean import clean\nfrom pre_commit.commands.gc import gc\nfrom pre_commit.commands.init_templatedir import init_templatedir\nfrom pre_commit.commands.install_uninstall import install\nfrom pre_commit.commands.install_uninstall import install_hooks\nfrom pre_commit.commands.install_uninstall import uninstall\nfrom pre_commit.commands.migrate_config import migrate_config\nfrom pre_commit.commands.run import run\nfrom pre_commit.commands.sample_config import sample_config\nfrom pre_commit.commands.try_repo import try_repo\nfrom pre_commit.error_handler import error_handler\nfrom pre_commit.error_handler import FatalError\nfrom pre_commit.logging_handler import logging_handler\nfrom pre_commit.store import Store\nfrom pre_commit.util import CalledProcessError\n\n\nlogger = logging.getLogger('pre_commit')\n\n# https://github.com/pre-commit/pre-commit/issues/217\n# On OSX, making a virtualenv using pyvenv at . causes `virtualenv` and `pip`\n# to install packages to the wrong place.  We don't want anything to deal with\n# pyvenv\nos.environ.pop('__PYVENV_LAUNCHER__', None)\n\n\ndef _add_color_option(parser):\n    parser.add_argument(\n        '--color', default=os.environ.get('PRE_COMMIT_COLOR', 'auto'),\n        type=color.use_color,\n        metavar='{' + ','.join(color.COLOR_CHOICES) + '}',\n        help='Whether to use color in output.  Defaults to `%(default)s`.',\n    )\n\n\ndef _add_config_option(parser):\n    parser.add_argument(\n        '-c', '--config', default=C.CONFIG_FILE,\n        help='Path to alternate config file',\n    )\n\n\ndef _add_hook_type_option(parser):\n    parser.add_argument(\n        '-t', '--hook-type', choices=(\n            'pre-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg',\n        ),\n        default='pre-commit',\n    )\n\n\ndef _add_run_options(parser):\n    parser.add_argument('hook', nargs='?', help='A single hook-id to run')\n    parser.add_argument('--verbose', '-v', action='store_true', default=False)\n    parser.add_argument(\n        '--origin', '-o',\n        help=\"The origin branch's commit_id when using `git push`.\",\n    )\n    parser.add_argument(\n        '--source', '-s',\n        help=\"The remote branch's commit_id when using `git push`.\",\n    )\n    parser.add_argument(\n        '--commit-msg-filename',\n        help='Filename to check when running during `commit-msg`',\n    )\n    parser.add_argument(\n        '--hook-stage', choices=C.STAGES, default='commit',\n        help='The stage during which the hook is fired.  One of %(choices)s',\n    )\n    parser.add_argument(\n        '--show-diff-on-failure', action='store_true',\n        help='When hooks fail, run `git diff` directly afterward.',\n    )\n    mutex_group = parser.add_mutually_exclusive_group(required=False)\n    mutex_group.add_argument(\n        '--all-files', '-a', action='store_true', default=False,\n        help='Run on all the files in the repo.',\n    )\n    mutex_group.add_argument(\n        '--files', nargs='*', default=[],\n        help='Specific filenames to run hooks on.',\n    )\n\n\ndef _adjust_args_and_chdir(args):\n    # `--config` was specified relative to the non-root working directory\n    if os.path.exists(args.config):\n        args.config = os.path.abspath(args.config)\n    if args.command in {'run', 'try-repo'}:\n        args.files = [os.path.abspath(filename) for filename in args.files]\n    if args.command == 'try-repo' and os.path.exists(args.repo):\n        args.repo = os.path.abspath(args.repo)\n\n    try:\n        os.chdir(git.get_root())\n    except CalledProcessError:\n        raise FatalError(\n            'git failed. Is it installed, and are you in a Git repository '\n            'directory?',\n        )\n\n    args.config = os.path.relpath(args.config)\n    if args.command in {'run', 'try-repo'}:\n        args.files = [os.path.relpath(filename) for filename in args.files]\n    if args.command == 'try-repo' and os.path.exists(args.repo):\n        args.repo = os.path.relpath(args.repo)\n\n\ndef main(argv=None):\n    argv = argv if argv is not None else sys.argv[1:]\n    argv = [five.to_text(arg) for arg in argv]\n    parser = argparse.ArgumentParser()\n\n    # https://stackoverflow.com/a/8521644/812183\n    parser.add_argument(\n        '-V', '--version',\n        action='version',\n        version='%(prog)s {}'.format(C.VERSION),\n    )\n\n    subparsers = parser.add_subparsers(dest='command')\n\n    autoupdate_parser = subparsers.add_parser(\n        'autoupdate',\n        help=\"Auto-update pre-commit config to the latest repos' versions.\",\n    )\n    _add_color_option(autoupdate_parser)\n    _add_config_option(autoupdate_parser)\n    autoupdate_parser.add_argument(\n        '--tags-only', action='store_true', help='LEGACY: for compatibility',\n    )\n    autoupdate_parser.add_argument(\n        '--bleeding-edge', action='store_true',\n        help=(\n            'Update to the bleeding edge of `master` instead of the latest '\n            'tagged version (the default behavior).'\n        ),\n    )\n    autoupdate_parser.add_argument(\n        '--repo', dest='repos', action='append', metavar='REPO',\n        help='Only update this repository -- may be specified multiple times.',\n    )\n\n    clean_parser = subparsers.add_parser(\n        'clean', help='Clean out pre-commit files.',\n    )\n    _add_color_option(clean_parser)\n    _add_config_option(clean_parser)\n\n    gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.')\n    _add_color_option(gc_parser)\n    _add_config_option(gc_parser)\n\n    init_templatedir_parser = subparsers.add_parser(\n        'init-templatedir',\n        help=(\n            'Install hook script in a directory intended for use with '\n            '`git config init.templateDir`.'\n        ),\n    )\n    _add_color_option(init_templatedir_parser)\n    _add_config_option(init_templatedir_parser)\n    init_templatedir_parser.add_argument(\n        'directory', help='The directory in which to write the hook script.',\n    )\n    _add_hook_type_option(init_templatedir_parser)\n\n    install_parser = subparsers.add_parser(\n        'install', help='Install the pre-commit script.',\n    )\n    _add_color_option(install_parser)\n    _add_config_option(install_parser)\n    install_parser.add_argument(\n        '-f', '--overwrite', action='store_true',\n        help='Overwrite existing hooks / remove migration mode.',\n    )\n    install_parser.add_argument(\n        '--install-hooks', action='store_true',\n        help=(\n            'Whether to install hook environments for all environments '\n            'in the config file.'\n        ),\n    )\n    _add_hook_type_option(install_parser)\n    install_parser.add_argument(\n        '--allow-missing-config', action='store_true', default=False,\n        help=(\n            'Whether to allow a missing `pre-commit` configuration file '\n            'or exit with a failure code.'\n        ),\n    )\n\n    install_hooks_parser = subparsers.add_parser(\n        'install-hooks',\n        help=(\n            'Install hook environments for all environments in the config '\n            'file.  You may find `pre-commit install --install-hooks` more '\n            'useful.'\n        ),\n    )\n    _add_color_option(install_hooks_parser)\n    _add_config_option(install_hooks_parser)\n\n    migrate_config_parser = subparsers.add_parser(\n        'migrate-config',\n        help='Migrate list configuration to new map configuration.',\n    )\n    _add_color_option(migrate_config_parser)\n    _add_config_option(migrate_config_parser)\n\n    run_parser = subparsers.add_parser('run', help='Run hooks.')\n    _add_color_option(run_parser)\n    _add_config_option(run_parser)\n    _add_run_options(run_parser)\n\n    sample_config_parser = subparsers.add_parser(\n        'sample-config', help='Produce a sample {} file'.format(C.CONFIG_FILE),\n    )\n    _add_color_option(sample_config_parser)\n    _add_config_option(sample_config_parser)\n\n    try_repo_parser = subparsers.add_parser(\n        'try-repo',\n        help='Try the hooks in a repository, useful for developing new hooks.',\n    )\n    _add_color_option(try_repo_parser)\n    _add_config_option(try_repo_parser)\n    try_repo_parser.add_argument(\n        'repo', help='Repository to source hooks from.',\n    )\n    try_repo_parser.add_argument(\n        '--ref', '--rev',\n        help=(\n            'Manually select a rev to run against, otherwise the `HEAD` '\n            'revision will be used.'\n        ),\n    )\n    _add_run_options(try_repo_parser)\n\n    uninstall_parser = subparsers.add_parser(\n        'uninstall', help='Uninstall the pre-commit script.',\n    )\n    _add_color_option(uninstall_parser)\n    _add_config_option(uninstall_parser)\n    _add_hook_type_option(uninstall_parser)\n\n    help = subparsers.add_parser(\n        'help', help='Show help for a specific command.',\n    )\n    help.add_argument('help_cmd', nargs='?', help='Command to show help for.')\n\n    # argparse doesn't really provide a way to use a `default` subparser\n    if len(argv) == 0:\n        argv = ['run']\n    args = parser.parse_args(argv)\n\n    if args.command == 'help' and args.help_cmd:\n        parser.parse_args([args.help_cmd, '--help'])\n    elif args.command == 'help':\n        parser.parse_args(['--help'])\n\n    with error_handler(), logging_handler(args.color):\n        if args.command not in {'clean', 'gc', 'sample-config'}:\n            _adjust_args_and_chdir(args)\n\n        git.check_for_cygwin_mismatch()\n\n        store = Store()\n        store.mark_config_used(args.config)\n\n        if args.command == 'autoupdate':\n            if args.tags_only:\n                logger.warning('--tags-only is the default')\n            return autoupdate(\n                args.config, store,\n                tags_only=not args.bleeding_edge,\n                repos=args.repos,\n            )\n        elif args.command == 'clean':\n            return clean(store)\n        elif args.command == 'gc':\n            return gc(store)\n        elif args.command == 'install':\n            return install(\n                args.config, store,\n                overwrite=args.overwrite, hooks=args.install_hooks,\n                hook_type=args.hook_type,\n                skip_on_missing_config=args.allow_missing_config,\n            )\n        elif args.command == 'init-templatedir':\n            return init_templatedir(\n                args.config, store,\n                args.directory, hook_type=args.hook_type,\n            )\n        elif args.command == 'install-hooks':\n            return install_hooks(args.config, store)\n        elif args.command == 'migrate-config':\n            return migrate_config(args.config)\n        elif args.command == 'run':\n            return run(args.config, store, args)\n        elif args.command == 'sample-config':\n            return sample_config()\n        elif args.command == 'try-repo':\n            return try_repo(args)\n        elif args.command == 'uninstall':\n            return uninstall(hook_type=args.hook_type)\n        else:\n            raise NotImplementedError(\n                'Command {} not implemented.'.format(args.command),\n            )\n\n        raise AssertionError(\n            'Command {} failed to exit with a returncode'.format(args.command),\n        )\n\n\nif __name__ == '__main__':\n    exit(main())\n", "path": "pre_commit/main.py"}], "after_files": [{"content": "import logging\nimport os.path\n\nfrom pre_commit.commands.install_uninstall import install\nfrom pre_commit.util import CalledProcessError\nfrom pre_commit.util import cmd_output\n\nlogger = logging.getLogger('pre_commit')\n\n\ndef init_templatedir(config_file, store, directory, hook_type):\n    install(\n        config_file, store, overwrite=True, hook_type=hook_type,\n        skip_on_missing_config=True, git_dir=directory,\n    )\n    try:\n        _, out, _ = cmd_output('git', 'config', 'init.templateDir')\n    except CalledProcessError:\n        configured_path = None\n    else:\n        configured_path = os.path.realpath(os.path.expanduser(out.strip()))\n    dest = os.path.realpath(directory)\n    if configured_path != dest:\n        logger.warning('`init.templateDir` not set to the target directory')\n        logger.warning(\n            'maybe `git config --global init.templateDir {}`?'.format(dest),\n        )\n", "path": "pre_commit/commands/init_templatedir.py"}, {"content": "from __future__ import unicode_literals\n\nimport argparse\nimport logging\nimport os\nimport sys\n\nimport pre_commit.constants as C\nfrom pre_commit import color\nfrom pre_commit import five\nfrom pre_commit import git\nfrom pre_commit.commands.autoupdate import autoupdate\nfrom pre_commit.commands.clean import clean\nfrom pre_commit.commands.gc import gc\nfrom pre_commit.commands.init_templatedir import init_templatedir\nfrom pre_commit.commands.install_uninstall import install\nfrom pre_commit.commands.install_uninstall import install_hooks\nfrom pre_commit.commands.install_uninstall import uninstall\nfrom pre_commit.commands.migrate_config import migrate_config\nfrom pre_commit.commands.run import run\nfrom pre_commit.commands.sample_config import sample_config\nfrom pre_commit.commands.try_repo import try_repo\nfrom pre_commit.error_handler import error_handler\nfrom pre_commit.error_handler import FatalError\nfrom pre_commit.logging_handler import logging_handler\nfrom pre_commit.store import Store\nfrom pre_commit.util import CalledProcessError\n\n\nlogger = logging.getLogger('pre_commit')\n\n# https://github.com/pre-commit/pre-commit/issues/217\n# On OSX, making a virtualenv using pyvenv at . causes `virtualenv` and `pip`\n# to install packages to the wrong place.  We don't want anything to deal with\n# pyvenv\nos.environ.pop('__PYVENV_LAUNCHER__', None)\n\n\nCOMMANDS_NO_GIT = {'clean', 'gc', 'init-templatedir', 'sample-config'}\n\n\ndef _add_color_option(parser):\n    parser.add_argument(\n        '--color', default=os.environ.get('PRE_COMMIT_COLOR', 'auto'),\n        type=color.use_color,\n        metavar='{' + ','.join(color.COLOR_CHOICES) + '}',\n        help='Whether to use color in output.  Defaults to `%(default)s`.',\n    )\n\n\ndef _add_config_option(parser):\n    parser.add_argument(\n        '-c', '--config', default=C.CONFIG_FILE,\n        help='Path to alternate config file',\n    )\n\n\ndef _add_hook_type_option(parser):\n    parser.add_argument(\n        '-t', '--hook-type', choices=(\n            'pre-commit', 'pre-push', 'prepare-commit-msg', 'commit-msg',\n        ),\n        default='pre-commit',\n    )\n\n\ndef _add_run_options(parser):\n    parser.add_argument('hook', nargs='?', help='A single hook-id to run')\n    parser.add_argument('--verbose', '-v', action='store_true', default=False)\n    parser.add_argument(\n        '--origin', '-o',\n        help=\"The origin branch's commit_id when using `git push`.\",\n    )\n    parser.add_argument(\n        '--source', '-s',\n        help=\"The remote branch's commit_id when using `git push`.\",\n    )\n    parser.add_argument(\n        '--commit-msg-filename',\n        help='Filename to check when running during `commit-msg`',\n    )\n    parser.add_argument(\n        '--hook-stage', choices=C.STAGES, default='commit',\n        help='The stage during which the hook is fired.  One of %(choices)s',\n    )\n    parser.add_argument(\n        '--show-diff-on-failure', action='store_true',\n        help='When hooks fail, run `git diff` directly afterward.',\n    )\n    mutex_group = parser.add_mutually_exclusive_group(required=False)\n    mutex_group.add_argument(\n        '--all-files', '-a', action='store_true', default=False,\n        help='Run on all the files in the repo.',\n    )\n    mutex_group.add_argument(\n        '--files', nargs='*', default=[],\n        help='Specific filenames to run hooks on.',\n    )\n\n\ndef _adjust_args_and_chdir(args):\n    # `--config` was specified relative to the non-root working directory\n    if os.path.exists(args.config):\n        args.config = os.path.abspath(args.config)\n    if args.command in {'run', 'try-repo'}:\n        args.files = [os.path.abspath(filename) for filename in args.files]\n    if args.command == 'try-repo' and os.path.exists(args.repo):\n        args.repo = os.path.abspath(args.repo)\n\n    try:\n        os.chdir(git.get_root())\n    except CalledProcessError:\n        raise FatalError(\n            'git failed. Is it installed, and are you in a Git repository '\n            'directory?',\n        )\n\n    args.config = os.path.relpath(args.config)\n    if args.command in {'run', 'try-repo'}:\n        args.files = [os.path.relpath(filename) for filename in args.files]\n    if args.command == 'try-repo' and os.path.exists(args.repo):\n        args.repo = os.path.relpath(args.repo)\n\n\ndef main(argv=None):\n    argv = argv if argv is not None else sys.argv[1:]\n    argv = [five.to_text(arg) for arg in argv]\n    parser = argparse.ArgumentParser()\n\n    # https://stackoverflow.com/a/8521644/812183\n    parser.add_argument(\n        '-V', '--version',\n        action='version',\n        version='%(prog)s {}'.format(C.VERSION),\n    )\n\n    subparsers = parser.add_subparsers(dest='command')\n\n    autoupdate_parser = subparsers.add_parser(\n        'autoupdate',\n        help=\"Auto-update pre-commit config to the latest repos' versions.\",\n    )\n    _add_color_option(autoupdate_parser)\n    _add_config_option(autoupdate_parser)\n    autoupdate_parser.add_argument(\n        '--tags-only', action='store_true', help='LEGACY: for compatibility',\n    )\n    autoupdate_parser.add_argument(\n        '--bleeding-edge', action='store_true',\n        help=(\n            'Update to the bleeding edge of `master` instead of the latest '\n            'tagged version (the default behavior).'\n        ),\n    )\n    autoupdate_parser.add_argument(\n        '--repo', dest='repos', action='append', metavar='REPO',\n        help='Only update this repository -- may be specified multiple times.',\n    )\n\n    clean_parser = subparsers.add_parser(\n        'clean', help='Clean out pre-commit files.',\n    )\n    _add_color_option(clean_parser)\n    _add_config_option(clean_parser)\n\n    gc_parser = subparsers.add_parser('gc', help='Clean unused cached repos.')\n    _add_color_option(gc_parser)\n    _add_config_option(gc_parser)\n\n    init_templatedir_parser = subparsers.add_parser(\n        'init-templatedir',\n        help=(\n            'Install hook script in a directory intended for use with '\n            '`git config init.templateDir`.'\n        ),\n    )\n    _add_color_option(init_templatedir_parser)\n    _add_config_option(init_templatedir_parser)\n    init_templatedir_parser.add_argument(\n        'directory', help='The directory in which to write the hook script.',\n    )\n    _add_hook_type_option(init_templatedir_parser)\n\n    install_parser = subparsers.add_parser(\n        'install', help='Install the pre-commit script.',\n    )\n    _add_color_option(install_parser)\n    _add_config_option(install_parser)\n    install_parser.add_argument(\n        '-f', '--overwrite', action='store_true',\n        help='Overwrite existing hooks / remove migration mode.',\n    )\n    install_parser.add_argument(\n        '--install-hooks', action='store_true',\n        help=(\n            'Whether to install hook environments for all environments '\n            'in the config file.'\n        ),\n    )\n    _add_hook_type_option(install_parser)\n    install_parser.add_argument(\n        '--allow-missing-config', action='store_true', default=False,\n        help=(\n            'Whether to allow a missing `pre-commit` configuration file '\n            'or exit with a failure code.'\n        ),\n    )\n\n    install_hooks_parser = subparsers.add_parser(\n        'install-hooks',\n        help=(\n            'Install hook environments for all environments in the config '\n            'file.  You may find `pre-commit install --install-hooks` more '\n            'useful.'\n        ),\n    )\n    _add_color_option(install_hooks_parser)\n    _add_config_option(install_hooks_parser)\n\n    migrate_config_parser = subparsers.add_parser(\n        'migrate-config',\n        help='Migrate list configuration to new map configuration.',\n    )\n    _add_color_option(migrate_config_parser)\n    _add_config_option(migrate_config_parser)\n\n    run_parser = subparsers.add_parser('run', help='Run hooks.')\n    _add_color_option(run_parser)\n    _add_config_option(run_parser)\n    _add_run_options(run_parser)\n\n    sample_config_parser = subparsers.add_parser(\n        'sample-config', help='Produce a sample {} file'.format(C.CONFIG_FILE),\n    )\n    _add_color_option(sample_config_parser)\n    _add_config_option(sample_config_parser)\n\n    try_repo_parser = subparsers.add_parser(\n        'try-repo',\n        help='Try the hooks in a repository, useful for developing new hooks.',\n    )\n    _add_color_option(try_repo_parser)\n    _add_config_option(try_repo_parser)\n    try_repo_parser.add_argument(\n        'repo', help='Repository to source hooks from.',\n    )\n    try_repo_parser.add_argument(\n        '--ref', '--rev',\n        help=(\n            'Manually select a rev to run against, otherwise the `HEAD` '\n            'revision will be used.'\n        ),\n    )\n    _add_run_options(try_repo_parser)\n\n    uninstall_parser = subparsers.add_parser(\n        'uninstall', help='Uninstall the pre-commit script.',\n    )\n    _add_color_option(uninstall_parser)\n    _add_config_option(uninstall_parser)\n    _add_hook_type_option(uninstall_parser)\n\n    help = subparsers.add_parser(\n        'help', help='Show help for a specific command.',\n    )\n    help.add_argument('help_cmd', nargs='?', help='Command to show help for.')\n\n    # argparse doesn't really provide a way to use a `default` subparser\n    if len(argv) == 0:\n        argv = ['run']\n    args = parser.parse_args(argv)\n\n    if args.command == 'help' and args.help_cmd:\n        parser.parse_args([args.help_cmd, '--help'])\n    elif args.command == 'help':\n        parser.parse_args(['--help'])\n\n    with error_handler(), logging_handler(args.color):\n        if args.command not in COMMANDS_NO_GIT:\n            _adjust_args_and_chdir(args)\n\n        git.check_for_cygwin_mismatch()\n\n        store = Store()\n        store.mark_config_used(args.config)\n\n        if args.command == 'autoupdate':\n            if args.tags_only:\n                logger.warning('--tags-only is the default')\n            return autoupdate(\n                args.config, store,\n                tags_only=not args.bleeding_edge,\n                repos=args.repos,\n            )\n        elif args.command == 'clean':\n            return clean(store)\n        elif args.command == 'gc':\n            return gc(store)\n        elif args.command == 'install':\n            return install(\n                args.config, store,\n                overwrite=args.overwrite, hooks=args.install_hooks,\n                hook_type=args.hook_type,\n                skip_on_missing_config=args.allow_missing_config,\n            )\n        elif args.command == 'init-templatedir':\n            return init_templatedir(\n                args.config, store,\n                args.directory, hook_type=args.hook_type,\n            )\n        elif args.command == 'install-hooks':\n            return install_hooks(args.config, store)\n        elif args.command == 'migrate-config':\n            return migrate_config(args.config)\n        elif args.command == 'run':\n            return run(args.config, store, args)\n        elif args.command == 'sample-config':\n            return sample_config()\n        elif args.command == 'try-repo':\n            return try_repo(args)\n        elif args.command == 'uninstall':\n            return uninstall(hook_type=args.hook_type)\n        else:\n            raise NotImplementedError(\n                'Command {} not implemented.'.format(args.command),\n            )\n\n        raise AssertionError(\n            'Command {} failed to exit with a returncode'.format(args.command),\n        )\n\n\nif __name__ == '__main__':\n    exit(main())\n", "path": "pre_commit/main.py"}]}