CrossSWE-Bench / scripts /fetch_sources.py
Silin-Chen's picture
CrossSWE-Bench: current 171-instance release
0e239c6
Raw History Blame Contribute Delete
2.82 kB
"""Fetch pinned base commits into an evaluator-owned bare cache."""
import argparse
from pathlib import Path
import re
import subprocess
from common import ROOT, contained, load_task, run, sha256
from source_construction import construct
def fetch(task, output):
output = Path(output).resolve()
output.mkdir(parents=True, exist_ok=True)
result = {}
for repo in task["repositories"]:
commit = repo["base_commit"]
if not re.fullmatch(r"[0-9a-f]{40}", commit):
raise ValueError(f"Invalid commit for {repo['key']}: {commit}")
cache = contained(output, repo["key"] + ".git")
if not cache.exists():
run("git", "init", "--bare", str(cache))
run("git", "-C", str(cache), "config", "remote.origin.url", repo["url"])
bare = run("git", "-C", str(cache), "rev-parse", "--is-bare-repository", capture=True).strip()
origin = run("git", "-C", str(cache), "config", "--get", "remote.origin.url", capture=True).strip()
if bare != "true" or origin != repo["url"]:
raise ValueError(f"Cache identity differs from pinned repository: {cache}")
exists = subprocess.run(["git", "-C", str(cache), "cat-file", "-e", commit + "^{commit}"],
stderr=subprocess.DEVNULL).returncode == 0
if not exists:
recipe = repo["source_policy"].get("construction_recipe")
if recipe:
task_root = contained(ROOT / "instances", task["instance_id"])
path = contained(task_root, recipe["path"])
if sha256(path) != recipe["sha256"]:
raise ValueError("Source construction recipe changed")
construct(repo, cache, path)
else:
try:
run("git", "-C", str(cache), "fetch", "--depth=1", "--no-tags", repo["url"], commit)
except subprocess.CalledProcessError as error:
raise RuntimeError(f"Could not obtain exact base for {repo['key']}. Inspect source_policy and provenance.json for synthetic base construction; no revision substitution is performed.") from error
actual = run("git", "-C", str(cache), "rev-parse", commit + "^{commit}", capture=True).strip()
if actual != commit:
raise ValueError(f"Unexpected source revision: {actual}")
result[repo["key"]] = cache
return result
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--instance-id", required=True)
parser.add_argument("--output-dir", type=Path)
args = parser.parse_args()
task = load_task(args.instance_id)
output = args.output_dir or ROOT / ".cache" / "source-repos" / task["instance_id"]
fetch(task, output)
if __name__ == "__main__":
main()