mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-14 21:10:27 -06:00
Make more readable --from-ref / --to-ref aliases for --source / --origin
This commit is contained in:
@@ -69,8 +69,8 @@ def _ns(
|
||||
color: bool,
|
||||
*,
|
||||
all_files: bool = False,
|
||||
origin: Optional[str] = None,
|
||||
source: Optional[str] = None,
|
||||
from_ref: Optional[str] = None,
|
||||
to_ref: Optional[str] = None,
|
||||
remote_name: Optional[str] = None,
|
||||
remote_url: Optional[str] = None,
|
||||
commit_msg_filename: Optional[str] = None,
|
||||
@@ -79,8 +79,8 @@ def _ns(
|
||||
return argparse.Namespace(
|
||||
color=color,
|
||||
hook_stage=hook_type.replace('pre-', ''),
|
||||
origin=origin,
|
||||
source=source,
|
||||
from_ref=from_ref,
|
||||
to_ref=to_ref,
|
||||
remote_name=remote_name,
|
||||
remote_url=remote_url,
|
||||
commit_msg_filename=commit_msg_filename,
|
||||
@@ -112,7 +112,7 @@ def _pre_push_ns(
|
||||
elif remote_sha != Z40 and _rev_exists(remote_sha):
|
||||
return _ns(
|
||||
'pre-push', color,
|
||||
origin=local_sha, source=remote_sha,
|
||||
from_ref=remote_sha, to_ref=local_sha,
|
||||
remote_name=remote_name, remote_url=remote_url,
|
||||
)
|
||||
else:
|
||||
@@ -139,7 +139,7 @@ def _pre_push_ns(
|
||||
source = subprocess.check_output(rev_cmd).decode().strip()
|
||||
return _ns(
|
||||
'pre-push', color,
|
||||
origin=local_sha, source=source,
|
||||
from_ref=source, to_ref=local_sha,
|
||||
remote_name=remote_name, remote_url=remote_url,
|
||||
)
|
||||
|
||||
@@ -161,8 +161,8 @@ def _run_ns(
|
||||
return _ns(hook_type, color)
|
||||
elif hook_type == 'post-checkout':
|
||||
return _ns(
|
||||
hook_type, color, source=args[0], origin=args[1],
|
||||
checkout_type=args[2],
|
||||
hook_type, color,
|
||||
from_ref=args[0], to_ref=args[1], checkout_type=args[2],
|
||||
)
|
||||
else:
|
||||
raise AssertionError(f'unexpected hook type: {hook_type}')
|
||||
|
||||
@@ -215,8 +215,8 @@ def _compute_cols(hooks: Sequence[Hook]) -> int:
|
||||
|
||||
|
||||
def _all_filenames(args: argparse.Namespace) -> Collection[str]:
|
||||
if args.origin and args.source:
|
||||
return git.get_changed_files(args.origin, args.source)
|
||||
if args.from_ref and args.to_ref:
|
||||
return git.get_changed_files(args.from_ref, args.to_ref)
|
||||
elif args.hook_stage in {'prepare-commit-msg', 'commit-msg'}:
|
||||
return (args.commit_msg_filename,)
|
||||
elif args.files:
|
||||
@@ -297,8 +297,8 @@ def run(
|
||||
if _has_unmerged_paths():
|
||||
logger.error('Unmerged files. Resolve before committing.')
|
||||
return 1
|
||||
if bool(args.source) != bool(args.origin):
|
||||
logger.error('Specify both --origin and --source.')
|
||||
if bool(args.from_ref) != bool(args.to_ref):
|
||||
logger.error('Specify both --from-ref and --to-ref.')
|
||||
return 1
|
||||
if stash and _has_unstaged_config(config_file):
|
||||
logger.error(
|
||||
@@ -316,10 +316,14 @@ def run(
|
||||
)
|
||||
return 1
|
||||
|
||||
# Expose origin / source as environment variables for hooks to consume
|
||||
if args.origin and args.source:
|
||||
environ['PRE_COMMIT_ORIGIN'] = args.origin
|
||||
environ['PRE_COMMIT_SOURCE'] = args.source
|
||||
# Expose from-ref / to-ref as environment variables for hooks to consume
|
||||
if args.from_ref and args.to_ref:
|
||||
# legacy names
|
||||
environ['PRE_COMMIT_ORIGIN'] = args.from_ref
|
||||
environ['PRE_COMMIT_SOURCE'] = args.to_ref
|
||||
# new names
|
||||
environ['PRE_COMMIT_FROM_REF'] = args.from_ref
|
||||
environ['PRE_COMMIT_TO_REF'] = args.to_ref
|
||||
|
||||
if args.remote_name and args.remote_url:
|
||||
environ['PRE_COMMIT_REMOTE_NAME'] = args.remote_name
|
||||
|
||||
@@ -129,7 +129,7 @@ def get_all_files() -> List[str]:
|
||||
return zsplit(cmd_output('git', 'ls-files', '-z')[1])
|
||||
|
||||
|
||||
def get_changed_files(new: str, old: str) -> List[str]:
|
||||
def get_changed_files(old: str, new: str) -> List[str]:
|
||||
return zsplit(
|
||||
cmd_output(
|
||||
'git', 'diff', '--name-only', '--no-ext-diff', '-z',
|
||||
|
||||
@@ -90,18 +90,42 @@ def _add_hook_type_option(parser: argparse.ArgumentParser) -> None:
|
||||
def _add_run_options(parser: argparse.ArgumentParser) -> None:
|
||||
parser.add_argument('hook', nargs='?', help='A single hook-id to run')
|
||||
parser.add_argument('--verbose', '-v', action='store_true', default=False)
|
||||
mutex_group = parser.add_mutually_exclusive_group(required=False)
|
||||
mutex_group.add_argument(
|
||||
'--all-files', '-a', action='store_true', default=False,
|
||||
help='Run on all the files in the repo.',
|
||||
)
|
||||
mutex_group.add_argument(
|
||||
'--files', nargs='*', default=[],
|
||||
help='Specific filenames to run hooks on.',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--origin', '-o',
|
||||
'--show-diff-on-failure', action='store_true',
|
||||
help='When hooks fail, run `git diff` directly afterward.',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--hook-stage', choices=C.STAGES, default='commit',
|
||||
help='The stage during which the hook is fired. One of %(choices)s',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--from-ref', '--source', '-s',
|
||||
help=(
|
||||
"The origin branch's commit_id when using `git push`. "
|
||||
'The ref of the previous HEAD when using `git checkout`.'
|
||||
'(for usage with `--from-ref`) -- this option represents the '
|
||||
'destination ref in a `from_ref...to_ref` diff expression. '
|
||||
'For `pre-push` hooks, this represents the branch being pushed. '
|
||||
'For `post-checkout` hooks, this represents the branch that is '
|
||||
'now checked out.'
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
'--source', '-s',
|
||||
'--to-ref', '--origin', '-o',
|
||||
help=(
|
||||
"The remote branch's commit_id when using `git push`. "
|
||||
'The ref of the new HEAD when using `git checkout`.'
|
||||
'(for usage with `--to-ref`) -- this option represents the '
|
||||
'original ref in a `from_ref...to_ref` diff expression. '
|
||||
'For `pre-push` hooks, this represents the branch you are pushing '
|
||||
'to. '
|
||||
'For `post-checkout` hooks, this represents the branch which was '
|
||||
'previously checked out.'
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
@@ -112,23 +136,6 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
|
||||
'--remote-name', help='Remote name used by `git push`.',
|
||||
)
|
||||
parser.add_argument('--remote-url', help='Remote url used by `git push`.')
|
||||
parser.add_argument(
|
||||
'--hook-stage', choices=C.STAGES, default='commit',
|
||||
help='The stage during which the hook is fired. One of %(choices)s',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--show-diff-on-failure', action='store_true',
|
||||
help='When hooks fail, run `git diff` directly afterward.',
|
||||
)
|
||||
mutex_group = parser.add_mutually_exclusive_group(required=False)
|
||||
mutex_group.add_argument(
|
||||
'--all-files', '-a', action='store_true', default=False,
|
||||
help='Run on all the files in the repo.',
|
||||
)
|
||||
mutex_group.add_argument(
|
||||
'--files', nargs='*', default=[],
|
||||
help='Specific filenames to run hooks on.',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--checkout-type',
|
||||
help=(
|
||||
|
||||
Reference in New Issue
Block a user