#!/usr/bin/env bash set -euo pipefail # Stamp composer.json build from local git commit count on master/HEAD. BUILD="$(git rev-list --count master 2>/dev/null || git rev-list --count HEAD)" BUILD="$BUILD" php -r ' $path = "composer.json"; $data = json_decode(file_get_contents($path), true); if (!is_array($data)) { fwrite(STDERR, "pre-commit: invalid composer.json\n"); exit(1); } $build = getenv("BUILD"); if ($build === false || $build === "") { fwrite(STDERR, "pre-commit: missing BUILD value\n"); exit(1); } $current = (string)($data["build"] ?? ""); if ($current === $build) { exit(0); } $data["build"] = $build; file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL); fwrite(STDOUT, "pre-commit: composer.json build updated to {$build}\n"); ' git add composer.json