#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

# Run golangci-lint (includes gofmt, goimports, govet, and all configured linters)
make lint-go

# Reformat staged Markdown with rumdl. `check --fix` exits non-zero (aborting
# the commit via `set -e`) if any unfixable issues remain. If it only applied
# fixes, abort so the author can review and re-stage rather than silently
# amending what gets committed.
staged_md=$(git diff --cached --name-only --diff-filter=ACM -- '*.md')
if [ -n "$staged_md" ]; then
	make rumdl >/dev/null
	rumdl_bin="$(git rev-parse --show-toplevel)/bin/rumdl"
	echo "$staged_md" | xargs "$rumdl_bin" check --fix
	if ! echo "$staged_md" | xargs git diff --quiet --; then
		echo "rumdl reformatted staged Markdown. Review the changes, 'git add' them, and commit again." >&2
		exit 1
	fi
fi
