Repository: pypa/setuptools_scm

Issue:
tarfile.ReadError: empty file - in filefinder on no commits
```
/usr/lib64/python3.7/distutils/dist.py:274: UserWarning: Unknown distribution option: 'test_requires'
  warnings.warn(msg)
/home/laura/dev/libs/rio-redis/.eggs/setuptools_scm-3.0.6-py3.7.egg/setuptools_scm/version.py:191: UserWarning: meta invoked without explicit configuration, will use defaults where required.
  "meta invoked without explicit configuration,"
running egg_info
writing rio_redis.egg-info/PKG-INFO
writing dependency_links to rio_redis.egg-info/dependency_links.txt
writing requirements to rio_redis.egg-info/requires.txt
writing top-level names to rio_redis.egg-info/top_level.txt
fatal: Not a valid object name
Traceback (most recent call last):
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 2287, in next
    tarinfo = self.tarinfo.fromtarfile(self)
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 1093, in fromtarfile
    obj = cls.frombuf(buf, tarfile.encoding, tarfile.errors)
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 1029, in frombuf
    raise EmptyHeaderError("empty header")
tarfile.EmptyHeaderError: empty header

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "setup.py", line 39, in <module>
    python_requires=">=3.6.0",
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/__init__.py", line 131, in setup
    return distutils.core.setup(**attrs)
  File "/usr/lib64/python3.7/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib64/python3.7/distutils/dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "/usr/lib64/python3.7/distutils/dist.py", line 985, in run_command
    cmd_obj.run()
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 278, in run
    self.find_sources()
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 293, in find_sources
    mm.run()
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 524, in run
    self.add_defaults()
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 563, in add_defaults
    rcfiles = list(walk_revctrl())
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/site-packages/setuptools/command/sdist.py", line 20, in walk_revctrl
    for item in ep.load()(dirname):
  File "/home/laura/dev/libs/rio-redis/.eggs/setuptools_scm-3.0.6-py3.7.egg/setuptools_scm/integration.py", line 33, in find_files
    res = command(path)
  File "/home/laura/dev/libs/rio-redis/.eggs/setuptools_scm-3.0.6-py3.7.egg/setuptools_scm/file_finder_git.py", line 47, in git_find_files
    git_files, git_dirs = _git_ls_files_and_dirs(toplevel)
  File "/home/laura/dev/libs/rio-redis/.eggs/setuptools_scm-3.0.6-py3.7.egg/setuptools_scm/file_finder_git.py", line 31, in _git_ls_files_and_dirs
    tf = tarfile.open(fileobj=proc.stdout, mode="r|*")
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 1601, in open
    t = cls(name, filemode, stream, **kwargs)
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 1482, in __init__
    self.firstmember = self.next()
  File "/home/laura/.local/share/virtualenvs/rio-redis-wQCA5cC-/lib/python3.7/tarfile.py", line 2302, in next
    raise ReadError("empty file")
tarfile.ReadError: empty file
```

Discussion:

Patch:
diff --git a/src/setuptools_scm/file_finder_git.py b/src/setuptools_scm/file_finder_git.py
index d886d06..b7b55ed 100644
--- a/src/setuptools_scm/file_finder_git.py
+++ b/src/setuptools_scm/file_finder_git.py
@@ -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):
@@ -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
@@ -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():
@@ -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"]
+    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:


Test patch:
diff --git a/testing/test_git.py b/testing/test_git.py
index d854a7c..f498db0 100644
--- a/testing/test_git.py
+++ b/testing/test_git.py
@@ -4,6 +4,7 @@ from setuptools_scm import git
 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
@@ -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)