Skip to content

Commit b79e64b

Browse files
[3.13] patchcheck: use URL paths to identify upstream remote (GH-135806) (#135809)
Co-authored-by: Kattni <kattni@kattni.com>
1 parent 6e0ad71 commit b79e64b

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
@@ -931,6 +931,7 @@ Anton Kasyanov
931931
Lou Kates
932932
Makoto Kato
933933
Irit Katriel
934+
Kattni
934935
Hiroaki Kawai
935936
Dmitry Kazakov
936937
Brian Kearns

Tools/patchcheck/patchcheck.py

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

6666

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

82106

83107
def get_git_remote_default_branch(remote_name):

0 commit comments

Comments
 (0)