Peter Pentchev | b91fc93 | 2023-01-19 15:27:13 +0200 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net> |
| 2 | # SPDX-License-Identifier: BSD-2-Clause |
| 3 | """Run git-if-needed.""" |
| 4 | |
| 5 | from __future__ import annotations |
| 6 | |
| 7 | import shlex |
Peter Pentchev | 81fa766 | 2024-01-18 13:19:34 +0200 | [diff] [blame] | 8 | import subprocess # noqa: S404 |
Peter Pentchev | b91fc93 | 2023-01-19 15:27:13 +0200 | [diff] [blame] | 9 | from typing import TYPE_CHECKING |
| 10 | |
| 11 | from . import defs |
| 12 | |
Peter Pentchev | 2c01f8f | 2024-01-18 13:37:48 +0200 | [diff] [blame^] | 13 | |
Peter Pentchev | b91fc93 | 2023-01-19 15:27:13 +0200 | [diff] [blame] | 14 | if TYPE_CHECKING: |
| 15 | import pathlib |
| 16 | |
| 17 | |
| 18 | def apply_series(cfg: defs.Config, tempd: pathlib.Path) -> None: |
| 19 | """Run git-if-needed to apply all the patches in a series file.""" |
| 20 | cmd: list[str | pathlib.Path] = [cfg.program, "-s", cfg.series, "--", "am"] |
| 21 | cmdstr = shlex.join(str(arg) for arg in cmd) |
| 22 | cfg.log.debug("Running `%(cmdstr)s`", {"cmdstr": cmdstr}) |
| 23 | try: |
Peter Pentchev | 78ab37b | 2024-01-18 13:07:33 +0200 | [diff] [blame] | 24 | subprocess.run(cmd, check=True, cwd=tempd, shell=False) # noqa: S603 |
Peter Pentchev | b91fc93 | 2023-01-19 15:27:13 +0200 | [diff] [blame] | 25 | except (OSError, subprocess.CalledProcessError) as err: |
| 26 | raise defs.GApplyError(f"Could not run `{cmdstr}` in {tempd}: {err}") from err |