Skip to content

Commit 0b285ba

Browse files
committed
Prevent matching bogus parent git directories
If our current directory is not tracked by git we should assume that the project does not use git as the vcs as any match will be a false positive.
1 parent e38b172 commit 0b285ba

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

flit/vcs/__init__.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
import subprocess
12
from pathlib import Path
23

34
from . import hg
45
from . import git
56

7+
def git_validate_ignore(directory: Path) -> bool:
8+
check_ignore = subprocess.run(
9+
['git', 'check-ignore', '.'],
10+
cwd=str(directory),
11+
stderr=subprocess.DEVNULL,
12+
stdout=subprocess.DEVNULL,
13+
).returncode
14+
if check_ignore == 0:
15+
return False
16+
return True
17+
618
def identify_vcs(directory: Path):
719
directory = directory.resolve()
820
for p in [directory] + list(directory.parents):
9-
if (p / '.git').is_dir():
21+
if (p / '.git').is_dir() and git_validate_ignore(directory):
1022
return git
1123
if (p / '.hg').is_dir():
1224
return hg

tests/test_sdist.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ast
22
from os.path import join as pjoin
33
from pathlib import Path
4+
from unittest.mock import patch
45
import pytest
56
from shutil import which, copy, copytree
67
import sys
@@ -81,7 +82,8 @@ def test_get_files_list_git(copy_sample):
8182

8283
builder = sdist.SdistBuilder.from_ini_path(td / 'pyproject.toml')
8384
with MockCommand('git', LIST_FILES_GIT):
84-
files = builder.select_files()
85+
with patch('flit.vcs.git_validate_ignore', return_value=True):
86+
files = builder.select_files()
8587

8688
assert set(files) == {
8789
'foo', pjoin('dir1', 'bar'), pjoin('dir1', 'subdir', 'qux'),

0 commit comments

Comments
 (0)