2025-02-09 21:33:11 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-09 21:10:22 +01:00
|
|
|
import contextlib
|
|
|
|
import os.path
|
|
|
|
import subprocess
|
2025-02-09 21:40:02 +01:00
|
|
|
import sys
|
2025-02-09 21:10:22 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pre_commit.util import cmd_output
|
|
|
|
from testing.auto_namedtuple import auto_namedtuple
|
|
|
|
|
|
|
|
|
|
|
|
TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
|
|
|
|
def get_resource_path(path):
|
|
|
|
return os.path.join(TESTING_DIR, 'resources', path)
|
|
|
|
|
|
|
|
|
|
|
|
def cmd_output_mocked_pre_commit_home(
|
|
|
|
*args, tempdir_factory, pre_commit_home=None, env=None, **kwargs,
|
|
|
|
):
|
|
|
|
if pre_commit_home is None:
|
|
|
|
pre_commit_home = tempdir_factory.get()
|
|
|
|
env = env if env is not None else os.environ
|
|
|
|
kwargs.setdefault('stderr', subprocess.STDOUT)
|
|
|
|
# Don't want to write to the home directory
|
|
|
|
env = dict(env, PRE_COMMIT_HOME=pre_commit_home)
|
|
|
|
ret, out, _ = cmd_output(*args, env=env, **kwargs)
|
|
|
|
return ret, out.replace('\r\n', '\n'), None
|
|
|
|
|
|
|
|
|
2025-02-09 21:40:02 +01:00
|
|
|
xfailif_windows = pytest.mark.xfail(sys.platform == 'win32', reason='windows')
|
2025-02-09 21:10:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def run_opts(
|
|
|
|
all_files=False,
|
|
|
|
files=(),
|
|
|
|
color=False,
|
|
|
|
verbose=False,
|
|
|
|
hook=None,
|
2025-02-09 21:23:50 +01:00
|
|
|
remote_branch='',
|
2025-02-09 21:29:18 +01:00
|
|
|
local_branch='',
|
2025-02-09 21:10:22 +01:00
|
|
|
from_ref='',
|
|
|
|
to_ref='',
|
2025-02-09 21:40:02 +01:00
|
|
|
pre_rebase_upstream='',
|
|
|
|
pre_rebase_branch='',
|
2025-02-09 21:10:22 +01:00
|
|
|
remote_name='',
|
|
|
|
remote_url='',
|
2025-02-09 21:40:02 +01:00
|
|
|
hook_stage='pre-commit',
|
2025-02-09 21:10:22 +01:00
|
|
|
show_diff_on_failure=False,
|
|
|
|
commit_msg_filename='',
|
2025-02-09 21:34:34 +01:00
|
|
|
prepare_commit_message_source='',
|
|
|
|
commit_object_name='',
|
2025-02-09 21:10:22 +01:00
|
|
|
checkout_type='',
|
2025-02-09 21:25:27 +01:00
|
|
|
is_squash_merge='',
|
2025-02-09 21:30:55 +01:00
|
|
|
rewrite_command='',
|
2025-02-09 21:10:22 +01:00
|
|
|
):
|
|
|
|
# These are mutually exclusive
|
|
|
|
assert not (all_files and files)
|
|
|
|
return auto_namedtuple(
|
|
|
|
all_files=all_files,
|
|
|
|
files=files,
|
|
|
|
color=color,
|
|
|
|
verbose=verbose,
|
|
|
|
hook=hook,
|
2025-02-09 21:23:50 +01:00
|
|
|
remote_branch=remote_branch,
|
2025-02-09 21:29:18 +01:00
|
|
|
local_branch=local_branch,
|
2025-02-09 21:10:22 +01:00
|
|
|
from_ref=from_ref,
|
|
|
|
to_ref=to_ref,
|
2025-02-09 21:40:02 +01:00
|
|
|
pre_rebase_upstream=pre_rebase_upstream,
|
|
|
|
pre_rebase_branch=pre_rebase_branch,
|
2025-02-09 21:10:22 +01:00
|
|
|
remote_name=remote_name,
|
|
|
|
remote_url=remote_url,
|
|
|
|
hook_stage=hook_stage,
|
|
|
|
show_diff_on_failure=show_diff_on_failure,
|
|
|
|
commit_msg_filename=commit_msg_filename,
|
2025-02-09 21:34:34 +01:00
|
|
|
prepare_commit_message_source=prepare_commit_message_source,
|
|
|
|
commit_object_name=commit_object_name,
|
2025-02-09 21:10:22 +01:00
|
|
|
checkout_type=checkout_type,
|
2025-02-09 21:25:27 +01:00
|
|
|
is_squash_merge=is_squash_merge,
|
2025-02-09 21:30:55 +01:00
|
|
|
rewrite_command=rewrite_command,
|
2025-02-09 21:10:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def cwd(path):
|
|
|
|
original_cwd = os.getcwd()
|
|
|
|
os.chdir(path)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(original_cwd)
|
|
|
|
|
|
|
|
|
2025-02-09 21:20:28 +01:00
|
|
|
def git_commit(*args, fn=cmd_output, msg='commit!', all_files=True, **kwargs):
|
2025-02-09 21:10:22 +01:00
|
|
|
kwargs.setdefault('stderr', subprocess.STDOUT)
|
|
|
|
|
2025-02-09 21:20:28 +01:00
|
|
|
cmd = ('git', 'commit', '--allow-empty', '--no-gpg-sign', *args)
|
|
|
|
if all_files: # allow skipping `-a` with `all_files=False`
|
|
|
|
cmd += ('-a',)
|
2025-02-09 21:10:22 +01:00
|
|
|
if msg is not None: # allow skipping `-m` with `msg=None`
|
|
|
|
cmd += ('-m', msg)
|
|
|
|
ret, out, _ = fn(*cmd, **kwargs)
|
|
|
|
return ret, out.replace('\r\n', '\n')
|