Skip to content

Commit

Permalink
Merge pull request #8 from bpintea/feature/code_formatter
Browse files Browse the repository at this point in the history
Code formatter
  • Loading branch information
bpintea authored Jun 10, 2018
2 parents cfa7eeb + 7e5c51a commit a67849a
Show file tree
Hide file tree
Showing 21 changed files with 1,698 additions and 1,447 deletions.
66 changes: 66 additions & 0 deletions devtools/astyle-c.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash
#
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.
#

# Reformats the driver code, using Artistic Style, to ensure consistency.


# By default, format the driver source code (no arguments are received);
# else append the arguments to the pre-set formating parameters
if [ $# -lt 1 ] ; then
ARGS=`git rev-parse --show-toplevel`/driver/*.[ch]
else
ARGS=${@:1}
fi

#
# Ensure astyle is available and is of the right version to have all below
# parameters supported.
#
if ! which -s astyle ; then
echo "ERROR: The astyle code formatter is not available. Exiting."
exit 1
fi

REQUIRED_ASTYLE_VERSION=3.1
FOUND_ASTYLE_VERSION=$(expr "`astyle --version`" : ".* \([0-9].[0-9]\(.[0-9]\)\{0,1\}\)")

if [ -z "${FOUND_ASTYLE_VERSION}" ] ; then
echo "ERROR: Required astyle version ${REQUIRED_ASTYLE_VERSION} not found."
echo " Could not determine astyle version."
exit 2
fi

if [ "${REQUIRED_ASTYLE_VERSION}" != "${FOUND_ASTYLE_VERSION}" ] ; then
echo "ERROR: Required astyle version ${REQUIRED_ASTYLE_VERSION} not found."
echo " Detected astyle version ${FOUND_ASTYLE_VERSION}"
exit 3
fi

# A10: "One True Brace Style" uses linux braces and adds braces to unbraced
# one line conditional statements. (and apparently loops too)
# k3 : align-pointer=name
# T : Indent using all tab characters, if possible.
# xC : break a line if the code exceeds # characters.
# xU : Indent, instead of align, continuation lines following lines that
# contain an opening paren '(' or an assignment '='.
# xt : Set the continuation indent for a line that ends with an opening paren
# '(' or an assignment '='.
# w : Indent multi-line preprocessor definitions ending with a backslash.
# S : Indent 'switch' blocks so that the 'case X:' statements are indented in
# the switch block
# xL : will cause the logical conditionals to be placed last on the previous
# line.
# xW : Indent preprocessor blocks at brace level zero and immediately within a
# namespace.
# xf :
# xh : Attach the return type to the function name (in deFinition and
# declaration).
# n : Do not retain a backup of the original file.
# --dry-run -Q : only output file that would be formatted
ASTYLE_PARAMS="-A10 -k3 -T -xC79 -xU -w -S -xt -xL -xf -xh -n"
astyle $ASTYLE_PARAMS $ARGS

41 changes: 41 additions & 0 deletions devtools/githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

# hook the script with:
# $ git config core.hooksPath devtools/githooks/


SRC_HOME=${SRC_HOME:-`git rev-parse --show-toplevel`}
C_FORMATTER="$SRC_HOME/devtools/astyle-c.sh"
CC_FORMATTER="$SRC_HOME/devtools/astyle-cc.sh --dry-run -Q"

# driver source files only
DRV_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep 'driver.*\.[ch][c]\?$')
# test source files only
TEST_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep 'test.*\.[ch][c]\?$')

[ -z "$DRV_FILES" ] && [ -z "$TEST_FILES" ] && exit 0

function checkfmt() {
[ $# -lt 2 ] && return 0
FORMATTER=$1
FILES=$2

unformatted=$($FORMATTER --dry-run -Q $FILES)
[ -z "$unformatted" ] && return 0

echo >&2 "Source files must be formatted with astyle. Please run:"
for src in $unformatted; do
# astyle outputs "Formatted somefile.c"
if [[ $src =~ \.[ch][c]?$ ]] ; then
echo >&2 "$FORMATTER $src"
fi
done

return 1
}

checkfmt $C_FORMATTER $DRV_FILES || exit 1
# checkfmt $CC_FORMATTER $TEST_FILES || exit 1


exit 0
Loading

0 comments on commit a67849a

Please sign in to comment.