Skip to content

Commit

Permalink
Add patches direct in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-software-de committed Dec 4, 2023
1 parent 4a706d7 commit afd62a0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down
24 changes: 24 additions & 0 deletions git_aggregator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import os
from string import Template
from pathlib import Path

import yaml

Expand All @@ -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.
Expand Down Expand Up @@ -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

Expand Down
28 changes: 27 additions & 1 deletion git_aggregator/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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'],
Expand Down

0 comments on commit afd62a0

Please sign in to comment.