Skip to content

Commit

Permalink
Use git to get list of submodules
Browse files Browse the repository at this point in the history
Parsing .gitmodules may yield paths that are ignored by git.
Refs #85
  • Loading branch information
Kentzo committed Dec 4, 2020
1 parent ddece8a commit 28df74b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
55 changes: 35 additions & 20 deletions git_archive_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,30 +345,19 @@ def walk_git_files(self, repo_path=fspath('')):
self.run_git_shell('git submodule init', repo_abspath)
self.run_git_shell('git submodule update', repo_abspath)

try:
repo_gitmodules_abspath = path.join(repo_abspath, fspath(".gitmodules"))

with open(repo_gitmodules_abspath) as f:
lines = f.readlines()

for l in lines:
m = re.match("^\\s*path\\s*=\\s*(.*)\\s*$", l)
for repo_submodule_path in self.list_repo_submodules(repo_abspath): # relative to repo_path
if self.is_file_excluded(repo_abspath, repo_submodule_path):
continue

if m:
repo_submodule_path = fspath(m.group(1)) # relative to repo_path
main_repo_submodule_path = path.join(repo_path, repo_submodule_path) # relative to main_repo_abspath
main_repo_submodule_path = path.join(repo_path, repo_submodule_path) # relative to main_repo_abspath

if self.is_file_excluded(repo_abspath, repo_submodule_path):
continue
for main_repo_submodule_file_path in self.walk_git_files(main_repo_submodule_path):
repo_submodule_file_path = path.relpath(main_repo_submodule_file_path, repo_path) # relative to repo_path

for main_repo_submodule_file_path in self.walk_git_files(main_repo_submodule_path):
repo_submodule_file_path = path.relpath(main_repo_submodule_file_path, repo_path) # relative to repo_path
if self.is_file_excluded(repo_abspath, repo_submodule_file_path):
continue
if self.is_file_excluded(repo_abspath, repo_submodule_file_path):
continue

yield main_repo_submodule_file_path
except IOError:
pass
yield main_repo_submodule_file_path
finally:
self._check_attr_gens[repo_abspath].close()
del self._check_attr_gens[repo_abspath]
Expand Down Expand Up @@ -538,6 +527,9 @@ def get_git_version(cls):

@classmethod
def list_repo_files(cls, repo_abspath):
"""
Return a list of all files as seen by git in a given repo.
"""
repo_file_paths = cls.run_git_shell(
'git ls-files -z --cached --full-name --no-empty-directory',
cwd=repo_abspath
Expand All @@ -551,6 +543,29 @@ def list_repo_files(cls, repo_abspath):

return repo_file_paths

@classmethod
def list_repo_submodules(cls, repo_abspath):
"""
Return a list of all direct submodules as seen by git in a given repo.
"""
if sys.platform.startswith('win32'):
shell_command = 'git submodule foreach --quiet "\\"{0}\\" -c \\"from __future__ import print_function; print(\'"$sm_path"\', end=chr(0))\\""'
else:
shell_command = 'git submodule foreach --quiet \'"{0}" -c "from __future__ import print_function; print(\\"$sm_path\\", end=chr(0))"\''

python_exe = sys.executable or 'python'
shell_command = shell_command.format(python_exe)

repo_submodule_paths = cls.run_git_shell(shell_command, cwd=repo_abspath)
repo_submodule_paths = repo_submodule_paths.split(b'\0')[:-1]

if sys.platform.startswith('win32'):
repo_submodule_paths = (git_fspath(p.replace(b'/', b'\\')) for p in repo_submodule_paths)
else:
repo_submodule_paths = map(git_fspath, repo_submodule_paths)

return repo_submodule_paths


def main(argv=None):
if argv is None:
Expand Down
5 changes: 4 additions & 1 deletion git_archive_all.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ class GitArchiver(object):
def get_git_version(cls) -> Optional[Tuple[int]]: ...

@classmethod
def list_repo_files(cls, repo_abspath: PathStr) -> Generator[PathStr, None, None]: ...
def list_repo_files(cls, repo_abspath: PathStr) -> Generator[PathStr, None, None]: ...

@classmethod
def list_repo_submodules(cls, repo_abspath: PathStr) -> Generator[PathStr, None, None]: ...

0 comments on commit 28df74b

Please sign in to comment.