Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get_versioned_files() failing when GIT_INDEX_FILE is set #123

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Changelog
0.43 (unreleased)
-----------------

- Nothing changed yet.
- Fix collecting files versioned by ``git`` when a project has submodules and
``GIT_INDEX_FILE`` is set. This bug was triggered when ``check-manifest``
was run as part of a git hook (
`#122 <https://github.com/mgedmin/check-manifest/issues/122>`__,
`#123 <https://github.com/mgedmin/check-manifest/pull/123>`__).


0.42 (2020-05-03)
Expand Down
39 changes: 5 additions & 34 deletions check_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,6 @@ def strip_toplevel_name(filelist):
return [name[len(prefix):] for name in names if name != prefix]


def add_prefix_to_each(prefix, filelist):
"""Add a prefix to each name in a file list.

>>> add_prefix_to_each('foo/bar', ['a', 'b', 'c/d'])
['foo/bar/a', 'foo/bar/b', 'foo/bar/c/d']

"""
return [posixpath.join(prefix, name) for name in filelist]


class VCS:

def __init__(self, ui):
Expand Down Expand Up @@ -406,33 +396,14 @@ def detect(cls, location):

def get_versioned_files(self):
"""List all files versioned by git in the current directory."""
files = self._git_ls_files()
submodules = self._list_submodules()
for subdir in submodules:
subdir = os.path.relpath(subdir).replace(os.path.sep, '/')
files += add_prefix_to_each(subdir, self._git_ls_files(subdir))
mgedmin marked this conversation as resolved.
Show resolved Hide resolved
return files

@classmethod
def _git_ls_files(cls, cwd=None):
output = run(['git', 'ls-files', '-z'], encoding=cls._encoding, cwd=cwd)
output = run(
["git", "ls-files", "-z", "--recurse-submodules"],
encoding=self._encoding,
)
# -z tells git to use \0 as a line terminator; split() treats it as a
# line separator, so we always get one empty line at the end, which we
# drop with the [:-1] slice
return output.split('\0')[:-1]

@classmethod
def _list_submodules(cls):
# This is incredibly expensive on my Jenkins instance (50 seconds for
# each invocation, even when there are no submodules whatsoever).
# Curiously, I cannot reproduce that in Appveyor, or even on the same
# Jenkins machine but when I run the tests manually. Still, 2-hour
# Jenkins runs are bad, so let's avoid running 'git submodule' when
# there's no .gitmodules file.
if not os.path.exists('.gitmodules'):
return []
return run(['git', 'submodule', '--quiet', 'foreach', '--recursive',
'printf "%s/%s\\n" $toplevel $path'], encoding=cls._encoding).splitlines()
return output.split("\0")[:-1]


class Mercurial(VCS):
Expand Down
4 changes: 4 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,10 @@ def test_get_versioned_files_with_git_submodules(self):
'subdir/sub2/sub3/file4',
])

def test_get_versioned_files_with_git_submodules_with_git_index_file_set(self):
with mock.patch.dict(os.environ, {"GIT_INDEX_FILE": ".git/index"}):
self.test_get_versioned_files_with_git_submodules()


class BzrHelper(VCSHelper):

Expand Down