import os
import subprocess
import sys

REPO = "https://github.com/MicrosoftDocs/azure-ai-docs.git"
F = "articles/foundry/openai/includes/concepts-model-retirement-schedule-content.md"
ROWS = ("sora-2 | 2025-10-06", "sora-2 | 2025-12-08")
HERE = os.path.dirname(os.path.abspath(__file__))

def git(wd, *a):
    return subprocess.run(["git", "-C", wd] + list(a), capture_output=True, text=True, encoding="utf-8").stdout

def main():
    wd = sys.argv[1] if len(sys.argv) > 1 else os.path.join(HERE, "_azdocs")
    if not os.path.isdir(os.path.join(wd, ".git")):
        subprocess.run(["git", "clone", "-q", "--filter=blob:none", "--no-checkout", "--sparse", REPO, wd], check=True)
    out = ["main HEAD %s (%s)" % (git(wd, "rev-parse", "--short", "HEAD").strip(), git(wd, "log", "-1", "--format=%cI").strip()), ""]
    for row in ROWS:
        out.append("ROW %s  (first-parent history on main, oldest first; printed when the row changes)" % row)
        prev = None
        for c in git(wd, "log", "--first-parent", "--reverse", "--format=%h %cI", "--", F).split("\n"):
            if not c.strip():
                continue
            sha, when = c.split()
            line = next((l for l in git(wd, "show", "%s:%s" % (sha, F)).splitlines() if row in l), "(row absent)")
            line = " ".join(line.split())
            if line != prev:
                out.append("  %s %s  %s" % (when, sha, line))
                prev = line
        out.append("")
    for sha in ("ca98eeda30",):
        out.append("change commit %s: %s" % (sha, git(wd, "log", "-1", "--format=%cI %an | %s", sha).strip()))
    text = "\n".join(out) + "\n"
    open(os.path.join(HERE, "azure_sora_dates_c7555.out.txt"), "w", encoding="utf-8", newline="\n").write(text)
    sys.stdout.write(text)

if __name__ == "__main__":
    main()
