Skip to content

Commit 18115aa

Browse files
authored
Merge pull request #48 from janverb/optional-target
Make the `target` section optional
2 parents 1ae3322 + 924027c commit 18115aa

File tree

5 files changed

+76
-20
lines changed

5 files changed

+76
-20
lines changed

README.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ Create a ``repos.yaml`` or ``repos.yml`` file:
2626
- oca 8.0
2727
- oca refs/pull/105/head
2828
- oca refs/pull/106/head
29-
target: acsone aggregated_branch_name
3029
3130
./connector-interfaces:
3231
remotes:
@@ -43,6 +42,8 @@ Create a ``repos.yaml`` or ``repos.yml`` file:
4342
4443
Environment variables inside of this file will be expanded if the proper option is selected.
4544

45+
All the ``merges`` are combined into a single branch. By default this branch is called ``_git_aggregated`` but another name may be given in the ``target`` section.
46+
4647
Fetching only required branches
4748
-------------------------------
4849

@@ -181,7 +182,8 @@ Use additional variables from file while expanding:
181182
182183
The env file should contain `VAR=value` lines. Lines starting with # are ignored.
183184

184-
You can also aggregate and automatically push the result to the target:
185+
You can also aggregate and automatically push the result to the target, if the
186+
``target`` option is configured:
185187

186188
.. code-block:: bash
187189

git_aggregator/config.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,24 @@ def get_repos(config, force=False):
9393
repo_dict["fetch_all"] = frozenset((repo_dict["fetch_all"],))
9494
elif isinstance(repo_dict["fetch_all"], list):
9595
repo_dict["fetch_all"] = frozenset(repo_dict["fetch_all"])
96-
if 'target' not in repo_data:
97-
raise ConfigException('%s: No target defined.' % directory)
98-
parts = (repo_data.get('target') or "") .split(' ')
99-
if len(parts) != 2:
96+
97+
# Explicitly cast to str because e.g. `8.0` will be parsed as float
98+
# There are many cases this doesn't handle, but the float one is common
99+
# because of Odoo conventions
100+
parts = str(repo_data.get('target', "")).split()
101+
remote_name = None
102+
if len(parts) == 0:
103+
branch = "_git_aggregated"
104+
elif len(parts) == 1:
105+
branch = parts[0]
106+
elif len(parts) == 2:
107+
remote_name, branch = parts
108+
else:
100109
raise ConfigException(
101110
'%s: Target must be formatted as '
102-
'"remote_name branch_name"' % directory)
111+
'"[remote_name] branch_name"' % directory)
103112

104-
remote_name, branch = repo_data.get('target').split(' ')
105-
if remote_name not in remote_names:
113+
if remote_name is not None and remote_name not in remote_names:
106114
raise ConfigException(
107115
'%s: Target remote %s not defined in remotes.' %
108116
(directory, remote_name))

git_aggregator/repo.py

+4
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ def fetch(self):
208208
def push(self):
209209
remote = self.target['remote']
210210
branch = self.target['branch']
211+
if remote is None:
212+
raise GitAggregatorException(
213+
"Cannot push %s, no target remote configured" % branch
214+
)
211215
logger.info("Push %s to %s", branch, remote)
212216
self.log_call(['git', 'push', '-f', remote, branch], cwd=self.cwd)
213217

tests/test_config.py

+25-11
Original file line numberDiff line numberDiff line change
@@ -252,40 +252,54 @@ def test_load_target_exception(self):
252252
oca: https://github.com/OCA/product-attribute.git
253253
merges:
254254
- oca 8.0
255+
target: oca 8.0 extra_arg
255256
"""
256257
with self.assertRaises(ConfigException) as ex:
257258
config.get_repos(self._parse_config(config_yaml))
258-
self.assertEquals(ex.exception.args[0],
259-
'/product_attribute: No target defined.')
259+
self.assertEquals(
260+
ex.exception.args[0],
261+
'/product_attribute: Target must be formatted as '
262+
'"[remote_name] branch_name"')
260263

261264
config_yaml = """
262265
/product_attribute:
263266
remotes:
264267
oca: https://github.com/OCA/product-attribute.git
265268
merges:
266269
- oca 8.0
267-
target:
270+
target: oba aggregated_branch
268271
"""
269272
with self.assertRaises(ConfigException) as ex:
270273
config.get_repos(self._parse_config(config_yaml))
271274
self.assertEquals(
272275
ex.exception.args[0],
273-
'/product_attribute: Target must be formatted as '
274-
'"remote_name branch_name"')
276+
'/product_attribute: Target remote oba not defined in remotes.')
275277

278+
def test_target_defaults(self):
276279
config_yaml = """
277280
/product_attribute:
278281
remotes:
279282
oca: https://github.com/OCA/product-attribute.git
280283
merges:
281284
- oca 8.0
282-
target: oba aggregated_branch
285+
target: 8.0
283286
"""
284-
with self.assertRaises(ConfigException) as ex:
285-
config.get_repos(self._parse_config(config_yaml))
286-
self.assertEquals(
287-
ex.exception.args[0],
288-
'/product_attribute: Target remote oba not defined in remotes.')
287+
repos = config.get_repos(self._parse_config(config_yaml))
288+
self.assertDictEqual(
289+
repos[0]["target"], {"branch": "8.0", "remote": None}
290+
)
291+
292+
config_yaml = """
293+
/product_attribute:
294+
remotes:
295+
oca: https://github.com/OCA/product-attribute.git
296+
merges:
297+
- oca 8.0
298+
"""
299+
repos = config.get_repos(self._parse_config(config_yaml))
300+
self.assertDictEqual(
301+
repos[0]["target"], {"branch": "_git_aggregated", "remote": None}
302+
)
289303

290304
def test_import_config__not_found(self):
291305
with self.assertRaises(ConfigException) as exc:

tests/test_repo.py

+28
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,34 @@ def test_simple_merge(self):
150150
self.assertEquals(rtype, 'branch')
151151
self.assertTrue(sha)
152152

153+
def test_push_missing_remote(self):
154+
remotes = [{
155+
'name': 'r1',
156+
'url': self.url_remote1
157+
}, {
158+
'name': 'r2',
159+
'url': self.url_remote2
160+
}]
161+
merges = [{
162+
'remote': 'r1',
163+
'ref': 'tag1'
164+
}, {
165+
'remote': 'r2',
166+
'ref': self.commit_3_sha
167+
}]
168+
target = {
169+
'remote': None,
170+
'branch': 'agg'
171+
}
172+
repo = Repo(self.cwd, remotes, merges, target, fetch_all=True)
173+
repo.aggregate()
174+
with self.assertRaises(exception.GitAggregatorException) as ex:
175+
repo.push()
176+
self.assertEquals(
177+
ex.exception.args[0],
178+
"Cannot push agg, no target remote configured"
179+
)
180+
153181
def test_update_aggregate(self):
154182
# in this test
155183
# * we'll aggregate a first time r1 master with r2

0 commit comments

Comments
 (0)