From 894e237af228044eb737c189e7c9b1d02bd4f0a9 Mon Sep 17 00:00:00 2001 From: Andreas Rogge Date: Tue, 20 Dec 2022 10:29:01 +0100 Subject: [PATCH] devtools: pr-tool warns if remote not uptodate When your local repository is not up to date, rebasing (and potentially other operations) will not lead to the desired result or may even fail. pr-tool will now check if your local repository's base branch points to the same commit as the remote repository's base branch and will issue a warning otherwise. --- devtools/pip-tools/pr_tool/main.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/devtools/pip-tools/pr_tool/main.py b/devtools/pip-tools/pr_tool/main.py index 3942c5f34cf..4f1510ae94b 100644 --- a/devtools/pip-tools/pr_tool/main.py +++ b/devtools/pip-tools/pr_tool/main.py @@ -116,7 +116,7 @@ def __call__(self, *args, **kwargs): if res.returncode != 0: raise InvokationError(res) - if "json" in kwargs: + if "json" in kwargs or len(self.cmd) >= 2 and self.cmd[1] == "api": return json.loads(res.stdout) return res.stdout @@ -441,7 +441,7 @@ def merge_pr( ) else: - base_branch = "{}/{}".format(pr["_git_remote"], pr["baseRefName"]) + base_branch = pr["_base_branch"] print( "Resetting to {} and rebasing onto {}".format( original_commit.hexsha, base_branch @@ -490,6 +490,7 @@ def handle_ret(ret): def get_current_pr_data(): + logging.debug("Getting pull request data from GitHub") return Gh().pr.view( json=[ "body", @@ -511,6 +512,20 @@ def get_current_pr_data(): ) +def get_remote_ref(branch, owner="{owner}", repo="{repo}"): + return Gh().api( + "repos/{owner}/{repo}/git/refs/heads/{branch}".format( + owner=owner, repo=repo, branch=branch + ) + ) + + +def repo_up_to_date(repo, pr_data, remote_ref): + local_head = repo.commit(pr_data["_base_branch"]) + remote_head = remote_ref["object"]["sha"] + return local_head != remote_head + + def setup_logging(*, verbose, debug): logging.basicConfig(format="%(levelname)s: %(name)s: %(message)s", stream=stderr) @@ -567,7 +582,16 @@ def main(): logging.info("Using git remote '{}'".format(git_remote)) pr_data = get_current_pr_data() - pr_data["_git_remote"] = git_remote + pr_data["_base_branch"] = "{}/{}".format(git_remote, pr_data["baseRefName"]) + + remote_ref = get_remote_ref(pr_data["baseRefName"]) + + if not repo_up_to_date(repo, pr_data, remote_ref): + logging.warning( + "You repository is out of date. Please run 'git fetch {}'".format( + git_remote + ) + ) if args.subcommand == "check": ret = check_merge_prereq(repo, pr_data)