Skip to content

Commit

Permalink
feat: Import diff-release.py script from the legacy common repo (#472)
Browse files Browse the repository at this point in the history
* You may check the backport status of PRs by running it like:
  `python scripts/diff-release.py main 22.03`
  • Loading branch information
achimnol committed Jun 14, 2022
1 parent fe047c6 commit b5d3565
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/472.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `scripts/diff-release.py` to check backport status of pull requests
77 changes: 77 additions & 0 deletions scripts/diff-release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import subprocess
import sys
import re


rx_oneline = re.compile(r"^(?P<commit>[a-f0-9]{40}) (?P<msg>.*?)(\(#(?P<pr>\d+)\))?$")


def do_diff_release(ref_old, ref_new):

# To ensure latest information from the repository, first fetch all branches
# and use the "origin" refspec prefix.
subprocess.run(['git', 'fetch'], capture_output=True)
if not ref_old.startswith('origin/'):
ref_old = 'origin/' + ref_old
if not ref_new.startswith('origin/'):
ref_new = 'origin/' + ref_new

proc = subprocess.run([
'git', 'merge-base', ref_old, ref_new,
], capture_output=True)
common_ancestor = proc.stdout.strip().decode()

proc = subprocess.run([
'git', 'rev-list', '--pretty=oneline', f'{common_ancestor}..{ref_old}',
], capture_output=True)
old_rev_list = proc.stdout.decode().splitlines()

proc = subprocess.run([
'git', 'rev-list', '--pretty=oneline', f'{common_ancestor}..{ref_new}',
], capture_output=True)
new_rev_list = proc.stdout.decode().splitlines()

pr_desc_map = {}

old_pr_set = set()
for rev in old_rev_list:
if (
(m := rx_oneline.search(rev.strip())) is not None and
(pr := m.group('pr')) is not None
):
old_pr_set.add(int(pr))
pr_desc_map[int(pr)] = m.group('msg')

new_pr_set = set()
for rev in new_rev_list:
if (
(m := rx_oneline.search(rev.strip())) is not None and
(pr := m.group('pr')) is not None
):
new_pr_set.add(int(pr))
pr_desc_map[int(pr)] = m.group('msg')

print()
print(f"The common ancestor commit:\n{common_ancestor}")

print()
print(f"All PRs in the {ref_new} branch since {common_ancestor[:12]}:")
print(" " + ", ".join(map(str, sorted(new_pr_set))))

print()
print(f"All PRs in the {ref_old} branch since {common_ancestor[:12]}:")
print(" " + ", ".join(map(str, sorted(old_pr_set))))

print()
print(f"PRs only in the {ref_new} branch:")
for pr in sorted(new_pr_set - old_pr_set, reverse=True):
print(f" {pr} {pr_desc_map[pr]}")

print()
print(f"PRs only in the {ref_old} branch:")
for pr in sorted(old_pr_set - new_pr_set, reverse=True):
print(f" {pr} {pr_desc_map[pr]}")


if __name__ == "__main__":
do_diff_release(sys.argv[1], sys.argv[2])

0 comments on commit b5d3565

Please sign in to comment.