daml/fmt.sh
fabiotudone-da f5d2135f97
Check protobuf compatibility of main and PR commits w.r.t. previous stable release [KVL-1109] (#10950)
* Check protobuf compatibility of release commits w.r.t. previous stable release

CHANGELOG_BEGIN
CHANGELOG_END

* Remove blank line

* Don't persist credentials

Co-authored-by: Gary Verhaegen <gary.verhaegen@digitalasset.com>

* check-protobuf-against-stable.sh: SRC_DIR -> PROJECT ROOT + simplify

* Don't set LATEST_STABLE global in a function

* Simplify by using only the main work tree

* Simplify further as the check will be only run from `main`

* Move the check to `ci/build.yml` so that it is also run on PRs

* Enter the development environment to use tools

* Make variables read-only

* Support release branches and PRs targeting them

* Fix and document the reference tag finding logic

* Fix SYSTEM_PULLREQUEST_TARGETBRANCH and print it

* Don't log the source branch

* Fix comment formatting

Co-authored-by: Gary Verhaegen <gary.verhaegen@digitalasset.com>

* Enable Slack integration

Co-authored-by: Gary Verhaegen <gary.verhaegen@digitalasset.com>

* Don't check if the branch is a release one

...as the check won't be run on release branches.

* Add compatibility_stable_protobuf to collect_build_data

* Do not activate dev-env globally but only in sub-shells

* Add an explanation about why the check is not run on release branch commits

* Simplify further by leveraging `buf`'s ability to compare against branches

* Use `buf`'s `tag` locator instead of `branch`

* Split buf checks by module and remove previous manual check

* Explain how to run locally

* Use more future-proof WIRE_JSON for participant-integration-api

Co-authored-by: Simon Meier <meiersi-da@users.noreply.github.com>

* Use stricter FILE for the ledger gRPC API

* Propose an explanation for WIRE in kvutils

* Fix comment typo

* Re-introduce linting configuration for kvutils

* Simplify explanation for KVUtils' breaking check rule

* Remove extra (C) header from 3rd-party proto

* Don't touch the copyright of google/rpc/status.proto

Co-authored-by: Gary Verhaegen <gary.verhaegen@digitalasset.com>
Co-authored-by: Simon Meier <meiersi-da@users.noreply.github.com>
2021-09-23 15:50:33 +00:00

152 lines
5.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Run formatters and linter, anything platform-independent and quick
#
# Usage: ./fmt.sh [--test]
set -euo pipefail
cd "$(dirname "$0")"
# load the dev-env
eval "$(dev-env/bin/dade-assist)"
## Config ##
is_test=
scalafmt_args=()
javafmt_args=(--set-exit-if-changed --replace)
diff_mode=false
dade_copyright_arg=update
buildifier_target=//:buildifier-fix
## Functions ##
log() {
echo "fmt.sh: $*" >&2
}
run() {
echo "$ ${*%Q}"
"$@"
ret=$?
if [[ $is_test = 1 && $ret -gt 0 ]]; then
log "command failed with return $ret"
log
log "run ./fmt.sh to fix the issue"
exit 1
fi
return 0
}
# We do not run on deleted files, or files that have been added since we last rebased onto main.
check_diff() {
# $1 merge_base
# $2 regex
# "${@:3}" command
changed_files=$(git diff --name-only --diff-filter=ACMRT "$1" | grep $2 || [[ $? == 1 ]])
if [[ -n "$changed_files" ]]; then
run "${@:3}" ${changed_files[@]:-}
else
echo "No changed file to check matching '$2', skipping."
fi
}
## Main ##
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
cat <<USAGE
Usage: ./fmt.sh [options]
Options:
-h, --help: shows this help
--test: only test for formatting changes, used by CI
USAGE
exit
;;
--test)
shift
is_test=1
scalafmt_args+=(--test)
javafmt_args=(--set-exit-if-changed --dry-run)
dade_copyright_arg=check
buildifier_target=//:buildifier
;;
--diff)
shift
merge_base="$(git merge-base origin/main HEAD)"
scalafmt_args+=('--mode=diff' "--diff-branch=${merge_base}")
diff_mode=true
;;
*)
echo "fmt.sh: unknown argument $1" >&2
exit 1
;;
esac
done
echo "\
─────────────────────────────▄██▄
──FORMAT ALL THE THINGS!!!───▀███
────────────────────────────────█
───────────────▄▄▄▄▄────────────█
──────────────▀▄────▀▄──────────█
──────────▄▀▀▀▄─█▄▄▄▄█▄▄─▄▀▀▀▄──█
─────────█──▄──█────────█───▄─█─█
─────────▀▄───▄▀────────▀▄───▄▀─█
──────────█▀▀▀────────────▀▀▀─█─█
──────────█───────────────────█─█
▄▀▄▄▀▄────█──▄█▀█▀█▀█▀█▀█▄────█─█
█▒▒▒▒█────█──█████████████▄───█─█
█▒▒▒▒█────█──██████████████▄──█─█
█▒▒▒▒█────█───██████████████▄─█─█
█▒▒▒▒█────█────██████████████─█─█
█▒▒▒▒█────█───██████████████▀─█─█
█▒▒▒▒█───██───██████████████──█─█
▀████▀──██▀█──█████████████▀──█▄█
──██───██──▀█──█▄█▄█▄█▄█▄█▀──▄█▀
──██──██────▀█─────────────▄▀▓█
──██─██──────▀█▀▄▄▄▄▄▄▄▄▄▀▀▓▓▓█
──████────────█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
──███─────────█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
──██──────────█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
──██──────────█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
──██─────────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
──██────────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█
──██───────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▌
──██──────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▌
──██─────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▌
──██────▐█▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓█▌
"
# Check for correct copyrights
run dade-copyright-headers "$dade_copyright_arg" .
# We do test hlint via Bazel rules but we run it separately
# to get linting failures early.
if [ "$diff_mode" = "true" ]; then
check_diff $merge_base '\.hs$' hlint -j4
check_diff $merge_base '\.java$' javafmt "${javafmt_args[@]:-}"
else
run hlint -j4 --git
java_files=$(find . -name "*.java")
if [[ -z "$java_files" ]]; then
echo "Unexpected: no Java file in the repository"
exit 1
fi
run javafmt "${javafmt_args[@]:-}" ${java_files[@]:-}
fi
# check for scala code style
run scalafmt "${scalafmt_args[@]:-}"
# check for Bazel build files code formatting
run bazel run "$buildifier_target"
# Note that we cannot use a symlink here because Windows.
if ! diff .bazelrc compatibility/.bazelrc >/dev/null; then
echo ".bazelrc and compatibility/.bazelrc are out of sync:"
diff -u .bazelrc compatibility/.bazelrc
fi