Skip to content

Commit

Permalink
devtools: pr-tool warns if remote not uptodate
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arogge authored and pstorz committed Jan 20, 2023
1 parent 252e25d commit 894e237
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions devtools/pip-tools/pr_tool/main.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 894e237

Please sign in to comment.