Skip to content

Commit 730aaf4

Browse files
Merge pull request #299 from RonnyPfannschmidt/fix-298
Fix 298 - handle no commits for file listing
2 parents 7ec3119 + 1487d5b commit 730aaf4

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/setuptools_scm/file_finder_git.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import os
22
import subprocess
33
import tarfile
4-
4+
import logging
55
from .file_finder import scm_find_files
6+
from .utils import trace
7+
8+
log = logging.getLogger(__name__)
69

710

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

2529

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

4242

43+
def _git_ls_files_and_dirs(toplevel):
44+
# use git archive instead of git ls-file to honor
45+
# export-ignore git attribute
46+
cmd = ["git", "archive", "--prefix", toplevel + os.path.sep, "HEAD"]
47+
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=toplevel)
48+
try:
49+
return _git_interpret_archive(proc.stdout, toplevel)
50+
except Exception:
51+
if proc.wait() != 0:
52+
log.exception("listing git files failed - pretending there aren't any")
53+
return (), ()
54+
55+
4356
def git_find_files(path=""):
4457
toplevel = _git_toplevel(path)
4558
if not toplevel:

testing/test_git.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
from datetime import date
66
from os.path import join as opj
7+
from setuptools_scm.file_finder_git import git_find_files
78

89

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

3031

32+
@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/298")
33+
def test_file_finder_no_history(wd, caplog):
34+
file_list = git_find_files(str(wd.cwd))
35+
assert file_list == []
36+
37+
assert "listing git files failed - pretending there aren't any" in caplog.text
38+
39+
3140
@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/281")
3241
def test_parse_call_order(wd):
3342
git.parse(str(wd.cwd), git.DEFAULT_DESCRIBE)

0 commit comments

Comments
 (0)