File size: 2,824 Bytes
0e239c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""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()