Skip to content

Commit d15a9b4

Browse files
[IMP] Add patches directly in repos.yaml
Appling it without extra command in shell_command_after
1 parent b662f47 commit d15a9b4

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

git_aggregator/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@
99

1010
from ._compat import string_types
1111
from .exception import ConfigException
12+
from .patch import Patches
1213

1314
log = logging.getLogger(__name__)
1415

1516

17+
def update_patches(repo_dict, repo_data):
18+
"""Check and update repo_dict with patch files"""
19+
patches_data = repo_data.get("patches")
20+
patches = repo_dict.setdefault("patches", Patches())
21+
if not patches_data:
22+
return
23+
for patch in patches_data:
24+
patches += Patches.prepare_patches(patch, repo_dict.get("cwd"))
25+
26+
1627
def get_repos(config, force=False):
1728
"""Return a :py:obj:`list` list of repos from config file.
1829
:param config: the repos config in :py:class:`dict` format.
@@ -126,6 +137,7 @@ def get_repos(config, force=False):
126137
cmds = [cmds]
127138
commands = cmds
128139
repo_dict['shell_command_after'] = commands
140+
update_patches(repo_dict, repo_data)
129141
repo_list.append(repo_dict)
130142
return repo_list
131143

git_aggregator/patch.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2023 Michael Tietz (MT Software) <[email protected]>
2+
# License AGPLv3 (http://www.gnu.org/licenses/agpl-3.0-standalone.html)
3+
import logging
4+
import subprocess
5+
from pathlib import Path
6+
7+
from .command import CommandExecutor
8+
9+
logger = logging.getLogger(__name__)
10+
11+
12+
class Patch(CommandExecutor):
13+
is_local = False
14+
15+
def __init__(self, path, cwd):
16+
super().__init__(cwd)
17+
self.path = path
18+
path = Path(path)
19+
if path.exists():
20+
self.is_local = True
21+
22+
def retrive_data(self):
23+
path = self.path
24+
if self.is_local:
25+
patch_path = Path(path).absolute()
26+
path = f"FILE:{str(patch_path)}"
27+
cmd = [
28+
"curl",
29+
path,
30+
]
31+
if logger.getEffectiveLevel() != logging.DEBUG:
32+
cmd.append('-s')
33+
return self.log_call(
34+
cmd,
35+
callwith=subprocess.Popen,
36+
stdout=subprocess.PIPE
37+
)
38+
39+
def apply(self):
40+
res = self.retrive_data()
41+
cmd = [
42+
"git",
43+
"am",
44+
]
45+
if logger.getEffectiveLevel() != logging.DEBUG:
46+
cmd.append('--quiet')
47+
self.log_call(cmd, cwd=self.cwd, stdin=res.stdout)
48+
49+
50+
class Patches(list):
51+
"""List of patches"""
52+
@staticmethod
53+
def prepare_patches(path, cwd):
54+
_path = Path(path)
55+
patches = Patches()
56+
if not _path.exists() or _path.is_file():
57+
patches.append(Patch(path, cwd))
58+
elif _path.is_dir():
59+
for fpath in _path.iterdir():
60+
if fpath.is_file():
61+
patches.append(Patch(str(fpath), cwd))
62+
return patches
63+
64+
def apply(self):
65+
for patch in self:
66+
patch.apply()

git_aggregator/repo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Repo(CommandExecutor):
3737

3838
def __init__(self, cwd, remotes, merges, target,
3939
shell_command_after=None, fetch_all=False, defaults=None,
40-
force=False):
40+
force=False, patches=None):
4141
"""Initialize a git repository aggregator
4242
4343
:param cwd: path to the directory where to initialize the repository
@@ -68,6 +68,7 @@ def __init__(self, cwd, remotes, merges, target,
6868
self.shell_command_after = shell_command_after or []
6969
self.defaults = defaults or dict()
7070
self.force = force
71+
self.patches = patches
7172

7273
@property
7374
def git_version(self):
@@ -173,6 +174,7 @@ def aggregate(self):
173174
self._reset_to(origin["remote"], origin["ref"])
174175
for merge in merges:
175176
self._merge(merge)
177+
self.patches.apply()
176178
self._execute_shell_command_after()
177179
logger.info('End aggregation of %s', self.cwd)
178180

tests/test_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_load(self):
4444
'merges': [{'ref': '8.0', 'remote': 'oca'},
4545
{'ref': 'refs/pull/105/head', 'remote': 'oca'},
4646
{'ref': 'refs/pull/106/head', 'remote': 'oca'}],
47+
'patches': [],
4748
'remotes': [],
4849
'shell_command_after': [],
4950
'target': {'branch': 'aggregated_branch_name',
@@ -91,6 +92,7 @@ def test_load_defaults(self):
9192
'merges': [{'ref': '8.0', 'remote': 'oca', 'depth': 1000},
9293
{'ref': 'refs/pull/105/head', 'remote': 'oca'},
9394
{'ref': 'refs/pull/106/head', 'remote': 'oca'}],
95+
'patches': [],
9496
'remotes': [],
9597
'shell_command_after': [],
9698
'target': {'branch': 'aggregated_branch_name',

0 commit comments

Comments
 (0)