File size: 1,728 Bytes
29ed697
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Install the committed storage policy in this checkout's local Git config."""
from pathlib import Path
import shlex
import subprocess
import sys

ROOT = Path(__file__).resolve().parents[1]


def main():
    if not (ROOT / ".git").exists():
        raise SystemExit("Run setup in a Git checkout, not a Nix store source copy.")
    def git(*args):
        return subprocess.check_output(["git", "-C", str(ROOT), *args], text=True).strip()
    existing = subprocess.run(["git", "-C", str(ROOT), "config", "--local", "--get", "core.hooksPath"],
                              text=True, capture_output=True).stdout.strip()
    if existing and existing != ".githooks":
        raise SystemExit(f"Existing hooksPath {existing!r}: integrate it before replacing hooks.")
    git("lfs", "version")
    git("xet", "--version")
    git("lfs", "install", "--local", "--skip-repo")
    git("xet", "install", "--local")
    git("config", "--local", "lfs.basictransfersonly", "false")
    python = shlex.quote(sys.executable)
    script = shlex.quote(str(ROOT / "scripts/storage_policy.py"))
    # Git shell-quotes its %f substitution. Keep it as a distinct argument.
    git("config", "--local", "filter.nanobrush-code.clean", f"{python} {script} clean -- %f")
    git("config", "--local", "filter.nanobrush-code.smudge", "cat")
    git("config", "--local", "filter.nanobrush-code.required", "true")
    git("config", "--local", "core.hooksPath", ".githooks")
    for name in ("pre-commit", "pre-push"):
        path = ROOT / ".githooks" / name
        path.chmod(path.stat().st_mode | 0o111)
    print("Installed local code/LFS filters, Xet transfers and commit/push guards.")


if __name__ == "__main__":
    main()