Skip to content

Commit 4cc8369

Browse files
bpo-44074: let patchcheck infer the base branch name (GH-25991)
(cherry picked from commit 21fbbb9) Co-authored-by: Leonardo Lai <[email protected]>
1 parent f47305a commit 4cc8369

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make patchcheck automatically detect the correct base branch name (previously it was hardcoded to 'master')

Tools/scripts/patchcheck.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def get_git_branch():
5050
try:
5151
return subprocess.check_output(cmd,
5252
stderr=subprocess.DEVNULL,
53-
cwd=SRCDIR)
53+
cwd=SRCDIR,
54+
encoding='UTF-8')
5455
except subprocess.CalledProcessError:
5556
return None
5657

@@ -64,28 +65,49 @@ def get_git_upstream_remote():
6465
try:
6566
subprocess.check_output(cmd,
6667
stderr=subprocess.DEVNULL,
67-
cwd=SRCDIR)
68+
cwd=SRCDIR,
69+
encoding='UTF-8')
6870
except subprocess.CalledProcessError:
6971
return "origin"
7072
return "upstream"
7173

7274

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+
7395
@status("Getting base branch for PR",
7496
info=lambda x: x if x is not None else "not a PR branch")
7597
def get_base_branch():
7698
if not os.path.exists(os.path.join(SRCDIR, '.git')):
7799
# Not a git checkout, so there's no base branch
78100
return None
101+
upstream_remote = get_git_upstream_remote()
79102
version = sys.version_info
80103
if version.releaselevel == 'alpha':
81-
base_branch = "master"
104+
base_branch = get_git_remote_default_branch(upstream_remote)
82105
else:
83106
base_branch = "{0.major}.{0.minor}".format(version)
84107
this_branch = get_git_branch()
85108
if this_branch is None or this_branch == base_branch:
86109
# Not on a git PR branch, so there's no base branch
87110
return None
88-
upstream_remote = get_git_upstream_remote()
89111
return upstream_remote + "/" + base_branch
90112

91113

0 commit comments

Comments
 (0)