Skip to content

Commit cb5ed62

Browse files
surafelabebeasottile
authored andcommitted
Expose remote branch ref as an environment variable
1 parent bb50e00 commit cb5ed62

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

pre_commit/commands/hook_impl.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def _ns(
6969
color: bool,
7070
*,
7171
all_files: bool = False,
72+
remote_branch: Optional[str] = None,
7273
from_ref: Optional[str] = None,
7374
to_ref: Optional[str] = None,
7475
remote_name: Optional[str] = None,
@@ -79,6 +80,7 @@ def _ns(
7980
return argparse.Namespace(
8081
color=color,
8182
hook_stage=hook_type.replace('pre-', ''),
83+
remote_branch=remote_branch,
8284
from_ref=from_ref,
8385
to_ref=to_ref,
8486
remote_name=remote_name,
@@ -106,13 +108,14 @@ def _pre_push_ns(
106108
remote_url = args[1]
107109

108110
for line in stdin.decode().splitlines():
109-
_, local_sha, _, remote_sha = line.split()
111+
_, local_sha, remote_branch, remote_sha = line.split()
110112
if local_sha == Z40:
111113
continue
112114
elif remote_sha != Z40 and _rev_exists(remote_sha):
113115
return _ns(
114116
'pre-push', color,
115117
from_ref=remote_sha, to_ref=local_sha,
118+
remote_branch=remote_branch,
116119
remote_name=remote_name, remote_url=remote_url,
117120
)
118121
else:
@@ -133,6 +136,7 @@ def _pre_push_ns(
133136
'pre-push', color,
134137
all_files=True,
135138
remote_name=remote_name, remote_url=remote_url,
139+
remote_branch=remote_branch,
136140
)
137141
else:
138142
rev_cmd = ('git', 'rev-parse', f'{first_ancestor}^')
@@ -141,6 +145,7 @@ def _pre_push_ns(
141145
'pre-push', color,
142146
from_ref=source, to_ref=local_sha,
143147
remote_name=remote_name, remote_url=remote_url,
148+
remote_branch=remote_branch,
144149
)
145150

146151
# nothing to push

pre_commit/commands/run.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ def run(
371371
environ['PRE_COMMIT_FROM_REF'] = args.from_ref
372372
environ['PRE_COMMIT_TO_REF'] = args.to_ref
373373

374-
if args.remote_name and args.remote_url:
374+
if args.remote_name and args.remote_url and args.remote_branch:
375+
environ['PRE_COMMIT_REMOTE_BRANCH'] = args.remote_branch
375376
environ['PRE_COMMIT_REMOTE_NAME'] = args.remote_name
376377
environ['PRE_COMMIT_REMOTE_URL'] = args.remote_url
377378

pre_commit/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def _add_run_options(parser: argparse.ArgumentParser) -> None:
9696
'--hook-stage', choices=C.STAGES, default='commit',
9797
help='The stage during which the hook is fired. One of %(choices)s',
9898
)
99+
parser.add_argument(
100+
'--remote-branch', help='Remote branch ref used by `git push`.',
101+
)
99102
parser.add_argument(
100103
'--from-ref', '--source', '-s',
101104
help=(

testing/util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def run_opts(
6161
color=False,
6262
verbose=False,
6363
hook=None,
64+
remote_branch='',
6465
from_ref='',
6566
to_ref='',
6667
remote_name='',
@@ -78,6 +79,7 @@ def run_opts(
7879
color=color,
7980
verbose=verbose,
8081
hook=hook,
82+
remote_branch=remote_branch,
8183
from_ref=from_ref,
8284
to_ref=to_ref,
8385
remote_name=remote_name,

tests/commands/run_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ def test_from_ref_to_ref_error_msg_error(
486486
def test_all_push_options_ok(cap_out, store, repo_with_passing_hook):
487487
args = run_opts(
488488
from_ref='master', to_ref='master',
489+
remote_branch='master',
489490
remote_name='origin', remote_url='https://example.com/repo',
490491
)
491492
ret, printed = _do_run(cap_out, store, repo_with_passing_hook, args)

0 commit comments

Comments
 (0)