Repository: pre-commit/pre-commit

Issue:
Crash on `local`-only `golang` repositories
While investigating: https://github.com/pre-commit/pre-commit-hooks/issues/255

Using this configuration:

```yaml
repos:
-   repo: local
    hooks:
    -   id: talisman
        name: talisman
        entry: talisman -githook pre-commit
        pass_filenames: false
        types: [text]
        language: golang
        additional_dependencies: [github.com/thoughtworks/talisman]
```

```
$ pre-commit run --all-files
[INFO] Initializing environment for local:github.com/thoughtworks/talisman.
[INFO] Installing environment for local.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: Command: ('/usr/bin/git', 'config', 'remote.origin.url')
Return code: 1
Expected return code: 0
Output: (none)
Errors: (none)

Check the log at /home/asottile/.cache/pre-commit/pre-commit.log
```

```
An unexpected error has occurred: CalledProcessError: Command: ('/usr/bin/git', 'config', 'remote.origin.url')
Return code: 1
Expected return code: 0
Output: (none)
Errors: (none)

Traceback (most recent call last):
  File "/tmp/wat/venv/local/lib/python2.7/site-packages/pre_commit/error_handler.py", line 47, in error_handler
    yield
  File "/tmp/wat/venv/local/lib/python2.7/site-packages/pre_commit/main.py", line 259, in main
    return run(runner, args)
  File "/tmp/wat/venv/local/lib/python2.7/site-packages/pre_commit/commands/run.py", line 256, in run
    repo.require_installed()
  File "/tmp/wat/venv/local/lib/python2.7/site-packages/pre_commit/repository.py", line 202, in require_installed
    _install_all(self._venvs, self.repo_config['repo'], self.store)
  File "/tmp/wat/venv/local/lib/python2.7/site-packages/pre_commit/repository.py", line 102, in _install_all
    language.install_environment(cmd_runner, version, deps)
  File "/tmp/wat/venv/local/lib/python2.7/site-packages/pre_commit/languages/golang.py", line 60, in install_environment
    remote = git.get_remote_url(repo_cmd_runner.path())
  File "/tmp/wat/venv/local/lib/python2.7/site-packages/pre_commit/git.py", line 41, in get_remote_url
    ret = cmd_output('git', 'config', 'remote.origin.url', cwd=git_root)[1]
  File "/tmp/wat/venv/local/lib/python2.7/site-packages/pre_commit/util.py", line 188, in cmd_output
    returncode, cmd, retcode, output=(stdout, stderr),
CalledProcessError: Command: ('/usr/bin/git', 'config', 'remote.origin.url')
Return code: 1
Expected return code: 0
Output: (none)
Errors: (none)
```

Discussion:

Patch:
diff --git a/pre_commit/commands/run.py b/pre_commit/commands/run.py
index c70eff0..3a08c8d 100644
--- a/pre_commit/commands/run.py
+++ b/pre_commit/commands/run.py
@@ -223,8 +223,8 @@ def run(runner, args, environ=os.environ):
         return 1
     if _has_unstaged_config(runner) and not no_stash:
         logger.error(
-            'Your pre-commit configuration is unstaged.\n'
-            '`git add {}` to fix this.'.format(runner.config_file),
+            'Your .pre-commit-config.yaml is unstaged.\n'
+            '`git add .pre-commit-config.yaml` to fix this.',
         )
         return 1
 
diff --git a/pre_commit/main.py b/pre_commit/main.py
index 865571a..4c9202a 100644
--- a/pre_commit/main.py
+++ b/pre_commit/main.py
@@ -42,7 +42,7 @@ def _add_color_option(parser):
 
 def _add_config_option(parser):
     parser.add_argument(
-        '-c', '--config', default=C.CONFIG_FILE,
+        '-c', '--config', default='.pre-commit-config.yaml',
         help='Path to alternate config file',
     )
 
diff --git a/pre_commit/repository.py b/pre_commit/repository.py
index 5c11921..bc0ecad 100644
--- a/pre_commit/repository.py
+++ b/pre_commit/repository.py
@@ -269,14 +269,14 @@ class MetaRepository(LocalRepository):
             {
                 'id': 'check-hooks-apply',
                 'name': 'Check hooks apply to the repository',
-                'files': C.CONFIG_FILE,
+                'files': '.pre-commit-config.yaml',
                 'language': 'system',
                 'entry': _make_entry(check_hooks_apply),
             },
             {
                 'id': 'check-useless-excludes',
                 'name': 'Check for useless excludes',
-                'files': C.CONFIG_FILE,
+                'files': '.pre-commit-config.yaml',
                 'language': 'system',
                 'entry': _make_entry(check_useless_excludes),
             },
