Repository: pre-commit/pre-commit

Issue:
Failure to check out revision pre-commit v1.15.1
```console
$ cat .pre-commit-config.yaml 
repos:
-   repo: https://github.com/asottile/pyupgrade
    rev: v1.13.0
    hooks:
    -   id: pyupgrade
$ pre-commit install-hooks
[INFO] Initializing environment for https://github.com/asottile/pyupgrade.
An unexpected error has occurred: CalledProcessError: Command: ('/usr/bin/git', 'checkout', 'v1.13.0')
Return code: 1
Expected return code: 0
Output: (none)
Errors: 
    error: pathspec 'v1.13.0' did not match any file(s) known to git.
    

Check the log at /code/.cache/pre-commit/pre-commit.log
lyftsvc@b780c05acb2e:/code/places$ cat ~/.cacp^C
lyftsvc@b780c05acb2e:/code/places$ cat /code/.cache/pre-commit/pre-commit.log 
An unexpected error has occurred: CalledProcessError: Command: ('/usr/bin/git', 'checkout', 'v1.13.0')
Return code: 1
Expected return code: 0
Output: (none)
Errors: 
    error: pathspec 'v1.13.0' did not match any file(s) known to git.
    

Traceback (most recent call last):
  File "/venv/lib/python3.6/site-packages/pre_commit/store.py", line 172, in clone_strategy
    self._shallow_clone(ref, _git_cmd)
  File "/venv/lib/python3.6/site-packages/pre_commit/store.py", line 149, in _shallow_clone
    git_cmd('-c', git_config, 'fetch', 'origin', ref, '--depth=1')
  File "/venv/lib/python3.6/site-packages/pre_commit/store.py", line 166, in _git_cmd
    cmd_output('git', *args, cwd=directory, env=env)
  File "/venv/lib/python3.6/site-packages/pre_commit/util.py", line 153, in cmd_output
    returncode, cmd, retcode, output=(stdout, stderr),
pre_commit.util.CalledProcessError: Command: ('/usr/bin/git', '-c', 'protocol.version=2', 'fetch', 'origin', 'v1.13.0', '--depth=1')
Return code: 128
Expected return code: 0
Output: (none)
Errors: 
    fatal: unknown value for config 'protocol.version': 2
    


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/venv/lib/python3.6/site-packages/pre_commit/error_handler.py", line 46, in error_handler
    yield
  File "/venv/lib/python3.6/site-packages/pre_commit/main.py", line 274, in main
    return install_hooks(args.config, store)
  File "/venv/lib/python3.6/site-packages/pre_commit/commands/install_uninstall.py", line 129, in install_hooks
    install_hook_envs(all_hooks(load_config(config_file), store), store)
  File "/venv/lib/python3.6/site-packages/pre_commit/repository.py", line 221, in all_hooks
    for repo in root_config['repos']
  File "/venv/lib/python3.6/site-packages/pre_commit/repository.py", line 222, in <genexpr>
    for hook in _repository_hooks(repo, store, root_config)
  File "/venv/lib/python3.6/site-packages/pre_commit/repository.py", line 197, in _repository_hooks
    return _cloned_repository_hooks(repo_config, store, root_config)
  File "/venv/lib/python3.6/site-packages/pre_commit/repository.py", line 166, in _cloned_repository_hooks
    manifest_path = os.path.join(store.clone(repo, rev), C.MANIFEST_FILE)
  File "/venv/lib/python3.6/site-packages/pre_commit/store.py", line 176, in clone
    return self._new_repo(repo, ref, deps, clone_strategy)
  File "/venv/lib/python3.6/site-packages/pre_commit/store.py", line 128, in _new_repo
    make_strategy(directory)
  File "/venv/lib/python3.6/site-packages/pre_commit/store.py", line 174, in clone_strategy
    self._complete_clone(ref, _git_cmd)
  File "/venv/lib/python3.6/site-packages/pre_commit/store.py", line 142, in _complete_clone
    git_cmd('checkout', ref)
  File "/venv/lib/python3.6/site-packages/pre_commit/store.py", line 166, in _git_cmd
    cmd_output('git', *args, cwd=directory, env=env)
  File "/venv/lib/python3.6/site-packages/pre_commit/util.py", line 153, in cmd_output
    returncode, cmd, retcode, output=(stdout, stderr),
pre_commit.util.CalledProcessError: Command: ('/usr/bin/git', 'checkout', 'v1.13.0')
Return code: 1
Expected return code: 0
Output: (none)
Errors: 
    error: pathspec 'v1.13.0' did not match any file(s) known to git.
```

Perhaps related to the new clone strategy


Discussion:
I just ~partially fixed this, but not the root cause -- v1.13.0 was tagged, but not on the mainline (I retagged the correct commit there)

the full-clone is not fetching all tags, the fix is to add `--tags` to the `fetch` line
CC @DanielChabrowski just fyi I misled you :laughing: 

Resolving patch:
diff --git a/pre_commit/store.py b/pre_commit/store.py
--- a/pre_commit/store.py
+++ b/pre_commit/store.py
@@ -138,7 +138,7 @@ def _get_result():
     def _complete_clone(self, ref, git_cmd):
         """Perform a complete clone of a repository and its submodules """
 
-        git_cmd('fetch', 'origin')
+        git_cmd('fetch', 'origin', '--tags')
         git_cmd('checkout', ref)
         git_cmd('submodule', 'update', '--init', '--recursive')
 


Test patch:
diff --git a/tests/store_test.py b/tests/store_test.py
--- a/tests/store_test.py
+++ b/tests/store_test.py
@@ -13,6 +13,7 @@
 from pre_commit.store import _get_default_directory
 from pre_commit.store import Store
 from pre_commit.util import CalledProcessError
+from pre_commit.util import cmd_output
 from testing.fixtures import git_dir
 from testing.util import cwd
 from testing.util import git_commit
@@ -147,6 +148,20 @@ def fake_shallow_clone(self, *args, **kwargs):
     assert store.select_all_repos() == [(path, rev, ret)]
 
 
+def test_clone_tag_not_on_mainline(store, tempdir_factory):
+    path = git_dir(tempdir_factory)
+    with cwd(path):
+        git_commit()
+        cmd_output('git', 'checkout', 'master', '-b', 'branch')
+        git_commit()
+        cmd_output('git', 'tag', 'v1')
+        cmd_output('git', 'checkout', 'master')
+        cmd_output('git', 'branch', '-D', 'branch')
+
+    # previously crashed on unreachable refs
+    store.clone(path, 'v1')
+
+
 def test_create_when_directory_exists_but_not_db(store):
     # In versions <= 0.3.5, there was no sqlite db causing a need for
     # backward compatibility