Skip to content

Commit

Permalink
Prevent matching bogus parent git directories
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jameshilliard committed Jul 26, 2024
1 parent e38b172 commit 0b285ba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion flit/vcs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import subprocess
from pathlib import Path

from . import hg
from . import git

def git_validate_ignore(directory: Path) -> bool:
check_ignore = subprocess.run(
['git', 'check-ignore', '.'],
cwd=str(directory),
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
).returncode
if check_ignore == 0:
return False
return True

def identify_vcs(directory: Path):
directory = directory.resolve()
for p in [directory] + list(directory.parents):
if (p / '.git').is_dir():
if (p / '.git').is_dir() and git_validate_ignore(directory):
return git
if (p / '.hg').is_dir():
return hg
Expand Down
4 changes: 3 additions & 1 deletion tests/test_sdist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ast
from os.path import join as pjoin
from pathlib import Path
from unittest.mock import patch
import pytest
from shutil import which, copy, copytree
import sys
Expand Down Expand Up @@ -81,7 +82,8 @@ def test_get_files_list_git(copy_sample):

builder = sdist.SdistBuilder.from_ini_path(td / 'pyproject.toml')
with MockCommand('git', LIST_FILES_GIT):
files = builder.select_files()
with patch('flit.vcs.git_validate_ignore', return_value=True):
files = builder.select_files()

assert set(files) == {
'foo', pjoin('dir1', 'bar'), pjoin('dir1', 'subdir', 'qux'),
Expand Down

0 comments on commit 0b285ba

Please sign in to comment.