Skip to content

Commit ff88ecd

Browse files
Merge pull request #602 from RonnyPfannschmidt/fix-587-file-finding-no-commit
fix #587 - stop file finding on missing commits
2 parents e214250 + 352d958 commit ff88ecd

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
In progress
22
===========
33

4+
* fix #587: don't fail file finders when distribution is not given
45
* fix #524: new parameters ``normalize`` and ``version_cls`` to customize the version normalization class.
56
* fix #585: switch from toml to tomli
67

src/setuptools_scm/file_finder_git.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .file_finder import is_toplevel_acceptable
77
from .file_finder import scm_find_files
8+
from .utils import do_ex
89
from .utils import trace
910

1011
log = logging.getLogger(__name__)
@@ -13,13 +14,17 @@
1314
def _git_toplevel(path):
1415
try:
1516
cwd = os.path.abspath(path or ".")
16-
with open(os.devnull, "wb") as devnull:
17-
out = subprocess.check_output(
18-
["git", "rev-parse", "--show-prefix"],
19-
cwd=cwd,
20-
universal_newlines=True,
21-
stderr=devnull,
22-
)
17+
out, err, ret = do_ex(["git", "rev-parse", "HEAD"], cwd=cwd)
18+
if ret != 0:
19+
# BAIL if there is no commit
20+
log.error("listing git files failed - pretending there aren't any")
21+
return None
22+
out, err, ret = do_ex(
23+
["git", "rev-parse", "--show-prefix"],
24+
cwd=cwd,
25+
)
26+
if ret != 0:
27+
return None
2328
out = out.strip()[:-1] # remove the trailing pathsep
2429
if not out:
2530
out = cwd
@@ -28,7 +33,7 @@ def _git_toplevel(path):
2833
# ``cwd`` is absolute path to current working directory.
2934
# the below method removes the length of ``out`` from
3035
# ``cwd``, which gives the git toplevel
31-
assert cwd.replace("\\", "/").endswith(out)
36+
assert cwd.replace("\\", "/").endswith(out), f"cwd={cwd!r}\nout={out!r}"
3237
# In windows cwd contains ``\`` which should be replaced by ``/``
3338
# for this assertion to work. Length of string isn't changed by replace
3439
# ``\\`` is just and escape for `\`
@@ -59,8 +64,11 @@ def _git_interpret_archive(fd, toplevel):
5964
def _git_ls_files_and_dirs(toplevel):
6065
# use git archive instead of git ls-file to honor
6166
# export-ignore git attribute
67+
6268
cmd = ["git", "archive", "--prefix", toplevel + os.path.sep, "HEAD"]
63-
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=toplevel)
69+
proc = subprocess.Popen(
70+
cmd, stdout=subprocess.PIPE, cwd=toplevel, stderr=subprocess.DEVNULL
71+
)
6472
try:
6573
try:
6674
return _git_interpret_archive(proc.stdout, toplevel)
@@ -70,7 +78,7 @@ def _git_ls_files_and_dirs(toplevel):
7078
proc.terminate()
7179
except Exception:
7280
if proc.wait() != 0:
73-
log.exception("listing git files failed - pretending there aren't any")
81+
log.error("listing git files failed - pretending there aren't any")
7482
return (), ()
7583

7684

src/setuptools_scm/file_finder_hg.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .file_finder import is_toplevel_acceptable
55
from .file_finder import scm_find_files
6+
from .utils import do_ex
67

78

89
def _hg_toplevel(path):
@@ -26,9 +27,9 @@ def _hg_toplevel(path):
2627
def _hg_ls_files_and_dirs(toplevel):
2728
hg_files = set()
2829
hg_dirs = {toplevel}
29-
out = subprocess.check_output(
30-
["hg", "files"], cwd=toplevel, universal_newlines=True
31-
)
30+
out, err, ret = do_ex(["hg", "files"], cwd=toplevel)
31+
if ret:
32+
(), ()
3233
for name in out.splitlines():
3334
name = os.path.normcase(name).replace("/", os.path.sep)
3435
fullname = os.path.join(toplevel, name)

testing/test_file_finder.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def inwd(request, wd, monkeypatch):
3131
bdir = wd.cwd / "bdir"
3232
bdir.mkdir()
3333
(bdir / "fileb").touch()
34-
wd.add_and_commit()
34+
if request.node.get_closest_marker("skip_commit") is None:
35+
wd.add_and_commit()
3536
monkeypatch.chdir(wd.cwd)
3637
yield wd
3738

@@ -184,3 +185,9 @@ def test_symlink_not_in_scm_while_target_is(inwd):
184185
"data/datafile",
185186
}
186187
)
188+
189+
190+
@pytest.mark.issue(587)
191+
@pytest.mark.skip_commit
192+
def test_not_commited(inwd):
193+
assert find_files() == []

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ testpaths=testing
66
filterwarnings=error
77
markers=
88
issue(id): reference to github issue
9+
skip_commit: allows to skip commiting in the helpers
910
# disable unraisable until investigated
1011
addopts = -p no:unraisableexception
1112

0 commit comments

Comments
 (0)