Skip to main content

judge.scratch_worktree

judge.scratch_worktree(root, rev, module, candidate=None)

A detached worktree of rev in which the unit’s module is deleted or replaced.

The worktree lies in a temporary directory outside the repository, so scoring never writes into the working tree. Every phase of scoring takes a worktree of its own: what a candidate writes while it runs (a consumer, a conftest.py, a test file) dies with the worktree and cannot reach a later phase.

Parameters

Name Type Description Default
root Path Project root (the repository root, or a folder inside the repository). required
rev str The commit to check out. required
module str | Path Project-relative path of the module to delete. required
candidate Path | None A file to put at module instead, or None to leave it deleted. None

Returns

Name Type Description
Iterator[Path] AbstractContextManager[Path]: Entering it gives the project inside the worktree (the worktree itself for a project at the top of the repository); the worktree is removed again on exit, also after an error.

Examples

import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory

from babregen.judge import scratch_worktree

def git(root, *args):
    subprocess.run(
        ["git", "-c", "user.name=t", "-c", "user.email=t@example.org", *args],
        cwd=root, check=True, capture_output=True,
    )

with TemporaryDirectory() as tmp:
    root = Path(tmp) / "repo"
    root.mkdir()
    git(root, "init", "-q")
    (root / "m.py").write_text("X = 1\n")
    git(root, "add", ".")
    git(root, "commit", "-q", "-m", "init")
    with scratch_worktree(root, "HEAD", "m.py") as tree:
        print((tree / "m.py").exists(), tree.parent != root)
    print(tree.exists())
False True
False