MCPcopy Index your code
hub / github.com/pre-commit/pre-commit / _log_and_exit

Function _log_and_exit

pre_commit/error_handler.py:19–67  ·  view source on GitHub ↗
(
    msg: str,
    ret_code: int,
    exc: BaseException,
    formatted: str,
)

Source from the content-addressed store, hash-verified

17
18
19def _log_and_exit(
20 msg: str,
21 ret_code: int,
22 exc: BaseException,
23 formatted: str,
24) -> None:
25 error_msg = f'{msg}: {type(exc).__name__}: '.encode() + force_bytes(exc)
26 output.write_line_b(error_msg)
27
28 _, git_version_b, _ = cmd_output_b('git', '--version', check=False)
29 git_version = git_version_b.decode(errors='backslashreplace').rstrip()
30
31 storedir = Store().directory
32 log_path = os.path.join(storedir, 'pre-commit.log')
33 with contextlib.ExitStack() as ctx:
34 if os.access(storedir, os.W_OK):
35 output.write_line(f'Check the log at {log_path}')
36 log: IO[bytes] = ctx.enter_context(open(log_path, 'wb'))
37 else: # pragma: win32 no cover
38 output.write_line(f'Failed to write to log at {log_path}')
39 log = sys.stdout.buffer
40
41 _log_line = functools.partial(output.write_line, stream=log)
42 _log_line_b = functools.partial(output.write_line_b, stream=log)
43
44 _log_line('### version information')
45 _log_line()
46 _log_line('```')
47 _log_line(f'pre-commit version: {C.VERSION}')
48 _log_line(f'git --version: {git_version}')
49 _log_line('sys.version:')
50 for line in sys.version.splitlines():
51 _log_line(f' {line}')
52 _log_line(f'sys.executable: {sys.executable}')
53 _log_line(f'os.name: {os.name}')
54 _log_line(f'sys.platform: {sys.platform}')
55 _log_line('```')
56 _log_line()
57
58 _log_line('### error information')
59 _log_line()
60 _log_line('```')
61 _log_line_b(error_msg)
62 _log_line('```')
63 _log_line()
64 _log_line('```')
65 _log_line(formatted.rstrip())
66 _log_line('```')
67 raise SystemExit(ret_code)
68
69
70@contextlib.contextmanager

Callers 1

error_handlerFunction · 0.85

Calls 3

force_bytesFunction · 0.90
cmd_output_bFunction · 0.90
StoreClass · 0.90

Tested by

no test coverage detected