Skip to content

Commit ac9d37c

Browse files
authored
patchcheck: use URL paths to identify upstream remote (GH-135806)
* find defined "(fetch)" remotes with "python/cpython" in their URL * if there is exactly one, use that remote name * if there is one named "upstream", "origin", or "python", use that remote (in that precedence order) * otherwise report an error listing the defined remotes
1 parent b14986c commit ac9d37c

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ Anton Kasyanov
943943
Lou Kates
944944
Makoto Kato
945945
Irit Katriel
946+
Kattni
946947
Hiroaki Kawai
947948
Dmitry Kazakov
948949
Brian Kearns

Tools/patchcheck/patchcheck.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,43 @@ def get_git_branch():
5353

5454

5555
def get_git_upstream_remote():
56-
"""Get the remote name to use for upstream branches
56+
"""
57+
Get the remote name to use for upstream branches
5758
58-
Uses "upstream" if it exists, "origin" otherwise
59+
Check for presence of "https://github.com/python/cpython" remote URL.
60+
If only one is found, return that remote name. If multiple are found,
61+
check for and return "upstream", "origin", or "python", in that
62+
order. Raise an error if no valid matches are found.
5963
"""
60-
cmd = "git remote get-url upstream".split()
61-
try:
62-
subprocess.check_output(cmd,
63-
stderr=subprocess.DEVNULL,
64-
cwd=SRCDIR,
65-
encoding='UTF-8')
66-
except subprocess.CalledProcessError:
67-
return "origin"
68-
return "upstream"
64+
cmd = "git remote -v".split()
65+
output = subprocess.check_output(
66+
cmd,
67+
stderr=subprocess.DEVNULL,
68+
cwd=SRCDIR,
69+
encoding="UTF-8"
70+
)
71+
# Filter to desired remotes, accounting for potential uppercasing
72+
filtered_remotes = {
73+
remote.split("\t")[0].lower() for remote in output.split('\n')
74+
if "python/cpython" in remote.lower() and remote.endswith("(fetch)")
75+
}
76+
if len(filtered_remotes) == 1:
77+
[remote] = filtered_remotes
78+
return remote
79+
for remote_name in ["upstream", "origin", "python"]:
80+
if remote_name in filtered_remotes:
81+
return remote_name
82+
remotes_found = "\n".join(
83+
{remote for remote in output.split('\n') if remote.endswith("(fetch)")}
84+
)
85+
raise ValueError(
86+
f"Patchcheck was unable to find an unambiguous upstream remote, "
87+
f"with URL matching 'https://github.com/python/cpython'. "
88+
f"For help creating an upstream remote, see Dev Guide: "
89+
f"https://devguide.python.org/getting-started/"
90+
f"git-boot-camp/#cloning-a-forked-cpython-repository "
91+
f"\nRemotes found: \n{remotes_found}"
92+
)
6993

7094

7195
def get_git_remote_default_branch(remote_name):

0 commit comments

Comments
 (0)