mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-14 17:41:45 +04:00
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:
parent
129a1f5ca1
commit
ac1301417b
1 changed files with 11 additions and 1 deletions
|
|
@ -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_config import validate_config
|
||||||
from pre_commit.commands.validate_manifest import validate_manifest
|
from pre_commit.commands.validate_manifest import validate_manifest
|
||||||
from pre_commit.error_handler import error_handler
|
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.logging_handler import logging_handler
|
||||||
from pre_commit.store import Store
|
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):
|
if args.command == 'try-repo' and os.path.exists(args.repo):
|
||||||
args.repo = os.path.abspath(args.repo)
|
args.repo = os.path.abspath(args.repo)
|
||||||
|
|
||||||
toplevel = git.get_root()
|
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)
|
os.chdir(toplevel)
|
||||||
|
|
||||||
args.config = os.path.relpath(args.config)
|
args.config = os.path.relpath(args.config)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue