|
| 1 | +# SPDX-License-Identifier: GPL-2.0-only |
| 2 | +# |
| 3 | +# Copyright (C) 2021-2025 SUSE |
| 4 | +# Author: Marcos Paulo de Souza <[email protected]> |
| 5 | + |
| 6 | +import logging |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | + |
| 10 | +from klpbuild.klplib import utils |
| 11 | +from klpbuild.klplib.cmd import add_arg_lp_name |
| 12 | +from klpbuild.klplib.config import get_user_path |
| 13 | + |
| 14 | +PLUGIN_CMD = "format-patches" |
| 15 | + |
| 16 | +def register_argparser(subparser): |
| 17 | + fmt = subparser.add_parser( |
| 18 | + "format-patches", help="SLE specific. Extract patches from kgraft-patches" |
| 19 | + ) |
| 20 | + add_arg_lp_name(fmt) |
| 21 | + fmt.add_argument("-v", "--version", type=int, required=True, help="Version to be added, like vX") |
| 22 | + |
| 23 | + |
| 24 | +def run(lp_name, version): |
| 25 | + ver = f"v{version}" |
| 26 | + # index 1 will be the test file |
| 27 | + index = 2 |
| 28 | + |
| 29 | + kgr_patches = get_user_path('kgr_patches_dir') |
| 30 | + if not kgr_patches: |
| 31 | + logging.warning("kgr_patches_dir not found, patches will be incomplete") |
| 32 | + |
| 33 | + # Remove dir to avoid leftover patches with different names |
| 34 | + patches_dir = utils.get_workdir(lp_name)/"patches" |
| 35 | + shutil.rmtree(patches_dir, ignore_errors=True) |
| 36 | + |
| 37 | + test_src = utils.get_tests_path(lp_name) |
| 38 | + subprocess.check_output( |
| 39 | + [ |
| 40 | + "/usr/bin/git", |
| 41 | + "-C", |
| 42 | + str(get_user_path('kgr_patches_tests_dir')), |
| 43 | + "format-patch", |
| 44 | + "-1", |
| 45 | + f"{test_src}", |
| 46 | + "--cover-letter", |
| 47 | + "--start-number", |
| 48 | + "1", |
| 49 | + "--subject-prefix", |
| 50 | + f"PATCH {ver}", |
| 51 | + "--output-directory", |
| 52 | + f"{patches_dir}", |
| 53 | + ] |
| 54 | + ) |
| 55 | + |
| 56 | + # Filter only the branches related to this BSC |
| 57 | + for branch in utils.get_lp_branches(lp_name, kgr_patches): |
| 58 | + logging.info(branch) |
| 59 | + bname = branch.replace(lp_name + "_", "") |
| 60 | + bs = " ".join(bname.split("_")) |
| 61 | + bsc = lp_name.replace("bsc", "bsc#") |
| 62 | + |
| 63 | + prefix = f"PATCH {ver} {bsc} {bs}" |
| 64 | + |
| 65 | + subprocess.check_output( |
| 66 | + [ |
| 67 | + "/usr/bin/git", |
| 68 | + "-C", |
| 69 | + str(kgr_patches), |
| 70 | + "format-patch", |
| 71 | + "-1", |
| 72 | + branch, |
| 73 | + "--start-number", |
| 74 | + f"{index}", |
| 75 | + "--subject-prefix", |
| 76 | + f"{prefix}", |
| 77 | + "--output-directory", |
| 78 | + f"{patches_dir}", |
| 79 | + ] |
| 80 | + ) |
| 81 | + |
| 82 | + index += 1 |
0 commit comments