|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) 2020 The Khronos Group Inc. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +# This script ensures proper POSIX text file formatting and a few other things. |
| 6 | +# This is supplementary to clang-format. |
| 7 | + |
| 8 | +# We need dos2unix and recode. |
| 9 | +if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v recode)" ]; then |
| 10 | + printf "Install 'dos2unix' and 'recode' to use this script.\n" |
| 11 | +fi |
| 12 | + |
| 13 | +set -e -uo pipefail |
| 14 | +IFS=$'\n\t' |
| 15 | + |
| 16 | +# Loops through all text files tracked by Git. |
| 17 | +git grep -zIl '' | |
| 18 | +while IFS= read -rd '' f; do |
| 19 | + # Exclude some files. |
| 20 | + if [[ "$f" == "external"* ]]; then |
| 21 | + continue |
| 22 | + elif [[ "$f" == "src/external"* ]]; then |
| 23 | + continue |
| 24 | + fi |
| 25 | + # Ensure that files are UTF-8 formatted. |
| 26 | + recode UTF-8 "$f" 2> /dev/null |
| 27 | + # Ensure that files have LF line endings and do not contain a BOM. |
| 28 | + dos2unix "$f" 2> /dev/null |
| 29 | + # Remove trailing space characters and ensures that files end |
| 30 | + # with newline characters. -l option handles newlines conveniently. |
| 31 | + perl -i -ple 's/\s*$//g' "$f" |
| 32 | +done |
| 33 | + |
| 34 | +# If no patch has been generated all is OK, clean up, and exit. |
| 35 | +if git diff --exit-code > patch.patch; then |
| 36 | + printf "Files in this commit comply with the formatting rules.\n" |
| 37 | + rm -f patch.patch |
| 38 | + exit 0 |
| 39 | +fi |
| 40 | + |
| 41 | +# A patch has been created, notify the user, clean up, and exit. |
| 42 | +printf "\n*** The following differences were found between the code " |
| 43 | +printf "and the formatting rules:\n\n" |
| 44 | +cat patch.patch |
| 45 | +printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n" |
| 46 | +rm -f patch.patch |
| 47 | +exit 1 |
0 commit comments