Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sphinx_polyversion/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ async def build_local(self) -> None:
path = Path(tmp)
# copy source files
logger.info("Copying source files...")
shutil.copytree(self.root, path, symlinks=True, dirs_exist_ok=True)
await self.vcs.checkout_local(self.root, path)
# setup build environment (e.g. poetry/pip venv)
async with await self.init_environment(path, rev) as env:
# construct metadata to pass to the build process
Expand Down
65 changes: 65 additions & 0 deletions sphinx_polyversion/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import asyncio
import enum
import re
import shutil
import tarfile
import tempfile
from asyncio.subprocess import PIPE
Expand Down Expand Up @@ -210,6 +211,7 @@ async def _copy_tree(

"""
# retrieve commit contents as tar archive
# NOTE: doesn't support git submodules
cmd = ("git", "archive", "--format", "tar", ref)
with tempfile.SpooledTemporaryFile(max_size=buffer_size) as f:
process = await asyncio.create_subprocess_exec(
Expand Down Expand Up @@ -259,6 +261,39 @@ async def file_exists(repo: Path, ref: GitRef, file: PurePath) -> bool:
return rc == 0


async def _get_unignored_files(directory: Path) -> AsyncGenerator[Path, None]:
"""
List all unignored files in the directory.

This uses git to retrieve all tracked and untracked files but excludes
files ignored e.g. by `.gitignore`.

Parameters
----------
directory : Path
Any directory in the repo.

Returns
-------
AsyncGenerator[Path, None]
The paths to all un-ignored files in the directory (recursive)

"""
cmd = (
"git",
"ls-files",
"--cached",
"--others",
"--exclude-standard",
)
process = await asyncio.create_subprocess_exec(*cmd, cwd=directory, stdout=PIPE)
while line := await process.stdout.readline(): # type: ignore[union-attr]
yield Path(line.strip().decode())
await process.wait()
if process.returncode:
raise CalledProcessError(process.returncode, " ".join(cmd))


# -- VersionProvider API -----------------------------------------------------


Expand Down Expand Up @@ -401,6 +436,10 @@ class Git(VersionProvider[GitRef]):
"""
Provide versions from git repository.

.. warning::

Currently git submodules aren't supported. Feel free to open an issue!

Parameters
----------
branch_regex : str | re.Pattern
Expand Down Expand Up @@ -509,6 +548,32 @@ async def checkout(self, root: Path, dest: Path, revision: GitRef) -> None:
"""
await _copy_tree(root, revision.obj, dest, self.buffer_size)

async def checkout_local(self, root: Path, dest: Path) -> None:
"""
Create copy of the local working directory at the given path.

Parameters
----------
root : Path
The root path of the project.
dest : Path
The destination to extract the revision to.

"""
try:
# NOTE: doesn't support git submodules
async for file in _get_unignored_files(root):
source = root / file
target = dest / file
target.parent.mkdir(parents=True, exist_ok=True)
assert source.exists()
shutil.copy2(source, target, follow_symlinks=False)
except CalledProcessError:
logger.warning(
"Could not list un-ignored files using git. Copying full working directory..."
)
shutil.copytree(root, dest, symlinks=True, dirs_exist_ok=True)

async def predicate(self, root: Path, ref: GitRef) -> bool:
"""
Check whether a revision should be build.
Expand Down
14 changes: 14 additions & 0 deletions sphinx_polyversion/vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ async def checkout(self, root: Path, dest: Path, revision: RT) -> None:

"""

@abstractmethod
async def checkout_local(self, root: Path, dest: Path) -> None:
"""
Create copy of the local working directory at the given path.

Parameters
----------
root : Path
The root path of the project.
dest : Path
The destination to extract the revision to.

"""

@abstractmethod
async def retrieve(self, root: Path) -> Iterable[RT]:
"""
Expand Down