Skip to content

Commit d4fdd71

Browse files
committed
Only copy files not ignored by git for local build
1 parent a05ba42 commit d4fdd71

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

sphinx_polyversion/driver.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from inspect import isawaitable
1212
from logging import getLogger
1313
from pathlib import Path
14+
from subprocess import CalledProcessError
1415
from types import TracebackType
1516
from typing import (
1617
TYPE_CHECKING,
@@ -33,6 +34,7 @@
3334

3435
from sphinx_polyversion.builder import Builder, BuildError
3536
from sphinx_polyversion.environment import Environment
37+
from sphinx_polyversion.git import get_unignored_files
3638
from sphinx_polyversion.json import GLOBAL_ENCODER, Encoder, JSONable
3739
from sphinx_polyversion.utils import shift_path
3840

@@ -333,8 +335,20 @@ async def build_local(self) -> None:
333335
with tempfile.TemporaryDirectory() as tmp:
334336
path = Path(tmp)
335337
# copy source files
336-
logger.info("Copying source files...")
337-
shutil.copytree(self.root, path, symlinks=True, dirs_exist_ok=True)
338+
logger.info("Copying source files (except for files ignored by git)...")
339+
try:
340+
files = await get_unignored_files(self.root)
341+
for filename in files:
342+
source = self.root / filename
343+
target = path / filename
344+
target.parent.mkdir(parents=True, exist_ok=True)
345+
if not target.exists():
346+
shutil.copy2(source, target, follow_symlinks=False)
347+
except CalledProcessError:
348+
logger.warning(
349+
"Could not list un-ignored files using git. Copying full working directory..."
350+
)
351+
shutil.copytree(self.root, path, symlinks=True, dirs_exist_ok=True)
338352
# setup build environment (e.g. poetry/pip venv)
339353
async with await self.init_environment(path, rev) as env:
340354
# construct metadata to pass to the build process

sphinx_polyversion/git.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,34 @@ async def file_exists(repo: Path, ref: GitRef, file: PurePath) -> bool:
258258
return rc == 0
259259

260260

261+
async def get_unignored_files(directory: Path) -> list[Path]:
262+
"""
263+
List all unignored files in the directory.
264+
265+
Parameters
266+
----------
267+
directory : Path
268+
Any directory in the repo.
269+
270+
Returns
271+
-------
272+
Path
273+
The paths to all un-ignored files in the directory (recursive)
274+
275+
"""
276+
cmd = (
277+
"git",
278+
"ls-files",
279+
"--cached",
280+
"--others",
281+
"--exclude-standard",
282+
)
283+
process = await asyncio.create_subprocess_exec(*cmd, cwd=directory, stdout=PIPE)
284+
out, err = await process.communicate()
285+
files = out.decode().split("\n")
286+
return [Path(path.strip()) for path in files]
287+
288+
261289
# -- VersionProvider API -----------------------------------------------------
262290

263291

0 commit comments

Comments
 (0)