From afd62a083221c1249ef76f60db1c0a3d479def0f Mon Sep 17 00:00:00 2001 From: Michael Tietz Date: Mon, 13 Nov 2023 07:00:13 +0100 Subject: [PATCH] Add patches direct in yaml --- README.rst | 2 ++ git_aggregator/config.py | 24 ++++++++++++++++++++++++ git_aggregator/repo.py | 28 +++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 4a74110..744bdc3 100644 --- a/README.rst +++ b/README.rst @@ -343,11 +343,13 @@ Contributors * Simone Orsi (camptocamp_) * Artem Kostyuk * Jan Verbeek +* Michael Tietz (MT_Software_) .. _ACSONE: https://www.acsone.eu .. _Tecnativa: https://www.tecnativa.com .. _camptocamp: https://www.camptocamp.com .. _LasLabs: https://laslabs.com +.. _MT_Software: https://github.com/mt-software-de Maintainer ---------- diff --git a/git_aggregator/config.py b/git_aggregator/config.py index 0043e84..d8c13d2 100644 --- a/git_aggregator/config.py +++ b/git_aggregator/config.py @@ -5,6 +5,7 @@ import logging import os from string import Template +from pathlib import Path import yaml @@ -15,6 +16,28 @@ log = logging.getLogger(__name__) +def update_patches(repo_dict, repo_data): + """Check and update repo_dict with patch files""" + patches = repo_data.get("patches") + if not patches: + repo_dict["patches"] = [] + return + _patches = [] + for patch in patches: + path = Path(patch) + if not path.exists(): + raise ConfigException( + "%s: The patch file or directory does not exists" % path + ) + if path.is_file(): + _patches.append(path) + elif path.is_dir(): + for _path in path.iterdir(): + if _path.is_file(): + _patches.append(_path) + repo_dict["patches"] = _patches + + def get_repos(config, force=False): """Return a :py:obj:`list` list of repos from config file. :param config: the repos config in :py:class:`dict` format. @@ -128,6 +151,7 @@ def get_repos(config, force=False): cmds = [cmds] commands = cmds repo_dict['shell_command_after'] = commands + update_patches(repo_dict, repo_data) repo_list.append(repo_dict) return repo_list diff --git a/git_aggregator/repo.py b/git_aggregator/repo.py index 677334c..1bfe36c 100644 --- a/git_aggregator/repo.py +++ b/git_aggregator/repo.py @@ -38,7 +38,7 @@ class Repo(object): def __init__(self, cwd, remotes, merges, target, shell_command_after=None, fetch_all=False, defaults=None, - force=False): + force=False, patches=None): """Initialize a git repository aggregator :param cwd: path to the directory where to initialize the repository @@ -69,6 +69,7 @@ def __init__(self, cwd, remotes, merges, target, self.shell_command_after = shell_command_after or [] self.defaults = defaults or dict() self.force = force + self.patches = patches @property def git_version(self): @@ -165,6 +166,12 @@ def log_call(self, cmd, callwith=subprocess.check_call, ret = console_to_str(ret) return ret + def _apply_patches(self): + if not self.patches: + return + for patch in self.patches: + self._patch(patch) + def aggregate(self): """ Aggregate all merges into the target branch If the target_dir doesn't exist, create an empty git repo otherwise @@ -189,6 +196,7 @@ def aggregate(self): self._reset_to(origin["remote"], origin["ref"]) for merge in merges: self._merge(merge) + self._apply_patches() self._execute_shell_command_after() logger.info('End aggregation of %s', self.cwd) @@ -315,6 +323,24 @@ def _merge(self, merge): cmd += self._fetch_options(merge) + (merge["remote"], merge["ref"]) self.log_call(cmd, cwd=self.cwd) + def _patch(self, patch_path): + cmd = ( + "patch", + "-p1", + "--no-backup-if-mismatch", + "-t", + "-i", + str(patch_path.resolve()), + ) + if logger.getEffectiveLevel() != logging.DEBUG: + cmd += ('--quiet',) + self.log_call(cmd, cwd=self.cwd) + self.log_call(("git", "add", "."), cwd=self.cwd) + self.log_call( + ("git", "commit", "-am", "Applied patch %s" % str(patch_path)), + cwd=self.cwd, + ) + def _get_remotes(self): lines = self.log_call( ['git', 'remote', '-v'],