From b4c5679dc814d6c328b0796514a8c0fd2bbec4e0 Mon Sep 17 00:00:00 2001 From: Ilya Kulakov Date: Wed, 22 Jul 2020 01:05:39 +0600 Subject: [PATCH] Fix --no-exclude is ignored. Refs #82 --- git_archive_all.py | 3 +++ test_git_archive_all.py | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/git_archive_all.py b/git_archive_all.py index 9d21257..8a727dd 100755 --- a/git_archive_all.py +++ b/git_archive_all.py @@ -272,6 +272,9 @@ def is_file_excluded(self, repo_abspath, repo_file_path): @return: True if file should be excluded. Otherwise False. """ + if not self.exclude: + return False + cache = self._ignored_paths_cache.setdefault(repo_abspath, {}) if repo_file_path not in cache: diff --git a/test_git_archive_all.py b/test_git_archive_all.py index 76d2e80..b16413f 100644 --- a/test_git_archive_all.py +++ b/test_git_archive_all.py @@ -63,6 +63,11 @@ def git_env(tmpdir_factory): 'email = git-archive-all@example.com\n', ]) + # .gitmodules's content is dynamic and is maintained by git. + # It's therefore ignored solely to simplify tests. + # + # If test is run with the --no-exclude CLI option (or its exclude=False API equivalent) + # then the file itself is included while its content is discarded for the same reason. with tmpdir_factory.getbasetemp().join('.gitattributes').open('w+') as f: f.writelines([ '.gitmodules export-ignore' @@ -138,19 +143,23 @@ def add_submodule(self, rel_path, contents): def commit(self, message): check_call(['git', 'commit', '-m', 'init'], cwd=self.path) - def archive(self, path): - a = GitArchiver(main_repo_abspath=self.path) + def archive(self, path, exclude=True): + a = GitArchiver(exclude=exclude, main_repo_abspath=self.path) a.create(path) -def make_expected_tree(contents): +def make_expected_tree(contents, exclude=True): e = {} for k, v in contents.items(): - if v.kind == 'file' and not v.excluded: + if v.kind == 'file' and not (exclude and v.excluded): e[k] = v.contents - elif v.kind in ('dir', 'submodule') and not v.excluded: - for nested_k, nested_v in make_expected_tree(v.contents).items(): + elif v.kind in ('dir', 'submodule') and not (exclude and v.excluded): + # See the comment in git_env. + if v.kind == 'submodule' and not exclude: + e['.gitmodules'] = None + + for nested_k, nested_v in make_expected_tree(v.contents, exclude).items(): nested_k = as_posix(os_path_join(k, nested_k)) e[nested_k] = nested_v @@ -163,7 +172,12 @@ def make_actual_tree(tar_file): for m in tar_file.getmembers(): if m.isfile(): name = fspath(m.name) - a[name] = tar_file.extractfile(m).read().decode() + + # See the comment in git_env. + if not name.endswith(fspath('.gitmodules')): + a[name] = tar_file.extractfile(m).read().decode() + else: + a[name] = None else: raise NotImplementedError @@ -362,7 +376,11 @@ def make_actual_tree(tar_file): pytest.param(non_unicode_backslash_quoted, id='Non-Unicode Backslash (Quoted)', marks=[skipif_file_win32, skipif_file_darwin]), pytest.param(ignore_dir, id='Ignore Directory') ]) -def test_ignore(contents, tmpdir, git_env, monkeypatch): +@pytest.mark.parametrize('exclude', [ + pytest.param(True, id='With export-ignore'), + pytest.param(False, id='Without export-ignore'), +]) +def test_ignore(contents, exclude, tmpdir, git_env, monkeypatch): """ Ensure that GitArchiver respects export-ignore. """ @@ -376,10 +394,10 @@ def test_ignore(contents, tmpdir, git_env, monkeypatch): repo.commit('init') repo_tar_path = os_path_join(tmpdir.strpath, 'repo.tar') - repo.archive(repo_tar_path) + repo.archive(repo_tar_path, exclude=exclude) repo_tar = TarFile(repo_tar_path, format=PAX_FORMAT, encoding='utf-8') - expected = make_expected_tree(contents) + expected = make_expected_tree(contents, exclude) actual = make_actual_tree(repo_tar) assert actual == expected