Skip to content

stubsabot: only update existing branches if the diff relative to master would be different #8854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions scripts/stubsabot.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,22 @@ def has_non_stubsabot_commits(branch: str) -> bool:
return False


def latest_commit_is_different_to_last_commit_on_origin(branch: str) -> bool:
assert not branch.startswith("origin/")
try:
# https://www.git-scm.com/docs/git-range-diff
# If the number of lines is >1,
# it indicates that something about our commit is different to the last commit
# (Could be the commit "content", or the commit message).
commit_comparison = subprocess.run(
["git", "range-diff", f"origin/{branch}~1..origin/{branch}", "HEAD~1..HEAD"], check=True, capture_output=True
)
return len(commit_comparison.stdout.splitlines()) > 1
except subprocess.CalledProcessError:
# origin/branch does not exist
return True


class RemoteConflict(Exception):
pass

Expand Down Expand Up @@ -588,6 +604,9 @@ async def suggest_typeshed_update(update: Update, session: aiohttp.ClientSession
subprocess.check_call(["git", "commit", "--all", "-m", f"{title}\n\n{body}"])
if action_level <= ActionLevel.local:
return
if not latest_commit_is_different_to_last_commit_on_origin(branch_name):
print(f"No pushing to origin required: origin/{branch_name} exists and requires no changes!")
return
somewhat_safe_force_push(branch_name)
if action_level <= ActionLevel.fork:
return
Expand All @@ -613,6 +632,9 @@ async def suggest_typeshed_obsolete(obsolete: Obsolete, session: aiohttp.ClientS
subprocess.check_call(["git", "commit", "--all", "-m", f"{title}\n\n{body}"])
if action_level <= ActionLevel.local:
return
if not latest_commit_is_different_to_last_commit_on_origin(branch_name):
print(f"No PR required: origin/{branch_name} exists and requires no changes!")
return
somewhat_safe_force_push(branch_name)
if action_level <= ActionLevel.fork:
return
Expand Down