1 1.1 christos #!/bin/bash 2 1.1 christos # 3 1.1 christos # Inspired by https://github.com/antiagainst/SPIRV-Tools/commit/c4f1bf8ddf7764b7c11fed1ce18ceb1d36b2eaf6 4 1.1 christos # 5 1.1.1.2 christos # Script to determine if source code in a diff is properly formatted. On 6 1.1.1.2 christos # GitHub, a commit range is provided which corresponds to the commit range of 7 1.1.1.2 christos # 1) commits associated with a pull request, or 8 1.1.1.3 christos # 2) commits on a branch which are not also part of main, or 9 1.1.1.3 christos # 3) commits since last push to main. 10 1.1.1.2 christos # 11 1.1 christos # Exits with non 0 exit code if formatting is needed. 12 1.1 christos # 13 1.1 christos # This script assumes to be invoked at the project root directory. 14 1.1 christos 15 1.1.1.2 christos COMMIT_RANGE="${1}" 16 1.1 christos 17 1.1 christos if [ -z "${COMMIT_RANGE}" ]; then 18 1.1 christos >&2 echo "Empty commit range, missing parameter" 19 1.1.1.2 christos exit 1 20 1.1 christos fi 21 1.1 christos 22 1.1 christos >&2 echo "Commit range $COMMIT_RANGE" 23 1.1 christos 24 1.1 christos FILES_TO_CHECK="$(git diff --name-only "$COMMIT_RANGE" | grep -e '\.c$' -e '\.h$' || true)" 25 1.1.1.3 christos CFV="${CLANG_FORMAT_VERSION:-10}" 26 1.1 christos 27 1.1 christos if [ -z "${FILES_TO_CHECK}" ]; then 28 1.1 christos >&2 echo "No source code to check for formatting" 29 1.1.1.2 christos exit 0 30 1.1 christos fi 31 1.1 christos 32 1.1 christos FORMAT_DIFF=$(git diff -U0 ${COMMIT_RANGE} -- ${FILES_TO_CHECK} | clang-format-diff$CFV -p1) 33 1.1 christos 34 1.1 christos if [ -z "${FORMAT_DIFF}" ]; then 35 1.1 christos >&2 echo "All source code in the diff is properly formatted" 36 1.1.1.2 christos exit 0 37 1.1 christos else 38 1.1 christos >&2 echo -e "Found formatting errors\n" 39 1.1 christos echo "${FORMAT_DIFF}" 40 1.1 christos >&2 echo -e "\nYou can save the diff above and apply it with 'git apply -p0 my_diff'" 41 1.1 christos exit 1 42 1.1 christos fi 43