builder: defer submodule detection to git

This commit is contained in:
Arun Babu Neelicattu 2024-11-06 14:50:16 +01:00 committed by Jakub Kadlčík
parent e252a4ea1e
commit b635baa305

View file

@ -27,7 +27,6 @@ from tito.common import (
find_spec_like_file, find_spec_like_file,
get_commit_timestamp, get_commit_timestamp,
) )
from tito.exception import TitoException
from tito.tar import TarFixer from tito.tar import TarFixer
@ -109,54 +108,31 @@ class SubmoduleAwareBuilder(Builder):
"%s > /dev/null" % git_archive_cmd, "%s > /dev/null" % git_archive_cmd,
) )
# pylint: disable=too-many-locals, too-many-arguments, consider-using-f-string def _submodule_archives(self, relative_git_dir, prefix, initial_tar, source_tree="."):
def _submodule_archives(self, relative_git_dir, prefix, commit, initial_tar, with chdir(source_tree):
submodule_tree="."): # Let git handle edge cases for .gitmodules (eg: empty files etc)
with chdir(submodule_tree): submodules_status_cmd = "git submodule status --recursive"
submodules_cmd = "git config --file .gitmodules --get-regexp path" submodules_status_output = run_command(submodules_status_cmd)
submodules_output = run_command(submodules_cmd)
# split submodules output on newline for line in submodules_status_output.strip().split("\n"):
# then on tab, and the directory is the last entry row = line.split()
submodules_list = [ submodule_commit, submodule_relative_dir = row[0], row[1]
line.split(" ")[-1] for line in submodules_output.split("\n")
]
# We ignore the hash in the sub modules list as we'll have to get the correct one submodule_tar_file_suffix = submodule_relative_dir.replace("/", "_")
# from the commit id in commit submodule_tar_file = "{0}.{1}".format(initial_tar, submodule_tar_file_suffix)
for submodule in submodules_list:
with chdir(submodule_tree):
submodule_git_dir = os.path.join(submodule, ".git")
if not os.path.exists(submodule_git_dir):
raise TitoException("%r path does not contain '.git'. "
"Have you cloned the repository recursively?\n"
"Try `git submodule update --init --recursive`!" %
os.path.abspath(submodule))
# to find the submodule shars:
# git rev-parse <commit>:./<submodule>
rev_parse_cmd = "git rev-parse %s:./%s" % (commit, submodule)
submodule_commit = run_command(rev_parse_cmd)
submodule_tar_file = "%s.%s" % (initial_tar, submodule.replace("/", "_"))
# prefix should be <prefix>/<submodule>
submodule_prefix = "%s/%s" % (prefix, submodule)
self.run_git_archive( # prefix should be <prefix>/<submodule>
relative_git_dir, submodule_prefix = "{0}/{1}".format(prefix, submodule_relative_dir)
submodule_prefix,
submodule_commit,
submodule_tar_file,
submodule,
)
yield (submodule_tar_file)
submodule_dir = os.path.join(submodule_tree, submodule) self.run_git_archive(
gitmodules_path = os.path.join(submodule_dir, ".gitmodules") relative_git_dir,
if os.path.exists(gitmodules_path): submodule_prefix,
for archive in self._submodule_archives( submodule_commit,
relative_git_dir, submodule_prefix, submodule_commit, submodule_tar_file,
submodule_tar_file, submodule_dir submodule_relative_dir,
): )
yield archive
yield submodule_tar_file
def create_tgz(self, git_root, prefix, commit, relative_dir, dest_tgz): def create_tgz(self, git_root, prefix, commit, relative_dir, dest_tgz):
""" """