feat(run): warn instead of error when --files is used outside a git repo

When `pre-commit run --files <files>` is called outside a git
repository, the tool previously raised a FatalError and exited
with code 1.

- Print a warning that pre-commit is running outside a git
  repository and only the explicitly passed files will be checked
- Continue execution rather than aborting
- All other commands (and `run` without `--files`) retain the
  existing fatal error behavior

This modification helps use pre-commit in jujutsu worktrees as long
as the modified file list is supplied on command line.

Signed-off-by: Venkateswara Rao Mandela <venkat.mandela@gmail.com>
This commit is contained in:
Venkateswara Rao Mandela 2026-04-05 14:48:46 +05:30
parent 129a1f5ca1
commit ac1301417b

View file

@ -26,6 +26,7 @@ from pre_commit.commands.try_repo import try_repo
from pre_commit.commands.validate_config import validate_config
from pre_commit.commands.validate_manifest import validate_manifest
from pre_commit.error_handler import error_handler
from pre_commit.errors import FatalError
from pre_commit.logging_handler import logging_handler
from pre_commit.store import Store
@ -185,7 +186,16 @@ def _adjust_args_and_chdir(args: argparse.Namespace) -> None:
if args.command == 'try-repo' and os.path.exists(args.repo):
args.repo = os.path.abspath(args.repo)
try:
toplevel = git.get_root()
except FatalError:
if args.command == 'run' and args.files:
logger.warning(
'pre-commit is running outside a git repository. '
'Only the files passed via `--files` will be checked.',
)
return
raise
os.chdir(toplevel)
args.config = os.path.relpath(args.config)