Skip to content

Commit afd62a0

Browse files
Add patches direct in yaml
1 parent 4a706d7 commit afd62a0

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,13 @@ Contributors
343343
* Simone Orsi (camptocamp_)
344344
* Artem Kostyuk
345345
* Jan Verbeek
346+
* Michael Tietz (MT_Software_)
346347

347348
.. _ACSONE: https://www.acsone.eu
348349
.. _Tecnativa: https://www.tecnativa.com
349350
.. _camptocamp: https://www.camptocamp.com
350351
.. _LasLabs: https://laslabs.com
352+
.. _MT_Software: https://github.com/mt-software-de
351353

352354
Maintainer
353355
----------

git_aggregator/config.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import os
77
from string import Template
8+
from pathlib import Path
89

910
import yaml
1011

@@ -15,6 +16,28 @@
1516
log = logging.getLogger(__name__)
1617

1718

19+
def update_patches(repo_dict, repo_data):
20+
"""Check and update repo_dict with patch files"""
21+
patches = repo_data.get("patches")
22+
if not patches:
23+
repo_dict["patches"] = []
24+
return
25+
_patches = []
26+
for patch in patches:
27+
path = Path(patch)
28+
if not path.exists():
29+
raise ConfigException(
30+
"%s: The patch file or directory does not exists" % path
31+
)
32+
if path.is_file():
33+
_patches.append(path)
34+
elif path.is_dir():
35+
for _path in path.iterdir():
36+
if _path.is_file():
37+
_patches.append(_path)
38+
repo_dict["patches"] = _patches
39+
40+
1841
def get_repos(config, force=False):
1942
"""Return a :py:obj:`list` list of repos from config file.
2043
:param config: the repos config in :py:class:`dict` format.
@@ -128,6 +151,7 @@ def get_repos(config, force=False):
128151
cmds = [cmds]
129152
commands = cmds
130153
repo_dict['shell_command_after'] = commands
154+
update_patches(repo_dict, repo_data)
131155
repo_list.append(repo_dict)
132156
return repo_list
133157

git_aggregator/repo.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Repo(object):
3838

3939
def __init__(self, cwd, remotes, merges, target,
4040
shell_command_after=None, fetch_all=False, defaults=None,
41-
force=False):
41+
force=False, patches=None):
4242
"""Initialize a git repository aggregator
4343
4444
:param cwd: path to the directory where to initialize the repository
@@ -69,6 +69,7 @@ def __init__(self, cwd, remotes, merges, target,
6969
self.shell_command_after = shell_command_after or []
7070
self.defaults = defaults or dict()
7171
self.force = force
72+
self.patches = patches
7273

7374
@property
7475
def git_version(self):
@@ -165,6 +166,12 @@ def log_call(self, cmd, callwith=subprocess.check_call,
165166
ret = console_to_str(ret)
166167
return ret
167168

169+
def _apply_patches(self):
170+
if not self.patches:
171+
return
172+
for patch in self.patches:
173+
self._patch(patch)
174+
168175
def aggregate(self):
169176
""" Aggregate all merges into the target branch
170177
If the target_dir doesn't exist, create an empty git repo otherwise
@@ -189,6 +196,7 @@ def aggregate(self):
189196
self._reset_to(origin["remote"], origin["ref"])
190197
for merge in merges:
191198
self._merge(merge)
199+
self._apply_patches()
192200
self._execute_shell_command_after()
193201
logger.info('End aggregation of %s', self.cwd)
194202

@@ -315,6 +323,24 @@ def _merge(self, merge):
315323
cmd += self._fetch_options(merge) + (merge["remote"], merge["ref"])
316324
self.log_call(cmd, cwd=self.cwd)
317325

326+
def _patch(self, patch_path):
327+
cmd = (
328+
"patch",
329+
"-p1",
330+
"--no-backup-if-mismatch",
331+
"-t",
332+
"-i",
333+
str(patch_path.resolve()),
334+
)
335+
if logger.getEffectiveLevel() != logging.DEBUG:
336+
cmd += ('--quiet',)
337+
self.log_call(cmd, cwd=self.cwd)
338+
self.log_call(("git", "add", "."), cwd=self.cwd)
339+
self.log_call(
340+
("git", "commit", "-am", "Applied patch %s" % str(patch_path)),
341+
cwd=self.cwd,
342+
)
343+
318344
def _get_remotes(self):
319345
lines = self.log_call(
320346
['git', 'remote', '-v'],

0 commit comments

Comments
 (0)