import subprocess import tempfile import shutil from pathlib import Path def fetch_path(repo_url: str, repo_path: str, dest: Path, ref: str = "HEAD", force: bool = False): with tempfile.TemporaryDirectory(prefix="jeaddons-") as tmp: tmp_path = Path(tmp) subprocess.check_call( ["git", "clone", "--filter=blob:none", "--no-checkout", "--depth", "1", repo_url, str(tmp_path)] ) subprocess.check_call(["git", "-C", str(tmp_path), "sparse-checkout", "init", "--cone"]) subprocess.check_call(["git", "-C", str(tmp_path), "sparse-checkout", "set", repo_path]) subprocess.check_call(["git", "-C", str(tmp_path), "checkout", ref]) src = tmp_path / repo_path if not src.exists(): raise FileNotFoundError(repo_path) target = dest / src.name if target.exists(): if not force: raise FileExistsError(target) if target.is_dir(): shutil.rmtree(target) else: target.unlink() if src.is_dir(): shutil.copytree(src, target) return target shutil.copy2(src, target) return target