Skip to content

Commit

Permalink
Fix --no-exclude is ignored.
Browse files Browse the repository at this point in the history
Refs #82
  • Loading branch information
Kentzo committed Jul 21, 2020
1 parent dfaa08e commit b4c5679
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
3 changes: 3 additions & 0 deletions git_archive_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 28 additions & 10 deletions test_git_archive_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def git_env(tmpdir_factory):
'email = [email protected]\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'
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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.
"""
Expand All @@ -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
Expand Down

0 comments on commit b4c5679

Please sign in to comment.