Skip to content

Commit 2b8da46

Browse files
committed
builder: defer submodule detection to git
1 parent e252a4e commit 2b8da46

File tree

1 file changed

+25
-49
lines changed

1 file changed

+25
-49
lines changed

src/tito/builder/submodule_aware_builder.py

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
find_spec_like_file,
2828
get_commit_timestamp,
2929
)
30-
from tito.exception import TitoException
3130
from tito.tar import TarFixer
3231

3332

@@ -109,54 +108,31 @@ def run_git_archive(self, relative_git_dir, prefix, commit, dest_tar, subdir=Non
109108
"%s > /dev/null" % git_archive_cmd,
110109
)
111110

112-
# pylint: disable=too-many-locals, too-many-arguments, consider-using-f-string
113-
def _submodule_archives(self, relative_git_dir, prefix, commit, initial_tar,
114-
submodule_tree="."):
115-
with chdir(submodule_tree):
116-
submodules_cmd = "git config --file .gitmodules --get-regexp path"
117-
submodules_output = run_command(submodules_cmd)
118-
119-
# split submodules output on newline
120-
# then on tab, and the directory is the last entry
121-
submodules_list = [
122-
line.split(" ")[-1] for line in submodules_output.split("\n")
123-
]
124-
125-
# We ignore the hash in the sub modules list as we'll have to get the correct one
126-
# from the commit id in commit
127-
for submodule in submodules_list:
128-
with chdir(submodule_tree):
129-
submodule_git_dir = os.path.join(submodule, ".git")
130-
if not os.path.exists(submodule_git_dir):
131-
raise TitoException("%r path does not contain '.git'. "
132-
"Have you cloned the repository recursively?\n"
133-
"Try `git submodule update --init --recursive`!" %
134-
os.path.abspath(submodule))
135-
# to find the submodule shars:
136-
# git rev-parse <commit>:./<submodule>
137-
rev_parse_cmd = "git rev-parse %s:./%s" % (commit, submodule)
138-
submodule_commit = run_command(rev_parse_cmd)
139-
submodule_tar_file = "%s.%s" % (initial_tar, submodule.replace("/", "_"))
140-
# prefix should be <prefix>/<submodule>
141-
submodule_prefix = "%s/%s" % (prefix, submodule)
142-
143-
self.run_git_archive(
144-
relative_git_dir,
145-
submodule_prefix,
146-
submodule_commit,
147-
submodule_tar_file,
148-
submodule,
149-
)
150-
yield (submodule_tar_file)
151-
152-
submodule_dir = os.path.join(submodule_tree, submodule)
153-
gitmodules_path = os.path.join(submodule_dir, ".gitmodules")
154-
if os.path.exists(gitmodules_path):
155-
for archive in self._submodule_archives(
156-
relative_git_dir, submodule_prefix, submodule_commit,
157-
submodule_tar_file, submodule_dir
158-
):
159-
yield archive
111+
def _submodule_archives(self, relative_git_dir, prefix, initial_tar, source_tree="."):
112+
with chdir(source_tree):
113+
# Let git handle edge cases for .gitmodules (eg: empty files etc)
114+
submodules_status_cmd = "git submodule status --recursive"
115+
submodules_status_output = run_command(submodules_status_cmd)
116+
117+
for line in submodules_status_output.strip().split("\n"):
118+
row = line.split()
119+
submodule_commit, submodule_relative_dir = row[0], row[1]
120+
121+
submodule_tar_file_suffix = submodule_relative_dir.replace("/", "_")
122+
submodule_tar_file = f"{initial_tar}.{submodule_tar_file_suffix}"
123+
124+
# prefix should be <prefix>/<submodule>
125+
submodule_prefix = f"{prefix}/{submodule_relative_dir}"
126+
127+
self.run_git_archive(
128+
relative_git_dir,
129+
submodule_prefix,
130+
submodule_commit,
131+
submodule_tar_file,
132+
submodule_relative_dir,
133+
)
134+
135+
yield submodule_tar_file
160136

161137
def create_tgz(self, git_root, prefix, commit, relative_dir, dest_tgz):
162138
"""

0 commit comments

Comments
 (0)