diff --git a/pre_commit/store.py b/pre_commit/store.py
index 3262bda..9c67345 100644
--- a/pre_commit/store.py
+++ b/pre_commit/store.py
@@ -156,6 +156,21 @@ class Store(object):
     def make_local(self, deps):
         def make_local_strategy(directory):
             copy_tree_to_path(resource_filename('empty_template'), directory)
+
+            env = no_git_env()
+            name, email = 'pre-commit', 'asottile+pre-commit@umich.edu'
+            env['GIT_AUTHOR_NAME'] = env['GIT_COMMITTER_NAME'] = name
+            env['GIT_AUTHOR_EMAIL'] = env['GIT_COMMITTER_EMAIL'] = email
+
+            # initialize the git repository so it looks more like cloned repos
+            def _git_cmd(*args):
+                cmd_output('git', '-C', directory, *args, env=env)
+
+            _git_cmd('init', '.')
+            _git_cmd('config', 'remote.origin.url', '<<unknown>>')
+            _git_cmd('add', '.')
+            _git_cmd('commit', '--no-edit', '--no-gpg-sign', '-n', '-minit')
+
         return self._new_repo(
             'local:{}'.format(','.join(sorted(deps))), C.LOCAL_REPO_VERSION,
             make_local_strategy,


Test patch:
diff --git a/testing/fixtures.py b/testing/fixtures.py
index edb1bcd..b1c7a89 100644
--- a/testing/fixtures.py
+++ b/testing/fixtures.py
@@ -47,7 +47,7 @@ def modify_manifest(path):
     with io.open(manifest_path, 'w') as manifest_file:
         manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
     cmd_output(
-        'git', 'commit', '-am', 'update {}'.format(C.MANIFEST_FILE), cwd=path,
+        'git', 'commit', '-am', 'update .pre-commit-hooks.yaml', cwd=path,
     )
 
 
diff --git a/tests/commands/run_test.py b/tests/commands/run_test.py
index 97c82c2..336222d 100644
--- a/tests/commands/run_test.py
+++ b/tests/commands/run_test.py
@@ -683,7 +683,7 @@ def test_error_with_unstaged_config(
 ):
     args = run_opts()
     ret, printed = _do_run(cap_out, modified_config_repo, args)
-    assert b'Your pre-commit configuration is unstaged.' in printed
+    assert b'Your .pre-commit-config.yaml is unstaged.' in printed
     assert ret == 1
 
 
@@ -695,7 +695,7 @@ def test_no_unstaged_error_with_all_files_or_files(
 ):
     args = run_opts(**opts)
     ret, printed = _do_run(cap_out, modified_config_repo, args)
-    assert b'Your pre-commit configuration is unstaged.' not in printed
+    assert b'Your .pre-commit-config.yaml is unstaged.' not in printed
 
 
 def test_files_running_subdir(
diff --git a/tests/repository_test.py b/tests/repository_test.py
index 2b9ab6e..1d38d24 100644
--- a/tests/repository_test.py
+++ b/tests/repository_test.py
@@ -541,6 +541,24 @@ def test_additional_golang_dependencies_installed(
     assert 'hello' in binaries
 
 
+def test_local_golang_additional_dependencies(store):
+    config = {
+        'repo': 'local',
+        'hooks': [{
+            'id': 'hello',
+            'name': 'hello',
+            'entry': 'hello',
+            'language': 'golang',
+            'additional_dependencies': ['github.com/golang/example/hello'],
+        }],
+    }
+    repo = Repository.create(config, store)
+    (_, hook), = repo.hooks
+    ret = repo.run_hook(hook, ('filename',))
+    assert ret[0] == 0
+    assert _norm_out(ret[1]) == b"Hello, Go examples!\n"
+
+
 def test_reinstall(tempdir_factory, store, log_info_mock):
     path = make_repo(tempdir_factory, 'python_hooks_repo')
     config = make_config_from_repo(path)