|
| 1 | +import argparse |
| 2 | +import os.path |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +from typing import Optional |
| 6 | +from typing import Sequence |
| 7 | +from typing import Tuple |
| 8 | + |
| 9 | +from pre_commit.commands.run import run |
| 10 | +from pre_commit.envcontext import envcontext |
| 11 | +from pre_commit.parse_shebang import normalize_cmd |
| 12 | +from pre_commit.store import Store |
| 13 | + |
| 14 | +Z40 = '0' * 40 |
| 15 | + |
| 16 | + |
| 17 | +def _run_legacy( |
| 18 | + hook_type: str, |
| 19 | + hook_dir: str, |
| 20 | + args: Sequence[str], |
| 21 | +) -> Tuple[int, bytes]: |
| 22 | + if os.environ.get('PRE_COMMIT_RUNNING_LEGACY'): |
| 23 | + raise SystemExit( |
| 24 | + f"bug: pre-commit's script is installed in migration mode\n" |
| 25 | + f'run `pre-commit install -f --hook-type {hook_type}` to fix ' |
| 26 | + f'this\n\n' |
| 27 | + f'Please report this bug at ' |
| 28 | + f'https://github.com/pre-commit/pre-commit/issues', |
| 29 | + ) |
| 30 | + |
| 31 | + if hook_type == 'pre-push': |
| 32 | + stdin = sys.stdin.buffer.read() |
| 33 | + else: |
| 34 | + stdin = b'' |
| 35 | + |
| 36 | + # not running in legacy mode |
| 37 | + legacy_hook = os.path.join(hook_dir, f'{hook_type}.legacy') |
| 38 | + if not os.access(legacy_hook, os.X_OK): |
| 39 | + return 0, stdin |
| 40 | + |
| 41 | + with envcontext((('PRE_COMMIT_RUNNING_LEGACY', '1'),)): |
| 42 | + cmd = normalize_cmd((legacy_hook, *args)) |
| 43 | + return subprocess.run(cmd, input=stdin).returncode, stdin |
| 44 | + |
| 45 | + |
| 46 | +def _validate_config( |
| 47 | + retv: int, |
| 48 | + config: str, |
| 49 | + skip_on_missing_config: bool, |
| 50 | +) -> None: |
| 51 | + if not os.path.isfile(config): |
| 52 | + if skip_on_missing_config or os.getenv('PRE_COMMIT_ALLOW_NO_CONFIG'): |
| 53 | + print(f'`{config}` config file not found. Skipping `pre-commit`.') |
| 54 | + raise SystemExit(retv) |
| 55 | + else: |
| 56 | + print( |
| 57 | + f'No {config} file was found\n' |
| 58 | + f'- To temporarily silence this, run ' |
| 59 | + f'`PRE_COMMIT_ALLOW_NO_CONFIG=1 git ...`\n' |
| 60 | + f'- To permanently silence this, install pre-commit with the ' |
| 61 | + f'--allow-missing-config option\n' |
| 62 | + f'- To uninstall pre-commit run `pre-commit uninstall`', |
| 63 | + ) |
| 64 | + raise SystemExit(1) |
| 65 | + |
| 66 | + |
| 67 | +def _ns( |
| 68 | + hook_type: str, |
| 69 | + color: bool, |
| 70 | + *, |
| 71 | + all_files: bool = False, |
| 72 | + origin: Optional[str] = None, |
| 73 | + source: Optional[str] = None, |
| 74 | + remote_name: Optional[str] = None, |
| 75 | + remote_url: Optional[str] = None, |
| 76 | + commit_msg_filename: Optional[str] = None, |
| 77 | +) -> argparse.Namespace: |
| 78 | + return argparse.Namespace( |
| 79 | + color=color, |
| 80 | + hook_stage=hook_type.replace('pre-', ''), |
| 81 | + origin=origin, |
| 82 | + source=source, |
| 83 | + remote_name=remote_name, |
| 84 | + remote_url=remote_url, |
| 85 | + commit_msg_filename=commit_msg_filename, |
| 86 | + all_files=all_files, |
| 87 | + files=(), |
| 88 | + hook=None, |
| 89 | + verbose=False, |
| 90 | + show_diff_on_failure=False, |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def _rev_exists(rev: str) -> bool: |
| 95 | + return not subprocess.call(('git', 'rev-list', '--quiet', rev)) |
| 96 | + |
| 97 | + |
| 98 | +def _pre_push_ns( |
| 99 | + color: bool, |
| 100 | + args: Sequence[str], |
| 101 | + stdin: bytes, |
| 102 | +) -> Optional[argparse.Namespace]: |
| 103 | + remote_name = args[0] |
| 104 | + remote_url = args[1] |
| 105 | + |
| 106 | + for line in stdin.decode().splitlines(): |
| 107 | + _, local_sha, _, remote_sha = line.split() |
| 108 | + if local_sha == Z40: |
| 109 | + continue |
| 110 | + elif remote_sha != Z40 and _rev_exists(remote_sha): |
| 111 | + return _ns( |
| 112 | + 'pre-push', color, |
| 113 | + origin=local_sha, source=remote_sha, |
| 114 | + remote_name=remote_name, remote_url=remote_url, |
| 115 | + ) |
| 116 | + else: |
| 117 | + # ancestors not found in remote |
| 118 | + ancestors = subprocess.check_output(( |
| 119 | + 'git', 'rev-list', local_sha, '--topo-order', '--reverse', |
| 120 | + '--not', f'--remotes={remote_name}', |
| 121 | + )).decode().strip() |
| 122 | + if not ancestors: |
| 123 | + continue |
| 124 | + else: |
| 125 | + first_ancestor = ancestors.splitlines()[0] |
| 126 | + cmd = ('git', 'rev-list', '--max-parents=0', local_sha) |
| 127 | + roots = set(subprocess.check_output(cmd).decode().splitlines()) |
| 128 | + if first_ancestor in roots: |
| 129 | + # pushing the whole tree including root commit |
| 130 | + return _ns( |
| 131 | + 'pre-push', color, |
| 132 | + all_files=True, |
| 133 | + remote_name=remote_name, remote_url=remote_url, |
| 134 | + ) |
| 135 | + else: |
| 136 | + rev_cmd = ('git', 'rev-parse', f'{first_ancestor}^') |
| 137 | + source = subprocess.check_output(rev_cmd).decode().strip() |
| 138 | + return _ns( |
| 139 | + 'pre-push', color, |
| 140 | + origin=local_sha, source=source, |
| 141 | + remote_name=remote_name, remote_url=remote_url, |
| 142 | + ) |
| 143 | + |
| 144 | + # nothing to push |
| 145 | + return None |
| 146 | + |
| 147 | + |
| 148 | +def _run_ns( |
| 149 | + hook_type: str, |
| 150 | + color: bool, |
| 151 | + args: Sequence[str], |
| 152 | + stdin: bytes, |
| 153 | +) -> Optional[argparse.Namespace]: |
| 154 | + if hook_type == 'pre-push': |
| 155 | + return _pre_push_ns(color, args, stdin) |
| 156 | + elif hook_type in {'prepare-commit-msg', 'commit-msg'}: |
| 157 | + return _ns(hook_type, color, commit_msg_filename=args[0]) |
| 158 | + elif hook_type in {'pre-merge-commit', 'pre-commit'}: |
| 159 | + return _ns(hook_type, color) |
| 160 | + else: |
| 161 | + raise AssertionError(f'unexpected hook type: {hook_type}') |
| 162 | + |
| 163 | + |
| 164 | +def hook_impl( |
| 165 | + store: Store, |
| 166 | + *, |
| 167 | + config: str, |
| 168 | + color: bool, |
| 169 | + hook_type: str, |
| 170 | + hook_dir: str, |
| 171 | + skip_on_missing_config: bool, |
| 172 | + args: Sequence[str], |
| 173 | +) -> int: |
| 174 | + retv, stdin = _run_legacy(hook_type, hook_dir, args) |
| 175 | + _validate_config(retv, config, skip_on_missing_config) |
| 176 | + ns = _run_ns(hook_type, color, args, stdin) |
| 177 | + if ns is None: |
| 178 | + return retv |
| 179 | + else: |
| 180 | + return retv | run(config, store, ns) |
0 commit comments