Skip to content

Commit 4b94e77

Browse files
Albert AkmukhametovLUCI
Albert Akmukhametov
authored and
LUCI
committed
Sync: Fix full submodule sync while shallow specified
Git allows to clone submodules as shallow clone [1]. On the other hand, when repo synchronize a projcet with submodules inside, it ignores the shallow parameter. When a project contains submodules, project.py parses the .gitmodules file for URL and path. This parsing does not consider the shallow option. Consequently, this parameter is not propgated to newly created Project instance for that submodule. [1] https://git-scm.com/docs/gitmodules#Documentation/gitmodules.txt-submoduleltnamegtshallow Change-Id: I54fc9c69ae1b8e3cda2801202e3f0c7693b718d2 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/454261 Tested-by: Albert Akmukhametov <alb.02057@gmail.com> Commit-Queue: Albert Akmukhametov <alb.02057@gmail.com> Reviewed-by: Josip Sokcevic <sokcevic@chromium.org> Reviewed-by: Никита Сказкоподателев (Nask) <skazkopodatelev@gmail.com>
1 parent fc901b9 commit 4b94e77

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

project.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,24 +2197,27 @@ def _GetSubmodules(self):
21972197

21982198
def get_submodules(gitdir, rev):
21992199
# Parse .gitmodules for submodule sub_paths and sub_urls.
2200-
sub_paths, sub_urls = parse_gitmodules(gitdir, rev)
2200+
sub_paths, sub_urls, sub_shallows = parse_gitmodules(gitdir, rev)
22012201
if not sub_paths:
22022202
return []
22032203
# Run `git ls-tree` to read SHAs of submodule object, which happen
22042204
# to be revision of submodule repository.
22052205
sub_revs = git_ls_tree(gitdir, rev, sub_paths)
22062206
submodules = []
2207-
for sub_path, sub_url in zip(sub_paths, sub_urls):
2207+
for sub_path, sub_url, sub_shallow in zip(
2208+
sub_paths, sub_urls, sub_shallows
2209+
):
22082210
try:
22092211
sub_rev = sub_revs[sub_path]
22102212
except KeyError:
22112213
# Ignore non-exist submodules.
22122214
continue
2213-
submodules.append((sub_rev, sub_path, sub_url))
2215+
submodules.append((sub_rev, sub_path, sub_url, sub_shallow))
22142216
return submodules
22152217

22162218
re_path = re.compile(r"^submodule\.(.+)\.path=(.*)$")
22172219
re_url = re.compile(r"^submodule\.(.+)\.url=(.*)$")
2220+
re_shallow = re.compile(r"^submodule\.(.+)\.shallow=(.*)$")
22182221

22192222
def parse_gitmodules(gitdir, rev):
22202223
cmd = ["cat-file", "blob", "%s:.gitmodules" % rev]
@@ -2228,9 +2231,9 @@ def parse_gitmodules(gitdir, rev):
22282231
gitdir=gitdir,
22292232
)
22302233
except GitError:
2231-
return [], []
2234+
return [], [], []
22322235
if p.Wait() != 0:
2233-
return [], []
2236+
return [], [], []
22342237

22352238
gitmodules_lines = []
22362239
fd, temp_gitmodules_path = tempfile.mkstemp()
@@ -2247,16 +2250,17 @@ def parse_gitmodules(gitdir, rev):
22472250
gitdir=gitdir,
22482251
)
22492252
if p.Wait() != 0:
2250-
return [], []
2253+
return [], [], []
22512254
gitmodules_lines = p.stdout.split("\n")
22522255
except GitError:
2253-
return [], []
2256+
return [], [], []
22542257
finally:
22552258
platform_utils.remove(temp_gitmodules_path)
22562259

22572260
names = set()
22582261
paths = {}
22592262
urls = {}
2263+
shallows = {}
22602264
for line in gitmodules_lines:
22612265
if not line:
22622266
continue
@@ -2270,10 +2274,16 @@ def parse_gitmodules(gitdir, rev):
22702274
names.add(m.group(1))
22712275
urls[m.group(1)] = m.group(2)
22722276
continue
2277+
m = re_shallow.match(line)
2278+
if m:
2279+
names.add(m.group(1))
2280+
shallows[m.group(1)] = m.group(2)
2281+
continue
22732282
names = sorted(names)
22742283
return (
22752284
[paths.get(name, "") for name in names],
22762285
[urls.get(name, "") for name in names],
2286+
[shallows.get(name, "") for name in names],
22772287
)
22782288

22792289
def git_ls_tree(gitdir, rev, paths):
@@ -2314,7 +2324,7 @@ def GetDerivedSubprojects(self):
23142324
# If git repo does not exist yet, querying its submodules will
23152325
# mess up its states; so return here.
23162326
return result
2317-
for rev, path, url in self._GetSubmodules():
2327+
for rev, path, url, shallow in self._GetSubmodules():
23182328
name = self.manifest.GetSubprojectName(self, path)
23192329
(
23202330
relpath,
@@ -2336,6 +2346,7 @@ def GetDerivedSubprojects(self):
23362346
review=self.remote.review,
23372347
revision=self.remote.revision,
23382348
)
2349+
clone_depth = 1 if shallow.lower() == "true" else None
23392350
subproject = Project(
23402351
manifest=self.manifest,
23412352
name=name,
@@ -2352,6 +2363,7 @@ def GetDerivedSubprojects(self):
23522363
sync_s=self.sync_s,
23532364
sync_tags=self.sync_tags,
23542365
parent=self,
2366+
clone_depth=clone_depth,
23552367
is_derived=True,
23562368
)
23572369
result.append(subproject)

0 commit comments

Comments
 (0)