timescaledb/scripts/clang_format_wrapper.sh
Matvey Arye cc52a78b9b Add scripts for clang-format
We are switching from pgindent to clang-format for several
reasons:
1) pgindent is harder to set up and depends on other tools
like gobjdump that differ between OSes making it harder
to set up a proper development environment.
2) clang-format is more standard and works better
with various IDEs and editors.

Our clang-format wrapper script is transactional in that this
version of the script copies all files to a temporary directory,
runs clang-format in the temp dir, and only then copies the files
back. This should prevent mis-formatting when the script is cancled
in the middle of running.

We changes travis to use clang-format instead of pgindent
and thus it uses a different docker image to do the format test.
We also changes travis to use the new clang docker
image for the license check and prefix checks for consistency.
This also paves the way for us to use more clang features for tests
in the future.
2019-02-05 16:55:16 -05:00

83 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# clang-format misunderstand sql function written in C because they have the signature
# Datum my_func(PG_FUNCTION_ARGS)
# and clang-format does not interpret PG_FUNCTION_ARGS as a type, name pair.
# This script replaces PG_FUNCTION_ARGS with "PG_FUNCTION_ARGS fake_var_for_clang" to
# make it look like a proper function for clang and then converts it back after clang runs.
SCRIPT_DIR=$(cd $(dirname $0); pwd)
BASE_DIR=$(dirname $SCRIPT_DIR)
TEMP_DIR="/tmp/timescaledb_format"
OPTIONS="${@}"
if [ -e ${TEMP_DIR} ]
then
echo "error: ${TEMP_DIR} already exists"
echo " delete ${TEMP_DIR} to format"
exit 1
fi
cleanup() {
echo "cleaning"
rm -rf ${TEMP_DIR}
}
trap cleanup EXIT SIGINT SIGTERM
mkdir ${TEMP_DIR}
if [[ $? != 0 ]]
then
echo "error: could not create temporary directory ${TEMP_DIR}"
exit 1
fi
#from this point on, if we get a failure we end up in an invalid state, so exit
set -e
CLANG_FORMAT_FLAGS=""
FILE_NAMES=""
for opt in ${OPTIONS}
do
if [[ "${opt:0:1}" != "-" ]]
then
file_path=${opt#"$BASE_DIR/"}
FILE_NAMES="${FILE_NAMES} $file_path"
else
CLANG_FORMAT_FLAGS="${CLANG_FORMAT_FLAGS} $opt"
fi
done
echo "copying to ${TEMP_DIR}"
for name in ${FILE_NAMES}
do
# sed -i have different semantics on mac and linux, don't use
mkdir -p $(dirname ${TEMP_DIR}/${name})
sed -e 's/(PG_FUNCTION_ARGS)/(PG_FUNCTION_ARGS fake_var_for_clang)/' ${BASE_DIR}/${name} > ${TEMP_DIR}/${name}
done
cp ${BASE_DIR}/.clang-format ${TEMP_DIR}/.clang-format
CURR_DIR=${PWD}
echo "formatting"
cd ${TEMP_DIR}
clang-format ${CLANG_FORMAT_FLAGS} ${FILE_NAMES}
cd ${CURR_DIR}
echo "copying back"
for name in ${FILE_NAMES}
do
sed -e 's/PG_FUNCTION_ARGS fake_var_for_clang/PG_FUNCTION_ARGS/' ${TEMP_DIR}/${name} > ${TEMP_DIR}/replace_file && \
mv ${TEMP_DIR}/replace_file ${BASE_DIR}/${name}
done
exit 0;