blob: c7913b2078f867e8cea9bcc81bc4643f079edf02 [file] [log] [blame]
#!/usr/bin/env bash
# Copyright 2021 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
set -o errexit
set -o pipefail
main_ref="origin/HEAD"
if ! git rev-parse "$main_ref" > /dev/null; then
exit 0
fi
# Ref that we'll use as the "base" when determining which files have been
# changed and should be validated. We use the common ancestor of JIRI_HEAD and
# the current commit to avoid checking files that have been modified by other
# commits on JIRI_HEAD when we're on a branch whose latest merged ancestor
# commit is behind JIRI_HEAD.
base_ref="$(git merge-base "$main_ref" HEAD)"
# Check that files are properly formatted by Black. Ideally we would check the
# git index rather than the current version of the file on disk, but `git show
# HEAD:<file>` is an order of magnitude slower than just reading the current
# version on disk, so it would take a noticeable amount of time with more than
# ~5 changed Python files.
#
# This is only best-effort, so it's OK so skip if Black isn't available via the
# user's $PATH.
if command -v black > /dev/null; then
black_output_path=$(mktemp)
changed_files="$(git diff "$base_ref" --diff-filter=d --name-only -- "*.py")"
if ! echo "$changed_files" | xargs black --check 2> "$black_output_path"; then
>&2 echo "Files not properly formatted. Run 'black .' and then 'git add'."
# Only print Black's output in case of failure; it's confusing to show
# the "all good" output in case of success.
cat "$black_output_path"
exit 1
fi
fi
# Check that recipe DEPS are properly formatted.
cd "$(dirname "${BASH_SOURCE[0]}")/../.."
./scripts/cleanup_deps.py --check