Skip to content
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
27 changes: 20 additions & 7 deletions src/setuptools_scm/file_finder_git.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
import subprocess
import tarfile

import logging
from .file_finder import scm_find_files
from .utils import trace

log = logging.getLogger(__name__)


def _git_toplevel(path):
Expand All @@ -14,6 +17,7 @@ def _git_toplevel(path):
universal_newlines=True,
stderr=devnull,
)
trace("find files toplevel", out)
return os.path.normcase(os.path.realpath(out.strip()))
except subprocess.CalledProcessError:
# git returned error, we are not in a git repo
Expand All @@ -23,12 +27,8 @@ def _git_toplevel(path):
return None


def _git_ls_files_and_dirs(toplevel):
# use git archive instead of git ls-file to honor
# export-ignore git attribute
cmd = ["git", "archive", "--prefix", toplevel + os.path.sep, "HEAD"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=toplevel)
tf = tarfile.open(fileobj=proc.stdout, mode="r|*")
def _git_interpret_archive(fd, toplevel):
tf = tarfile.open(fileobj=fd, mode="r|*")
git_files = set()
git_dirs = {toplevel}
for member in tf.getmembers():
Expand All @@ -40,6 +40,19 @@ def _git_ls_files_and_dirs(toplevel):
return git_files, git_dirs


def _git_ls_files_and_dirs(toplevel):
# use git archive instead of git ls-file to honor
# export-ignore git attribute
cmd = ["git", "archive", "--prefix", toplevel + os.path.sep, "HEAD"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively output of ls-files can be checked with check-attr.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be wrothwhile to turn this into a pr - and whats the mechanics of that?

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=toplevel)
try:
return _git_interpret_archive(proc.stdout, toplevel)
except Exception:
if proc.wait() != 0:
log.exception("listing git files failed - pretending there aren't any")
return (), ()


def git_find_files(path=""):
toplevel = _git_toplevel(path)
if not toplevel:
Expand Down
9 changes: 9 additions & 0 deletions testing/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from datetime import date
from os.path import join as opj
from setuptools_scm.file_finder_git import git_find_files


@pytest.fixture
Expand All @@ -28,6 +29,14 @@ def test_parse_describe_output(given, tag, number, node, dirty):
assert parsed == (tag, number, node, dirty)


@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/298")
def test_file_finder_no_history(wd, caplog):
file_list = git_find_files(str(wd.cwd))
assert file_list == []

assert "listing git files failed - pretending there aren't any" in caplog.text


@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/281")
def test_parse_call_order(wd):
git.parse(str(wd.cwd), git.DEFAULT_DESCRIBE)
Expand Down