#!/usr/bin/env bash
# pre-push: catch the submodule footgun where the parent repo pushes a
# commit referencing a submodule SHA that CI can't compile against —
# typically because the submodule had uncommitted/unpushed work that
# the parent's working tree silently relied on.
#
# Symptom this hook prevents (v5.24.50, #94):
#   - Edit termlib files; never `git commit` them inside the submodule.
#   - Local Haven build works (working tree has the changes).
#   - Push the parent. CI checks out the submodule at its pinned SHA
#     (which doesn't have the changes) and fails to compile.
#
# Three checks, each prints a warning and exits non-zero if violated:
#   1. Submodule has uncommitted changes (worktree differs from HEAD).
#   2. Submodule HEAD is ahead of its upstream tracking branch
#      (commits exist locally that haven't been pushed).
#   3. Submodule's recorded pointer in the parent index differs from
#      its actual HEAD (the `+` flag in `git submodule status`).
#
# Override with `git push --no-verify` if you really mean it.

set -euo pipefail

bad=0
warn() { echo "PRE-PUSH WARN: $*" >&2; bad=1; }

# Process substitution so the loop runs in this shell — `bad` survives.
while read -r line; do
    [ -z "$line" ] && continue
    flag="${line:0:1}"
    rest="${line:1}"
    sha="${rest%% *}"
    rest_after_sha="${rest#* }"
    path="${rest_after_sha%% *}"

    case "$flag" in
        '+'|'U')
            # `git submodule status` prints the *checked-out* SHA after the
            # flag, not the parent's pin — the two were the wrong way round
            # here, so the warning named the value you needed to change as the
            # one already recorded. Show both, since which is which is the
            # whole question when you hit this.
            pinned="$(git ls-tree HEAD -- "$path" 2>/dev/null | awk '{print $3}')"
            warn "submodule '$path' SHA mismatch: checked out ${sha}, parent pins ${pinned:-<unpinned>}. Did you forget 'git add $path' after committing inside the submodule?"
            ;;
    esac

    # Only inspect initialised submodules (skip '-')
    if [ -d "$path/.git" ] || [ -f "$path/.git" ]; then
        # --ignore-submodules=all: still catches real uncommitted SOURCE files
        # (the #94 footgun), but ignores benign nested-submodule pointer drift
        # (a submodule's own submodules, e.g. wayland-android's cairo/fontconfig/
        # glib) that CI doesn't build from and that otherwise forced a blanket
        # --no-verify — which skipped every check below, including preflight.
        if ! git -C "$path" diff --quiet --ignore-submodules=all HEAD 2>/dev/null; then
            warn "submodule '$path' has uncommitted changes — CI won't see them. The parent's pinned SHA is the only source of truth."
        fi
        if upstream=$(git -C "$path" rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null); then
            if git -C "$path" log "$upstream..HEAD" --oneline -1 2>/dev/null | grep -q .; then
                warn "submodule '$path' has commits ahead of $upstream — push the submodule first."
            fi
        fi
    fi
done < <(git submodule status 2>/dev/null)

# Fast CI `checks`-job gates (hardcoded strings, translation-export drift):
# deterministic and seconds-fast, so catch them here instead of a ~13-min CI
# round-trip. Export-drift is what reddened CI on #359. Changelog is skipped
# (a release concern the release flow + CI handle).
repo_root="$(git rev-parse --show-toplevel)"
if [ -x "$repo_root/scripts/preflight.sh" ]; then
    if ! "$repo_root/scripts/preflight.sh" --no-changelog >&2; then
        bad=1
    fi
fi

if [ "$bad" -ne 0 ]; then
    echo "" >&2
    echo "Push aborted by pre-push hook (tools/git-hooks/pre-push)." >&2
    echo "Override with --no-verify if you really mean it." >&2
    exit 1
fi
exit 0
