#!/usr/bin/env python3

import os
import subprocess
import sys
import tempfile

if __name__ == "__main__":
    target = sys.argv[1]

    d, b = os.path.split(target)

    with tempfile.TemporaryDirectory(prefix="is-clean-") as tmp:
        head = os.path.join(tmp, "head")
        with open(head, "xb") as f:
            p = subprocess.run(["git", "show", "HEAD:./"+b], check=True, stdout=subprocess.PIPE, cwd=d or None)
            f.write(p.stdout)

        cmdline = [ "make" ]
        if d:
            cmdline += [ "-C", d ]
        cmdline += [ b ]

        subprocess.run(cmdline, check=True)

        subprocess.run(["diff", target, head], check=True)
