[3.13] patchcheck: use URL paths to identify upstream remote (GH-135806) (#135809)

Co-authored-by: Kattni <kattni@kattni.com>
This commit is contained in:
Miss Islington (bot) 2025-06-23 11:57:03 +02:00 committed by GitHub
parent 6e0ad71a29
commit b79e64b67d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 12 deletions

View file

@ -931,6 +931,7 @@ Anton Kasyanov
Lou Kates Lou Kates
Makoto Kato Makoto Kato
Irit Katriel Irit Katriel
Kattni
Hiroaki Kawai Hiroaki Kawai
Dmitry Kazakov Dmitry Kazakov
Brian Kearns Brian Kearns

View file

@ -65,19 +65,43 @@ def get_git_branch():
def get_git_upstream_remote(): def get_git_upstream_remote():
"""Get the remote name to use for upstream branches
Uses "upstream" if it exists, "origin" otherwise
""" """
cmd = "git remote get-url upstream".split() Get the remote name to use for upstream branches
try:
subprocess.check_output(cmd, Check for presence of "https://github.com/python/cpython" remote URL.
stderr=subprocess.DEVNULL, If only one is found, return that remote name. If multiple are found,
cwd=SRCDIR, check for and return "upstream", "origin", or "python", in that
encoding='UTF-8') order. Raise an error if no valid matches are found.
except subprocess.CalledProcessError: """
return "origin" cmd = "git remote -v".split()
return "upstream" output = subprocess.check_output(
cmd,
stderr=subprocess.DEVNULL,
cwd=SRCDIR,
encoding="UTF-8"
)
# Filter to desired remotes, accounting for potential uppercasing
filtered_remotes = {
remote.split("\t")[0].lower() for remote in output.split('\n')
if "python/cpython" in remote.lower() and remote.endswith("(fetch)")
}
if len(filtered_remotes) == 1:
[remote] = filtered_remotes
return remote
for remote_name in ["upstream", "origin", "python"]:
if remote_name in filtered_remotes:
return remote_name
remotes_found = "\n".join(
{remote for remote in output.split('\n') if remote.endswith("(fetch)")}
)
raise ValueError(
f"Patchcheck was unable to find an unambiguous upstream remote, "
f"with URL matching 'https://github.com/python/cpython'. "
f"For help creating an upstream remote, see Dev Guide: "
f"https://devguide.python.org/getting-started/"
f"git-boot-camp/#cloning-a-forked-cpython-repository "
f"\nRemotes found: \n{remotes_found}"
)
def get_git_remote_default_branch(remote_name): def get_git_remote_default_branch(remote_name):