From 65b1c3028f00a8c82cc3a8aa72b7c84034d92403 Mon Sep 17 00:00:00 2001 From: Leonardo Lai Date: Sat, 8 May 2021 15:14:36 +0200 Subject: [PATCH 1/3] Let patchcheck infer the base branch name --- Tools/scripts/patchcheck.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/Tools/scripts/patchcheck.py b/Tools/scripts/patchcheck.py index 8a8480a0c22b37..4ae264253a93db 100755 --- a/Tools/scripts/patchcheck.py +++ b/Tools/scripts/patchcheck.py @@ -50,7 +50,8 @@ def get_git_branch(): try: return subprocess.check_output(cmd, stderr=subprocess.DEVNULL, - cwd=SRCDIR) + cwd=SRCDIR, + encoding='UTF-8') except subprocess.CalledProcessError: return None @@ -64,28 +65,46 @@ def get_git_upstream_remote(): try: subprocess.check_output(cmd, stderr=subprocess.DEVNULL, - cwd=SRCDIR) + cwd=SRCDIR, + encoding='UTF-8') except subprocess.CalledProcessError: return "origin" return "upstream" +def get_git_remote_default_branch(remote_name): + """Get the name of the default branch for the given remote + + It is typically called 'main', but may differ + """ + cmd = "git rev-parse --abbrev-ref {}".format(remote_name).split() + try: + full_name = subprocess.check_output(cmd, + stderr=subprocess.DEVNULL, + cwd=SRCDIR, + encoding='UTF-8') + base_branch = full_name.split("/")[1] + return base_branch + except subprocess.CalledProcessError: + return None + + @status("Getting base branch for PR", info=lambda x: x if x is not None else "not a PR branch") def get_base_branch(): if not os.path.exists(os.path.join(SRCDIR, '.git')): # Not a git checkout, so there's no base branch return None + upstream_remote = get_git_upstream_remote() version = sys.version_info if version.releaselevel == 'alpha': - base_branch = "master" + base_branch = get_git_remote_default_branch(upstream_remote) else: base_branch = "{0.major}.{0.minor}".format(version) this_branch = get_git_branch() if this_branch is None or this_branch == base_branch: # Not on a git PR branch, so there's no base branch return None - upstream_remote = get_git_upstream_remote() return upstream_remote + "/" + base_branch From 10d3c6f98f128eb4149b7016bb27371845ad3ad7 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 8 May 2021 13:57:01 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Tools-Demos/2021-05-08-13-57-00.bpo-44074.F09kCK.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tools-Demos/2021-05-08-13-57-00.bpo-44074.F09kCK.rst diff --git a/Misc/NEWS.d/next/Tools-Demos/2021-05-08-13-57-00.bpo-44074.F09kCK.rst b/Misc/NEWS.d/next/Tools-Demos/2021-05-08-13-57-00.bpo-44074.F09kCK.rst new file mode 100644 index 00000000000000..8bccb080a54186 --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2021-05-08-13-57-00.bpo-44074.F09kCK.rst @@ -0,0 +1 @@ +Make patchcheck automatically detect the correct base branch name (previously it was hardcoded to 'master') \ No newline at end of file From 51909610300ad1b9314afb099058f68a063e60ae Mon Sep 17 00:00:00 2001 From: Leonardo Lai Date: Sun, 9 May 2021 10:09:32 +0200 Subject: [PATCH 3/3] parse the default branch name with git remote --- Tools/scripts/patchcheck.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Tools/scripts/patchcheck.py b/Tools/scripts/patchcheck.py index 4ae264253a93db..d9cceb5d5acdfd 100755 --- a/Tools/scripts/patchcheck.py +++ b/Tools/scripts/patchcheck.py @@ -77,16 +77,19 @@ def get_git_remote_default_branch(remote_name): It is typically called 'main', but may differ """ - cmd = "git rev-parse --abbrev-ref {}".format(remote_name).split() + cmd = "git remote show {}".format(remote_name).split() try: - full_name = subprocess.check_output(cmd, - stderr=subprocess.DEVNULL, - cwd=SRCDIR, - encoding='UTF-8') - base_branch = full_name.split("/")[1] - return base_branch + remote_info = subprocess.check_output(cmd, + stderr=subprocess.DEVNULL, + cwd=SRCDIR, + encoding='UTF-8') except subprocess.CalledProcessError: return None + for line in remote_info.splitlines(): + if "HEAD branch:" in line: + base_branch = line.split(":")[1].strip() + return base_branch + return None @status("Getting base branch for PR",