Modified: trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/checkout.py (293513 => 293514)
--- trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/checkout.py 2022-04-27 17:06:09 UTC (rev 293513)
+++ trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/checkout.py 2022-04-27 17:12:06 UTC (rev 293514)
@@ -50,6 +50,7 @@
url=""
sentinal=obj.sentinal,
remotes=obj.remotes,
+ forwarding=obj.forwarding,
credentials=obj.credentials,
)
if obj.fallback_repository:
@@ -104,11 +105,17 @@
with open(os.path.expanduser('~/.git-credentials'), 'w') as f:
f.write(git_credentials_content)
- def __init__(self, path, url="" http_proxy=None, sentinal=True, fallback_url=None, primary=True, remotes=None, credentials=None):
+ def __init__(
+ self, path, url="" http_proxy=None,
+ sentinal=True, fallback_url=None, primary=True,
+ remotes=None, credentials=None,
+ forwarding=None,
+ ):
self.sentinal = sentinal
self.path = path
self.url = ""
self.remotes = remotes or dict()
+ self.forwarding = forwarding or [('origin', list(self.remotes.keys()))]
self.credentials = credentials or dict()
self._repository = None
self._child_process = None
@@ -204,8 +211,8 @@
return ref == line.split()[0]
return False
- def push_update(self, branch=None, tag=None, remote=None, track=False):
- if not remote or remote in (self.REMOTE, 'fork'):
+ def push_update(self, branch=None, tag=None, remote=None, track=False, dest_branch=None):
+ if not remote or remote in ('fork',):
return False
if tag:
@@ -219,10 +226,26 @@
return False
return not run(
- [self.repository.executable(), 'push', remote, branch, '-f'],
+ [self.repository.executable(), 'push', remote, '{}:{}'.format(branch, dest_branch or branch)],
cwd=self.repository.root_path,
).returncode
+ def forward_update(self, branch=None, tag=None, remote=None, track=False):
+ for match, tos in self.forwarding:
+ if match not in (remote, '{}:{}'.format(remote, branch or tag)):
+ continue
+ for to in tos:
+ split = to.split(':', 1)
+ self.push_update(
+ branch=branch,
+ tag=tag,
+ remote=split[0],
+ track=track,
+ dest_branch=split[1] if len(split) > 1 else None,
+ )
+ return
+ return
+
def fetch(self, remote=REMOTE):
return not run(
[self.repository.executable(), 'fetch', remote],
@@ -273,7 +296,7 @@
if branch in all_branches:
all_branches.remove(branch)
self.update_for(branch=branch, remote=remote)
- [self.push_update(branch=branch, remote=remote) for remote in self.remotes.keys()]
+ self.forward_update(branch=branch, remote=remote)
# Then, track all untracked branches
print('Tracking new branches...')
@@ -282,7 +305,7 @@
[self.repository.executable(), 'branch', '--track', branch, 'remotes/{}/{}'.format(remote, branch)],
cwd=self.repository.root_path,
)
- [self.push_update(branch=branch, remote=remote) for remote in self.remotes.keys()]
+ self.forward_update(branch=branch, remote=remote)
self.repository.cache.populate(branch=branch)
# Sync all tags
@@ -295,4 +318,4 @@
remote_tags = set(self.repository.tags(remote=target_remote))
for tag in origin_tags - remote_tags:
print(f' {tag}')
- self.push_update(tag=tag, remote=target_remote)
+ self.forward_update(tag=tag, remote=remote)
Modified: trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/hooks.py (293513 => 293514)
--- trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/hooks.py 2022-04-27 17:06:09 UTC (rev 293513)
+++ trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/hooks.py 2022-04-27 17:12:06 UTC (rev 293514)
@@ -23,6 +23,7 @@
import hashlib
import hmac
import json
+import re
import sys
from flask import current_app, json as fjson, request
@@ -39,6 +40,7 @@
TYPES = ('pull_request', 'push')
BRANCH_PREFIX = 'refs/heads/'
TAG_PREFIX = 'refs/tags/'
+ REMOTES_RE = re.compile(r'remote\.(?P<remote>\S+)\.url')
@classmethod
def is_valid(cls, type, data):
@@ -68,17 +70,27 @@
return None
branch = data.get('ref')
+ remote = ''
+ name = data.get('repository', {}).get('url', '').split('://')[-1]
+ if name and self.checkout.repository:
+ for config_arg, url in self.checkout.repository.config().items():
+ match = self.REMOTES_RE.match(config_arg)
+ if not match or '{}.git'.format(name) not in [url.split('@')[-1], url.split('://')[-1]]:
+ continue
+ remote = match.group('remote')
+ break
+
if type == 'push' and branch:
try:
if branch.startswith(self.BRANCH_PREFIX):
branch = branch[len(self.BRANCH_PREFIX):]
self.checkout.update_for(branch, track=True)
- [self.checkout.push_update(branch=branch, remote=remote, track=True) for remote in self.checkout.remotes.keys()]
+ self.checkout.forward_update(branch=branch, remote=remote, track=True)
if branch.startswith(self.TAG_PREFIX):
tag = branch[len(self.TAG_PREFIX):]
self.checkout.fetch()
- [self.checkout.push_update(tag=tag, remote=remote) for remote in self.checkout.remotes.keys()]
+ self.checkout.forward_update(tag=tag, remote=remote)
except BaseException as e:
sys.stderr.write('{}\n'.format(e))
Modified: trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/tests/checkout_unittest.py (293513 => 293514)
--- trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/tests/checkout_unittest.py 2022-04-27 17:06:09 UTC (rev 293513)
+++ trunk/Tools/Scripts/libraries/reporelaypy/reporelaypy/tests/checkout_unittest.py 2022-04-27 17:12:06 UTC (rev 293514)
@@ -1,4 +1,4 @@
-# Copyright (C) 2021 Apple Inc. All rights reserved.
+# Copyright (C) 2021-2022 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
@@ -54,6 +54,7 @@
path=self.path,
url=""
remotes={},
+ forwarding=[('origin', [])],
credentials={
'https://github.com': dict(
username='username',