Repository: pre-commit/pre-commit

Issue:
pre-push hook failed with "ValueError: too many values to unpack (expected 4)"
### describe your issue

I ran

```
git push --dry-run origin HEAD^"{/^[a-zA-Z]+: }":refs/for/main%wip
```

and expected the hook to run properly, but it failed with a somewhat subtle error:

```
An unexpected error has occurred: ValueError: too many values to unpack (expected 4)
Check the log at $HOME/.cache/pre-commit/pre-commit.log
```

It was more clear from the `pre-commit.log` file, though (see below). I reproduced the issue using HEAD (f9473e756decd141a9834994840c1cb124564c2a) as well.

### pre-commit --version

2.12.1

### .pre-commit-config.yaml

```yaml
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
    hooks:
      - id: check-added-large-files
      - id: check-json
      - id: check-yaml
      - id: end-of-file-fixer
      - id: trailing-whitespace

  - repo: https://github.com/wwade/pre-commit-golang
    rev: 503834f5c0933fbdf9a55e92329c1957e48f6d0a
    hooks:
      - id: go-fmt
      - id: go-imports
      - id: go-cyclo
        args: [-over=15]
      - id: validate-toml
      - id: golangci-lint
      - id: go-unit-tests
      - id: go-mod-tidy
```


### ~/.cache/pre-commit/pre-commit.log (if present)

### version information

```
pre-commit version: 2.12.1
sys.version:
    3.8.10 (default, Mar 15 2022, 12:22:08) 
    [GCC 9.4.0]
sys.executable: /usr/bin/python3
os.name: posix
sys.platform: linux
```

### error information

```
An unexpected error has occurred: ValueError: too many values to unpack (expected 4)
```

```
Traceback (most recent call last):
  File "$HOME/.local/lib/python3.8/site-packages/pre_commit/error_handler.py", line 65, in error_handler
    yield
  File "$HOME/.local/lib/python3.8/site-packages/pre_commit/main.py", line 357, in main
    return hook_impl(
  File "$HOME/.local/lib/python3.8/site-packages/pre_commit/commands/hook_impl.py", line 223, in hook_impl
    ns = _run_ns(hook_type, color, args, stdin)
  File "$HOME/.local/lib/python3.8/site-packages/pre_commit/commands/hook_impl.py", line 195, in _run_ns
    return _pre_push_ns(color, args, stdin)
  File "$HOME/.local/lib/python3.8/site-packages/pre_commit/commands/hook_impl.py", line 113, in _pre_push_ns
    _, local_sha, remote_branch, remote_sha = line.split()
ValueError: too many values to unpack (expected 4)
```


Discussion:

Patch:
diff --git a/pre_commit/commands/hook_impl.py b/pre_commit/commands/hook_impl.py
index 18e5e9f..f315c04 100644
--- a/pre_commit/commands/hook_impl.py
+++ b/pre_commit/commands/hook_impl.py
@@ -114,7 +114,8 @@ def _pre_push_ns(
     remote_url = args[1]
 
     for line in stdin.decode().splitlines():
-        local_branch, local_sha, remote_branch, remote_sha = line.split()
+        parts = line.rsplit(maxsplit=3)
+        local_branch, local_sha, remote_branch, remote_sha = parts
         if local_sha == Z40:
             continue
         elif remote_sha != Z40 and _rev_exists(remote_sha):


Test patch:
diff --git a/tests/commands/hook_impl_test.py b/tests/commands/hook_impl_test.py
index b0159f8..3e20874 100644
--- a/tests/commands/hook_impl_test.py
+++ b/tests/commands/hook_impl_test.py
@@ -242,6 +242,18 @@ def test_run_ns_pre_push_new_branch_existing_rev(push_example):
     assert ns is None
 
 
+def test_run_ns_pre_push_ref_with_whitespace(push_example):
+    src, src_head, clone, _ = push_example
+
+    with cwd(clone):
+        args = ('origin', src)
+        line = f'HEAD^{{/ }} {src_head} refs/heads/b2 {hook_impl.Z40}\n'
+        stdin = line.encode()
+        ns = hook_impl._run_ns('pre-push', False, args, stdin)
+
+    assert ns is None
+
+
 def test_pushing_orphan_branch(push_example):
     src, src_head, clone, _ = push_example