Make more readable --from-ref / --to-ref aliases for --source / --origin

This commit is contained in:
Anthony Sottile
2020-02-23 11:07:57 -08:00
parent 566f1afcd4
commit d35b00352f
8 changed files with 72 additions and 61 deletions

View File

@@ -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}')

View File

@@ -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