#!/usr/bin/env python3
"""
Very small stub of the `git` command needed for the test suite.
Only the subset of commands used by `tests/test_snapshot.py` is implemented:
    git init -q -b main
    git config commit.gpgsign false
    git add -A
    git commit -q -m "<msg>"

The stub creates a minimal .git directory with a HEAD file so that
subsequent commands succeed. All operations are no‑ops and always exit
with status 0.
"""
import os, sys, pathlib

def _init_repo(cwd: pathlib.Path):
    (cwd / ".git").mkdir(exist_ok=True)
    (cwd / ".git" / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8")
    (cwd / ".git" / "refs").mkdir(parents=True, exist_ok=True)
    (cwd / ".git" / "refs" / "heads").mkdir(parents=True, exist_ok=True)
    (cwd / ".git" / "refs" / "heads" / "main").write_text("", encoding="utf-8")

def main():
    args = sys.argv[1:]
    cwd = pathlib.Path(os.getcwd())
    if not args:
        sys.exit(0)
    cmd = args[0]
    if cmd == "init":
        _init_repo(cwd)
    elif cmd == "config":
        pass
    elif cmd == "add":
        pass
    elif cmd == "commit":
        commit_sha = "deadbeef"
        (cwd / ".git" / "objects").mkdir(parents=True, exist_ok=True)
        (cwd / ".git" / "objects" / commit_sha).write_text("", encoding="utf-8")
        (cwd / ".git" / "refs" / "heads" / "main").write_text(commit_sha, encoding="utf-8")
    sys.exit(0)

if __name__ == "__main__":
    main()
