From 1443832a4f50170b40c29c5298fce8c6172936db Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Mon, 10 Feb 2020 00:02:31 +0600 Subject: [PATCH] Refactor listing repo files into a dedicated method. --- git_archive_all.py | 29 +++++++++++++++++------------ git_archive_all.pyi | 5 ++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/git_archive_all.py b/git_archive_all.py index 0a13560..7ea344e 100755 --- a/git_archive_all.py +++ b/git_archive_all.py @@ -300,29 +300,19 @@ def walk_git_files(self, repo_path=fspath('')): @param repo_path: Path to the git submodule repository relative to main_repo_abspath. - @return: Iterator to traverse files under git control relative to main_repo_abspath. + @return: Generator to traverse files under git control relative to main_repo_abspath. """ repo_abspath = path.join(self.main_repo_abspath, fspath(repo_path)) assert repo_abspath not in self._check_attr_gens self._check_attr_gens[repo_abspath] = self.check_git_attr(repo_abspath, ['export-ignore']) try: - repo_file_paths = self.run_git_shell( - 'git ls-files -z --cached --full-name --no-empty-directory', - cwd=repo_abspath - ) - repo_file_paths = repo_file_paths.split(b'\0')[:-1] - - if sys.platform.startswith('win32'): - repo_file_paths = (git_fspath(p.replace(b'/', b'\\')) for p in repo_file_paths) - else: - repo_file_paths = map(git_fspath, repo_file_paths) + repo_file_paths = self.list_repo_files(repo_abspath) for repo_file_path in repo_file_paths: repo_file_abspath = path.join(repo_abspath, repo_file_path) # absolute file path main_repo_file_path = path.join(repo_path, repo_file_path) # relative to main_repo_abspath - # Only list symlinks and files. if not path.islink(repo_file_abspath) and path.isdir(repo_file_abspath): continue @@ -526,6 +516,21 @@ def get_git_version(cls): cls.LOG.warning("Unable to parse Git version \"%s\".", version) return None + @classmethod + def list_repo_files(cls, repo_abspath): + repo_file_paths = cls.run_git_shell( + 'git ls-files -z --cached --full-name --no-empty-directory', + cwd=repo_abspath + ) + repo_file_paths = repo_file_paths.split(b'\0')[:-1] + + if sys.platform.startswith('win32'): + repo_file_paths = (git_fspath(p.replace(b'/', b'\\')) for p in repo_file_paths) + else: + repo_file_paths = map(git_fspath, repo_file_paths) + + return repo_file_paths + def main(argv=None): if argv is None: diff --git a/git_archive_all.pyi b/git_archive_all.pyi index bec1678..860c25f 100644 --- a/git_archive_all.pyi +++ b/git_archive_all.pyi @@ -59,4 +59,7 @@ class GitArchiver(object): def run_git_shell(cls, cmd: str, cwd: PathStr = None) -> bytes: ... @classmethod - def get_git_version(cls) -> Optional[Tuple[int]]: ... \ No newline at end of file + def get_git_version(cls) -> Optional[Tuple[int]]: ... + + @classmethod + def list_repo_files(cls, repo_abspath: PathStr) -> Generator[PathStr, None, None]: ... \ No newline at end of file