Merge pull request #1947 from FlorentClarret/pre_push_local_branch

Expose local branch ref as an environment variable
This commit is contained in:
Anthony Sottile 2021-06-19 11:46:17 -07:00 committed by GitHub
commit ba5e6eb42d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 2 deletions

View file

@ -70,6 +70,7 @@ def _ns(
*, *,
all_files: bool = False, all_files: bool = False,
remote_branch: Optional[str] = None, remote_branch: Optional[str] = None,
local_branch: Optional[str] = None,
from_ref: Optional[str] = None, from_ref: Optional[str] = None,
to_ref: Optional[str] = None, to_ref: Optional[str] = None,
remote_name: Optional[str] = None, remote_name: Optional[str] = None,
@ -82,6 +83,7 @@ def _ns(
color=color, color=color,
hook_stage=hook_type.replace('pre-', ''), hook_stage=hook_type.replace('pre-', ''),
remote_branch=remote_branch, remote_branch=remote_branch,
local_branch=local_branch,
from_ref=from_ref, from_ref=from_ref,
to_ref=to_ref, to_ref=to_ref,
remote_name=remote_name, remote_name=remote_name,
@ -110,7 +112,7 @@ def _pre_push_ns(
remote_url = args[1] remote_url = args[1]
for line in stdin.decode().splitlines(): for line in stdin.decode().splitlines():
_, local_sha, remote_branch, remote_sha = line.split() local_branch, local_sha, remote_branch, remote_sha = line.split()
if local_sha == Z40: if local_sha == Z40:
continue continue
elif remote_sha != Z40 and _rev_exists(remote_sha): elif remote_sha != Z40 and _rev_exists(remote_sha):
@ -118,6 +120,7 @@ def _pre_push_ns(
'pre-push', color, 'pre-push', color,
from_ref=remote_sha, to_ref=local_sha, from_ref=remote_sha, to_ref=local_sha,
remote_branch=remote_branch, remote_branch=remote_branch,
local_branch=local_branch,
remote_name=remote_name, remote_url=remote_url, remote_name=remote_name, remote_url=remote_url,
) )
else: else:
@ -139,6 +142,7 @@ def _pre_push_ns(
all_files=True, all_files=True,
remote_name=remote_name, remote_url=remote_url, remote_name=remote_name, remote_url=remote_url,
remote_branch=remote_branch, remote_branch=remote_branch,
local_branch=local_branch,
) )
else: else:
rev_cmd = ('git', 'rev-parse', f'{first_ancestor}^') rev_cmd = ('git', 'rev-parse', f'{first_ancestor}^')
@ -148,6 +152,7 @@ def _pre_push_ns(
from_ref=source, to_ref=local_sha, from_ref=source, to_ref=local_sha,
remote_name=remote_name, remote_url=remote_url, remote_name=remote_name, remote_url=remote_url,
remote_branch=remote_branch, remote_branch=remote_branch,
local_branch=local_branch,
) )
# nothing to push # nothing to push

View file

@ -371,7 +371,11 @@ def run(
environ['PRE_COMMIT_FROM_REF'] = args.from_ref environ['PRE_COMMIT_FROM_REF'] = args.from_ref
environ['PRE_COMMIT_TO_REF'] = args.to_ref environ['PRE_COMMIT_TO_REF'] = args.to_ref
if args.remote_name and args.remote_url and args.remote_branch: if (
args.remote_name and args.remote_url and
args.remote_branch and args.local_branch
):
environ['PRE_COMMIT_LOCAL_BRANCH'] = args.local_branch
environ['PRE_COMMIT_REMOTE_BRANCH'] = args.remote_branch environ['PRE_COMMIT_REMOTE_BRANCH'] = args.remote_branch
environ['PRE_COMMIT_REMOTE_NAME'] = args.remote_name environ['PRE_COMMIT_REMOTE_NAME'] = args.remote_name
environ['PRE_COMMIT_REMOTE_URL'] = args.remote_url environ['PRE_COMMIT_REMOTE_URL'] = args.remote_url

View file

@ -99,6 +99,9 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
parser.add_argument( parser.add_argument(
'--remote-branch', help='Remote branch ref used by `git push`.', '--remote-branch', help='Remote branch ref used by `git push`.',
) )
parser.add_argument(
'--local-branch', help='Local branch ref used by `git push`.',
)
parser.add_argument( parser.add_argument(
'--from-ref', '--source', '-s', '--from-ref', '--source', '-s',
help=( help=(

View file

@ -62,6 +62,7 @@ def run_opts(
verbose=False, verbose=False,
hook=None, hook=None,
remote_branch='', remote_branch='',
local_branch='',
from_ref='', from_ref='',
to_ref='', to_ref='',
remote_name='', remote_name='',
@ -81,6 +82,7 @@ def run_opts(
verbose=verbose, verbose=verbose,
hook=hook, hook=hook,
remote_branch=remote_branch, remote_branch=remote_branch,
local_branch=local_branch,
from_ref=from_ref, from_ref=from_ref,
to_ref=to_ref, to_ref=to_ref,
remote_name=remote_name, remote_name=remote_name,

View file

@ -487,6 +487,7 @@ def test_all_push_options_ok(cap_out, store, repo_with_passing_hook):
args = run_opts( args = run_opts(
from_ref='master', to_ref='master', from_ref='master', to_ref='master',
remote_branch='master', remote_branch='master',
local_branch='master',
remote_name='origin', remote_url='https://example.com/repo', remote_name='origin', remote_url='https://example.com/repo',
) )
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args) ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)