Skip to content

Commit cf18ce8

Browse files
karhamambolivar-ampere
authored andcommitted
project.py: extend cache support for submodules
If cache is used try to also use it for submodules. Signed-off-by: Kari Hamalainen <kari.hamalainen@nordicsemi.no>
1 parent aeca29a commit cf18ce8

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/west/app/project.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
ManifestProject, _manifest_content_at, ManifestImportFailed
2727
from west.manifest import is_group as is_project_group
2828
from west.manifest import MANIFEST_REV_BRANCH as MANIFEST_REV
29+
from west.manifest import Submodule
2930
from west.manifest import QUAL_MANIFEST_REV_BRANCH as QUAL_MANIFEST_REV
3031
from west.manifest import QUAL_REFS_WEST as QUAL_REFS
3132

@@ -1160,23 +1161,43 @@ def update_submodules(self, project):
11601161
for config_opt in self.args.submodule_init_config:
11611162
config_opts.extend(['-c', config_opt])
11621163

1163-
# For the list type, update given list of submodules.
1164-
if isinstance(submodules, list):
1165-
for submodule in submodules:
1164+
cache_dir = self.project_cache(project)
1165+
# For the boolean type, update all the submodules.
1166+
if isinstance(submodules, bool):
1167+
if cache_dir is None:
11661168
if self.sync_submodules:
1167-
project.git(['submodule', 'sync', '--recursive',
1168-
'--', submodule.path])
1169+
project.git(['submodule', 'sync', '--recursive'])
11691170
project.git(config_opts +
1170-
['submodule', 'update',
1171-
'--init', submodules_update_strategy,
1172-
'--recursive', submodule.path])
1173-
# For the bool type, update all project submodules
1174-
elif isinstance(submodules, bool):
1171+
['submodule', 'update', '--init',
1172+
submodules_update_strategy, '--recursive'])
1173+
return
1174+
else:
1175+
# Cache used so convert to a list so that --reference can be used.
1176+
res = project.git(['submodule', 'status'], capture_stdout=True)
1177+
if not res.stdout or res.returncode:
1178+
self.die(
1179+
f"Submodule status failed for project: {project.name}.")
1180+
mod_list = [s.strip() for s in res.stdout.decode('utf-8').split('\n') if s]
1181+
submodules = [Submodule(line.split(' ')[1]) for line in mod_list]
1182+
1183+
# For the list type, update given list of submodules.
1184+
for submodule in submodules:
11751185
if self.sync_submodules:
1176-
project.git(['submodule', 'sync', '--recursive'])
1186+
project.git(['submodule', 'sync', '--recursive',
1187+
'--', submodule.path])
1188+
ref = []
1189+
if (cache_dir):
1190+
submodule_ref = Path(cache_dir, submodule.path)
1191+
if any(os.scandir(submodule_ref)):
1192+
ref = ['--reference', os.fspath(submodule_ref)]
1193+
self.small_banner(f'using reference from: {submodule_ref}')
1194+
self.dbg(
1195+
f'found {submodule.path} in --path-cache {submodule_ref}',
1196+
level=Verbosity.DBG_MORE)
11771197
project.git(config_opts +
1178-
['submodule', 'update', '--init',
1179-
submodules_update_strategy, '--recursive'])
1198+
['submodule', 'update',
1199+
'--init', submodules_update_strategy,
1200+
'--recursive'] + ref + [submodule.path])
11801201

11811202
def update(self, project):
11821203
if self.args.stats:

tests/test_project.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ def test_update_recovery(tmpdir):
12621262
assert prev == rgood
12631263

12641264

1265-
def setup_cache_workspace(workspace, foo_head, bar_head):
1265+
def setup_cache_workspace(workspace, remote, foo_head, bar_head):
12661266
# Shared helper code that sets up a workspace used to test the
12671267
# 'west update --foo-cache' options.
12681268

@@ -1275,10 +1275,10 @@ def setup_cache_workspace(workspace, foo_head, bar_head):
12751275
projects:
12761276
- name: foo
12771277
path: subdir/foo
1278-
url: should-not-be-fetched
1278+
url: file://{remote}
12791279
revision: {foo_head}
12801280
- name: bar
1281-
url: should-not-be-fetched
1281+
url: file://{remote}
12821282
revision: {bar_head}
12831283
''')
12841284

@@ -1287,14 +1287,16 @@ def test_update_name_cache(tmpdir):
12871287
# Test that 'west update --name-cache' works and doesn't hit the
12881288
# network if it doesn't have to.
12891289

1290+
remote = tmpdir / 'remote'
1291+
create_repo(remote)
12901292
name_cache_dir = tmpdir / 'name_cache'
12911293
create_repo(name_cache_dir / 'foo')
12921294
create_repo(name_cache_dir / 'bar')
12931295
foo_head = rev_parse(name_cache_dir / 'foo', 'HEAD')
12941296
bar_head = rev_parse(name_cache_dir / 'bar', 'HEAD')
12951297

12961298
workspace = tmpdir / 'workspace'
1297-
setup_cache_workspace(workspace, foo_head, bar_head)
1299+
setup_cache_workspace(workspace, remote, foo_head, bar_head)
12981300
workspace.chdir()
12991301
foo = workspace / 'subdir' / 'foo'
13001302
bar = workspace / 'bar'
@@ -1322,14 +1324,16 @@ def test_update_path_cache(tmpdir):
13221324
# Test that 'west update --path-cache' works and doesn't hit the
13231325
# network if it doesn't have to.
13241326

1327+
remote = tmpdir / 'remote'
1328+
create_repo(remote)
13251329
path_cache_dir = tmpdir / 'path_cache_dir'
13261330
create_repo(path_cache_dir / 'subdir' / 'foo')
13271331
create_repo(path_cache_dir / 'bar')
13281332
foo_head = rev_parse(path_cache_dir / 'subdir' / 'foo', 'HEAD')
13291333
bar_head = rev_parse(path_cache_dir / 'bar', 'HEAD')
13301334

13311335
workspace = tmpdir / 'workspace'
1332-
setup_cache_workspace(workspace, foo_head, bar_head)
1336+
setup_cache_workspace(workspace, remote, foo_head, bar_head)
13331337
workspace.chdir()
13341338
foo = workspace / 'subdir' / 'foo'
13351339
bar = workspace / 'bar'

0 commit comments

Comments
 (0)