|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import re |
| 6 | +import git |
| 7 | + |
| 8 | +if __name__ == '__main__': |
| 9 | + parser = argparse.ArgumentParser(description='Rolling release update') |
| 10 | + parser.add_argument('--repo', help='Repository path', required=True) |
| 11 | + parser.add_argument('--new-base-branch', help='Branch name', required=True) |
| 12 | + parser.add_argument('--old-rolling-branch', help='Branch name for old rolling release: ex: sig-cloud-8/4.18.0-553.33.1.el8_10', required=True) |
| 13 | + args = parser.parse_args() |
| 14 | + |
| 15 | + |
| 16 | + rolling_product = args.old_rolling_branch.split('/')[0] |
| 17 | + print('[rolling release update] Rolling Product: ', rolling_product) |
| 18 | + |
| 19 | + repo = git.Repo(args.repo) |
| 20 | + print('[rolling release update] Checking out old Rolling Branch: ', args.old_rolling_branch) |
| 21 | + repo.git.checkout(args.old_rolling_branch) |
| 22 | + print('[rolling release update] Finding the last resf_kernel tag the rolling release was based') |
| 23 | + results = subprocess.run(['git', 'log', '--decorate', '--oneline'], stderr=subprocess.PIPE, stdout=subprocess.PIPE, |
| 24 | + cwd=args.repo) |
| 25 | + if results.returncode != 0: |
| 26 | + print(results.stderr) |
| 27 | + exit(1) |
| 28 | + latest_resf_sha = '' |
| 29 | + for line in results.stdout.split(b'\n'): |
| 30 | + if b'tag: resf_kernel' in line: |
| 31 | + print(line) |
| 32 | + latest_resf_sha = line.split(b' ')[0] |
| 33 | + break |
| 34 | + print('[rolling release update] Last RESF tag sha: ', latest_resf_sha) |
| 35 | + |
| 36 | + print('[rolling release update] Finding the CIQ Kernel and Associated Upstream commits between the last resf tag and HEAD') |
| 37 | + rolling_commit_map = {} |
| 38 | + rollint_commit_map_rev = {} |
| 39 | + rolling_commits = repo.git.log(f'{latest_resf_sha.decode()}..HEAD') |
| 40 | + for line in rolling_commits.split('\n'): |
| 41 | + if line.startswith('commit '): |
| 42 | + ciq_commit = line.split('commit ')[1] |
| 43 | + rolling_commit_map[ciq_commit] = '' |
| 44 | + if line.startswith(' commit '): |
| 45 | + upstream_commit = line.split(' commit ')[1] |
| 46 | + rolling_commit_map[ciq_commit] = upstream_commit |
| 47 | + rollint_commit_map_rev[upstream_commit] = ciq_commit |
| 48 | + |
| 49 | + print('{ "CIQ COMMMIT" : "UPSTREAM COMMMIT" }') |
| 50 | + print(json.dumps(rolling_commit_map, indent=2)) |
| 51 | + |
| 52 | + |
| 53 | + print('[rolling release update] Checking out new base branch: ', args.new_base_branch) |
| 54 | + repo.git.checkout(args.new_base_branch) |
| 55 | + |
| 56 | + print('[rolling release update] Finding the new resf_kernel tag the rolling release is based') |
| 57 | + results = subprocess.run(['git', 'log', '--decorate', '--oneline'], stderr=subprocess.PIPE, stdout=subprocess.PIPE, |
| 58 | + cwd=args.repo) |
| 59 | + if results.returncode != 0: |
| 60 | + print(results.stderr) |
| 61 | + exit(1) |
| 62 | + |
| 63 | + print('[rolling release update] Finding the kernel version for the new rolling release') |
| 64 | + new_rolling_branch_kernel = '' |
| 65 | + for line in results.stdout.split(b'\n'): |
| 66 | + if b'tag: resf_kernel' in line: |
| 67 | + print(line) |
| 68 | + r = re.match(b'.*(?P<vendor>.*)_kernel-(?P<kernel_ver>[0-9.-]*el[89]_[0-9]*)', line) |
| 69 | + print(r) |
| 70 | + if r: |
| 71 | + new_rolling_branch_kernel = r.group('kernel_ver') |
| 72 | + break |
| 73 | + new_rolling_branch_kernel = f'{rolling_product}/{new_rolling_branch_kernel.decode()}' |
| 74 | + print('[rolling release update} New Branch to create ', new_rolling_branch_kernel) |
| 75 | + |
| 76 | + print('[rolling release update] Check if branch Exists: ', new_rolling_branch_kernel) |
| 77 | + results = subprocess.run(['git', 'show-ref', '--quiet', f'refs/heads/{new_rolling_branch_kernel}'], |
| 78 | + stderr=subprocess.PIPE, stdout=subprocess.PIPE, cwd=args.repo) |
| 79 | + if results.returncode == 0: |
| 80 | + print(f'Branch {new_rolling_branch_kernel} already exists') |
| 81 | + exit(1) |
| 82 | + else: |
| 83 | + print(f'Branch {new_rolling_branch_kernel} does not exists creating') |
| 84 | + |
| 85 | + results = subprocess.run(['git', 'checkout', '-b', new_rolling_branch_kernel], stderr=subprocess.PIPE, |
| 86 | + stdout=subprocess.PIPE, cwd=args.repo) |
| 87 | + if results.returncode != 0: |
| 88 | + print(results.stderr) |
| 89 | + exit(1) |
| 90 | + |
| 91 | + print('[rolling release update] Crating Map of all new commits from last rolling release fork') |
| 92 | + new_base_commit_map = {} |
| 93 | + new_base_commit_map_rev = {} |
| 94 | + new_base_commits = repo.git.log(f'{latest_resf_sha.decode()}..HEAD') |
| 95 | + for line in new_base_commits.split('\n'): |
| 96 | + if line.startswith('commit '): |
| 97 | + ciq_commit = line.split('commit ')[1] |
| 98 | + new_base_commit_map[ciq_commit] = '' |
| 99 | + if line.startswith(' commit '): |
| 100 | + upstream_commit = line.split(' commit ')[1] |
| 101 | + new_base_commit_map[ciq_commit] = upstream_commit |
| 102 | + new_base_commit_map_rev[upstream_commit] = ciq_commit |
| 103 | + |
| 104 | + print('{ "CIQ COMMMIT" : "UPSTREAM COMMMIT" }') |
| 105 | + print(json.dumps(new_base_commit_map, indent=2)) |
| 106 | + |
| 107 | + print('[rolling release update] Checking if any of the commits from the old rolling release are already present in the new base branch') |
| 108 | + commits_to_remove = {} |
| 109 | + for ciq_commit, upstream_commit in rolling_commit_map.items(): |
| 110 | + if upstream_commit in new_base_commit_map_rev: |
| 111 | + print(f"Commit {ciq_commit} already present in new base branch") |
| 112 | + print(repo.git.show(ciq_commit)) |
| 113 | + commits_to_remove[ciq_commit] = upstream_commit |
| 114 | + |
| 115 | + print('[rolling release update] Removing commits from the new branch') |
| 116 | + for ciq_commit, upstream_commit in commits_to_remove.items(): |
| 117 | + del rolling_commit_map[ciq_commit] |
| 118 | + print("Removing commit: ", ciq_commit) |
| 119 | + repo.git.show(ciq_commit) |
| 120 | + |
| 121 | + print('[rolling release update] Applying the remaining commits to the new branch') |
| 122 | + for ciq_commit, upstream_commit in reversed(rolling_commit_map.items()): |
| 123 | + print('Applying commit ', repo.git.show('--pretty="%H %s"', '-s', ciq_commit)) |
| 124 | + result = subprocess.run(['git', 'cherry-pick', '-s', ciq_commit], stderr=subprocess.PIPE, |
| 125 | + stdout=subprocess.PIPE, cwd=args.repo) |
| 126 | + if result.returncode != 0: |
| 127 | + print(result.stderr.split(b'\n')) |
| 128 | + exit(1) |
| 129 | + |
0 commit comments