@@ -53,19 +53,43 @@ def get_git_branch():
53
53
54
54
55
55
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
57
58
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.
59
63
"""
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"\n Remotes found: \n { remotes_found } "
92
+ )
69
93
70
94
71
95
def get_git_remote_default_branch (remote_name ):
0 commit comments