#!/usr/bin/env python3
"""Never refuse: announce changed core paths so a person can notice them.

Print one byte-sorted, space-separated line on stdout when core changes.
The first push lists every incoming core path; untouched core is silent.
Git errors produce no notice and do not refuse the push. The hook's GIT_DIR
is kept so the notice can read commits in a bare repo.
"""

import subprocess
import sys


def main(args):
    if len(args) < 2 or not args[0] or not args[1]:
        print("50-core-notice: usage: 50-core-notice <old> <new>", file=sys.stderr)
        return 0
    old, new = args[:2]
    first = all(char == "0" for char in old)
    if first:
        command = ["git", "ls-tree", "-r", "--name-only", new]
    else:
        command = ["git", "diff", "--name-only", old, new, "--", "core/"]
    result = subprocess.run(command, capture_output=True)
    if result.returncode:
        return 0
    paths = [line for line in result.stdout.split(b"\n")
             if line and (not first or line.startswith(b"core/"))]
    if paths:
        sys.stdout.buffer.write(b"core changed: " + b" ".join(sorted(paths)) + b"\n")
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))
