|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +DESCRIPTION = """ |
| 4 | +This script merges one release branch of SKC into another. |
| 5 | +
|
| 6 | +Example 1: Merge stackhpc/yoga into stackhpc/zed: |
| 7 | +
|
| 8 | + merge.py yoga zed |
| 9 | +
|
| 10 | +Example 2: Merge the branch created in example 1 into stackhpc/2023.1: |
| 11 | +
|
| 12 | + merge.py zed 2023.1 zed-yoga-merge |
| 13 | +
|
| 14 | +Example 3: Continue after manually resolving merge conflicts seen in example 2: |
| 15 | +
|
| 16 | + merge.py zed 2023.1 zed-yoga-merge --continue |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +import argparse |
| 21 | +import os |
| 22 | +from subprocess import check_call, check_output |
| 23 | +import sys |
| 24 | + |
| 25 | + |
| 26 | +def command(cmd): |
| 27 | + print("Running:", cmd) |
| 28 | + check_call(cmd) |
| 29 | + |
| 30 | + |
| 31 | +def parse_args(): |
| 32 | + parser = argparse.ArgumentParser(description=DESCRIPTION, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 33 | + #"Merge one branch of SKC into the next") |
| 34 | + parser.add_argument("previous", type=str, help="The previous version") |
| 35 | + parser.add_argument("current", type=str, help="The current version") |
| 36 | + parser.add_argument("previous_branch", type=str, nargs="?", default=None, help="Optional branch to use as the previous release. Allows merging multiple branches in parallel.") |
| 37 | + parser.add_argument("--continue", dest="cont", action="store_true", help="Continue after merge conflicts have been resolved.") |
| 38 | + parser.add_argument("--remote", type=str, default="origin", help="Git remote") |
| 39 | + return parser.parse_args() |
| 40 | + |
| 41 | + |
| 42 | +def fetch(args): |
| 43 | + command(["git", "fetch", args.remote]) |
| 44 | + |
| 45 | + |
| 46 | +def checkout(args): |
| 47 | + merge_branch = f"{args.current}-{args.previous}-merge" |
| 48 | + current_branch = f"{args.remote}/stackhpc/{args.current}" |
| 49 | + command(["git", "checkout", "-B", merge_branch, current_branch]) |
| 50 | + |
| 51 | + |
| 52 | +def update_submodules(): |
| 53 | + command(["git", "submodule", "update"]) |
| 54 | + |
| 55 | + |
| 56 | +def merge_in_progress(): |
| 57 | + repo_root = check_output(["git", "rev-parse", "--show-toplevel"]) |
| 58 | + repo_root = repo_root.decode().strip() |
| 59 | + return os.path.isfile(os.path.join(repo_root, ".git", "MERGE_HEAD")) |
| 60 | + |
| 61 | + |
| 62 | +def uncommitted_changes(): |
| 63 | + unstaged = check_output(["git", "diff"]) |
| 64 | + staged = check_output(["git", "diff", "--cached"]) |
| 65 | + return unstaged or staged |
| 66 | + |
| 67 | + |
| 68 | +def continue_merge(): |
| 69 | + if merge_in_progress(): |
| 70 | + command(["git", "merge", "--continue"]) |
| 71 | + else: |
| 72 | + print("No merge in progress") |
| 73 | + |
| 74 | + |
| 75 | +def merge(args): |
| 76 | + if args.previous_branch: |
| 77 | + previous_branch = args.previous_branch |
| 78 | + else: |
| 79 | + previous_branch = f"{args.remote}/stackhpc/{args.previous}" |
| 80 | + commit_message = f"Merge stackhpc/{args.previous} into stackhpc/{args.current}" |
| 81 | + command(["git", "merge", previous_branch, "-m", commit_message]) |
| 82 | + |
| 83 | + |
| 84 | +def show_diff(args): |
| 85 | + print("Proposed changes:") |
| 86 | + current_branch = f"{args.remote}/stackhpc/{args.current}" |
| 87 | + command(["git", "diff", current_branch]) |
| 88 | + |
| 89 | + |
| 90 | +def create_pr(args): |
| 91 | + current_branch = f"stackhpc/{args.current}" |
| 92 | + pr_title = f"{args.current}: {args.previous} merge" |
| 93 | + command(["gh", "pr", "create", "-f", "-a", "@me", "-B", current_branch, "-t", pr_title]) |
| 94 | + |
| 95 | + |
| 96 | +def main(): |
| 97 | + args = parse_args() |
| 98 | + if args.cont: |
| 99 | + continue_merge() |
| 100 | + else: |
| 101 | + if merge_in_progress(): |
| 102 | + print("Merge in progress - did you miss the --continue argument?") |
| 103 | + sys.exit(1) |
| 104 | + if uncommitted_changes(): |
| 105 | + print("You have uncommitted changes - aborting") |
| 106 | + sys.exit(1) |
| 107 | + fetch(args) |
| 108 | + checkout(args) |
| 109 | + update_submodules() |
| 110 | + merge(args) |
| 111 | + show_diff(args) |
| 112 | + create_pr(args) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments