@@ -50,7 +50,8 @@ def get_git_branch():
50
50
try :
51
51
return subprocess .check_output (cmd ,
52
52
stderr = subprocess .DEVNULL ,
53
- cwd = SRCDIR )
53
+ cwd = SRCDIR ,
54
+ encoding = 'UTF-8' )
54
55
except subprocess .CalledProcessError :
55
56
return None
56
57
@@ -64,28 +65,49 @@ def get_git_upstream_remote():
64
65
try :
65
66
subprocess .check_output (cmd ,
66
67
stderr = subprocess .DEVNULL ,
67
- cwd = SRCDIR )
68
+ cwd = SRCDIR ,
69
+ encoding = 'UTF-8' )
68
70
except subprocess .CalledProcessError :
69
71
return "origin"
70
72
return "upstream"
71
73
72
74
75
+ def get_git_remote_default_branch (remote_name ):
76
+ """Get the name of the default branch for the given remote
77
+
78
+ It is typically called 'main', but may differ
79
+ """
80
+ cmd = "git remote show {}" .format (remote_name ).split ()
81
+ try :
82
+ remote_info = subprocess .check_output (cmd ,
83
+ stderr = subprocess .DEVNULL ,
84
+ cwd = SRCDIR ,
85
+ encoding = 'UTF-8' )
86
+ except subprocess .CalledProcessError :
87
+ return None
88
+ for line in remote_info .splitlines ():
89
+ if "HEAD branch:" in line :
90
+ base_branch = line .split (":" )[1 ].strip ()
91
+ return base_branch
92
+ return None
93
+
94
+
73
95
@status ("Getting base branch for PR" ,
74
96
info = lambda x : x if x is not None else "not a PR branch" )
75
97
def get_base_branch ():
76
98
if not os .path .exists (os .path .join (SRCDIR , '.git' )):
77
99
# Not a git checkout, so there's no base branch
78
100
return None
101
+ upstream_remote = get_git_upstream_remote ()
79
102
version = sys .version_info
80
103
if version .releaselevel == 'alpha' :
81
- base_branch = "master"
104
+ base_branch = get_git_remote_default_branch ( upstream_remote )
82
105
else :
83
106
base_branch = "{0.major}.{0.minor}" .format (version )
84
107
this_branch = get_git_branch ()
85
108
if this_branch is None or this_branch == base_branch :
86
109
# Not on a git PR branch, so there's no base branch
87
110
return None
88
- upstream_remote = get_git_upstream_remote ()
89
111
return upstream_remote + "/" + base_branch
90
112
91
113
0 commit comments