@@ -65,19 +65,43 @@ def get_git_branch():
65
65
66
66
67
67
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
69
70
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.
71
75
"""
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"\n Remotes found: \n { remotes_found } "
104
+ )
81
105
82
106
83
107
def get_git_remote_default_branch (remote_name ):
0 commit comments