#!/bin/sh
# script/repo-source-manifest: print every file this repository contains
# whose absence from a docker build context would go unnoticed — one
# repo-relative path per line, LC_ALL=C-sorted.
#
# That is the Go sources. A missing go.mod, go.sum or .golangci.yml
# fails the build loudly (the COPY errors, or golangci-lint refuses to
# start), so they cannot hide anything; they are listed anyway because
# it costs two lines and makes the manifest the linter's whole input
# rather than most of it.
#
# The list comes from the git index, never from a walk of the working
# tree, and that is the point: script/assert-context-complete compares
# it against the inventory the build itself emitted, and a check that
# reads its expectation from the same place it reads its evidence proves
# nothing. .dockerignore governs what docker sends; it cannot touch what
# git tracks.
#
# Tracked files only, and only those present in the worktree — a file
# staged for deletion is not something the build context is missing.
# Untracked files are not expected either, so local scratch work in the
# tree is not a failure.
#
# A path containing a newline or a quote is quoted by git and will not
# match the plain path the build emits, so it fails loudly rather than
# passing silently. No such path exists here, and none should.
set -eu

ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"

die() {
    echo "script/repo-source-manifest: $*" >&2
    exit 1
}

main() {
    cd "$ROOT"

    git rev-parse --is-inside-work-tree >/dev/null 2>&1 ||
        die "not a git work tree, so there is nothing to compare a build context against"

    # Captured before the loop so that a git failure is the script's
    # exit status: in a pipeline only the last command's status counts.
    tracked="$(git ls-files -- '*.go' go.mod go.sum .golangci.yml .golangci.yaml)"

    manifest="$(
        printf '%s\n' "$tracked" | while IFS= read -r f; do
            if [ -n "$f" ] && [ -f "$f" ]; then
                printf '%s\n' "$f"
            fi
        done | LC_ALL=C sort
    )"

    [ -n "$manifest" ] ||
        die "the git index lists no Go sources, so any build context would satisfy the check"

    printf '%s\n' "$manifest"
}

main "$@"
