|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Script to apply a set of patches to llvm-project sources. |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import os |
| 9 | +import pathlib |
| 10 | +import subprocess |
| 11 | +import sys |
| 12 | + |
| 13 | + |
| 14 | +def main(): |
| 15 | + parser = argparse.ArgumentParser(description=__doc__) |
| 16 | + parser.add_argument( |
| 17 | + "patchdir", |
| 18 | + help="Set of patches to apply. This should be a directory containing one or more ordered *.patch files.", |
| 19 | + ) |
| 20 | + parser.add_argument( |
| 21 | + "--llvm_dir", |
| 22 | + help="Directory of the llvm-project git checkout, if not the current directory.", |
| 23 | + ) |
| 24 | + parser.add_argument( |
| 25 | + "--method", |
| 26 | + choices=["am", "apply"], |
| 27 | + default="apply", |
| 28 | + help="Git command to use. git am will add each patch as a commit, whereas git apply will leave patched changes staged.", |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "--reset", |
| 32 | + help="Clean and hard reset the repo to a specified commit before patching.", |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "--restore_on_fail", |
| 36 | + action="store_true", |
| 37 | + help="If a patch in a series cannot be applied, restore the original state instead of leaving patches missing. Return code will be 2 instead of 1.", |
| 38 | + ) |
| 39 | + args = parser.parse_args() |
| 40 | + |
| 41 | + if args.llvm_dir: |
| 42 | + git_cmd = ["git", "-C", args.llvm_dir] |
| 43 | + else: |
| 44 | + git_cmd = ["git"] |
| 45 | + |
| 46 | + if args.reset: |
| 47 | + reset_args = git_cmd + ["reset", "--quiet", "--hard", args.reset] |
| 48 | + subprocess.check_output(reset_args) |
| 49 | + clean_args = git_cmd + ["clean", "--quiet", "--force", "-dx", args.reset] |
| 50 | + subprocess.check_output(clean_args) |
| 51 | + |
| 52 | + abs_patch_dir = os.path.abspath(args.patchdir) |
| 53 | + patch_list = list(pathlib.Path(abs_patch_dir).glob("*.patch")) |
| 54 | + patch_list.sort() |
| 55 | + |
| 56 | + print(f"Found {len(patch_list)} patches to apply:") |
| 57 | + print("\n".join(p.name for p in patch_list)) |
| 58 | + |
| 59 | + if args.method == "am": |
| 60 | + merge_args = git_cmd + ["am", "-k", "--ignore-whitespace", "--3way"] |
| 61 | + for patch in patch_list: |
| 62 | + merge_args.append(str(patch)) |
| 63 | + p = subprocess.run(merge_args, capture_output=True, text=True) |
| 64 | + print(p.stdout) |
| 65 | + print(p.stderr) |
| 66 | + |
| 67 | + if p.returncode == 0: |
| 68 | + print(f"All patches applied.") |
| 69 | + sys.exit(0) |
| 70 | + if args.restore_on_fail: |
| 71 | + # Check that the operation can be aborted. |
| 72 | + # git am doesn't give any specific return codes, |
| 73 | + # so check for unresolved working files. |
| 74 | + rebase_apply_path = os.path.join(".git", "rebase-apply") |
| 75 | + if args.llvm_dir: |
| 76 | + rebase_apply_path = os.path.join(args.llvm_dir, rebase_apply_path) |
| 77 | + if os.path.isdir(rebase_apply_path): |
| 78 | + print("Aborting git am...") |
| 79 | + subprocess.run(git_cmd + ["am", "--abort"], check=True) |
| 80 | + print(f"Abort successful.") |
| 81 | + sys.exit(2) |
| 82 | + else: |
| 83 | + print("Unable to abort.") |
| 84 | + sys.exit(1) |
| 85 | + else: |
| 86 | + applied_patches = [] |
| 87 | + for current_patch in patch_list: |
| 88 | + print(f"Checking {current_patch.name}...") |
| 89 | + # Check that the patch applies before trying to apply it. |
| 90 | + apply_check_args = git_cmd + [ |
| 91 | + "apply", |
| 92 | + "--ignore-whitespace", |
| 93 | + "--3way", |
| 94 | + "--check", |
| 95 | + str(current_patch), |
| 96 | + ] |
| 97 | + p_check = subprocess.run(apply_check_args) |
| 98 | + |
| 99 | + if p_check.returncode == 0: |
| 100 | + # Patch will apply. |
| 101 | + print(f"Applying {current_patch.name}...") |
| 102 | + apply_args = git_cmd + [ |
| 103 | + "apply", |
| 104 | + "--ignore-whitespace", |
| 105 | + "--3way", |
| 106 | + str(current_patch), |
| 107 | + ] |
| 108 | + apply_args = subprocess.run(apply_args, check=True) |
| 109 | + applied_patches.append(current_patch) |
| 110 | + else: |
| 111 | + # Patch won't apply. |
| 112 | + print(f"Unable to apply {current_patch.name}") |
| 113 | + if args.restore_on_fail: |
| 114 | + # Remove any patches that have already been applied. |
| 115 | + while len(applied_patches) > 0: |
| 116 | + previous_patch = applied_patches.pop() |
| 117 | + print(f"Reversing {previous_patch.name}...") |
| 118 | + reverse_args = git_cmd + [ |
| 119 | + "apply", |
| 120 | + "--ignore-whitespace", |
| 121 | + "--3way", |
| 122 | + "--reverse", |
| 123 | + str(previous_patch), |
| 124 | + ] |
| 125 | + p_check = subprocess.run(reverse_args, check=True) |
| 126 | + print( |
| 127 | + f"Rollback successful, failure occured on {current_patch.name}" |
| 128 | + ) |
| 129 | + sys.exit(2) |
| 130 | + sys.exit(1) |
| 131 | + print(f"All patches applied.") |
| 132 | + |
| 133 | + |
| 134 | +main() |
0 commit comments