Skip to content

Commit a67849a

Browse files
authored
Merge pull request #8 from bpintea/feature/code_formatter
Code formatter
2 parents cfa7eeb + 7e5c51a commit a67849a

File tree

21 files changed

+1698
-1447
lines changed

21 files changed

+1698
-1447
lines changed

devtools/astyle-c.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
#
3+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
4+
# or more contributor license agreements. Licensed under the Elastic License;
5+
# you may not use this file except in compliance with the Elastic License.
6+
#
7+
8+
# Reformats the driver code, using Artistic Style, to ensure consistency.
9+
10+
11+
# By default, format the driver source code (no arguments are received);
12+
# else append the arguments to the pre-set formating parameters
13+
if [ $# -lt 1 ] ; then
14+
ARGS=`git rev-parse --show-toplevel`/driver/*.[ch]
15+
else
16+
ARGS=${@:1}
17+
fi
18+
19+
#
20+
# Ensure astyle is available and is of the right version to have all below
21+
# parameters supported.
22+
#
23+
if ! which -s astyle ; then
24+
echo "ERROR: The astyle code formatter is not available. Exiting."
25+
exit 1
26+
fi
27+
28+
REQUIRED_ASTYLE_VERSION=3.1
29+
FOUND_ASTYLE_VERSION=$(expr "`astyle --version`" : ".* \([0-9].[0-9]\(.[0-9]\)\{0,1\}\)")
30+
31+
if [ -z "${FOUND_ASTYLE_VERSION}" ] ; then
32+
echo "ERROR: Required astyle version ${REQUIRED_ASTYLE_VERSION} not found."
33+
echo " Could not determine astyle version."
34+
exit 2
35+
fi
36+
37+
if [ "${REQUIRED_ASTYLE_VERSION}" != "${FOUND_ASTYLE_VERSION}" ] ; then
38+
echo "ERROR: Required astyle version ${REQUIRED_ASTYLE_VERSION} not found."
39+
echo " Detected astyle version ${FOUND_ASTYLE_VERSION}"
40+
exit 3
41+
fi
42+
43+
# A10: "One True Brace Style" uses linux braces and adds braces to unbraced
44+
# one line conditional statements. (and apparently loops too)
45+
# k3 : align-pointer=name
46+
# T : Indent using all tab characters, if possible.
47+
# xC : break a line if the code exceeds # characters.
48+
# xU : Indent, instead of align, continuation lines following lines that
49+
# contain an opening paren '(' or an assignment '='.
50+
# xt : Set the continuation indent for a line that ends with an opening paren
51+
# '(' or an assignment '='.
52+
# w : Indent multi-line preprocessor definitions ending with a backslash.
53+
# S : Indent 'switch' blocks so that the 'case X:' statements are indented in
54+
# the switch block
55+
# xL : will cause the logical conditionals to be placed last on the previous
56+
# line.
57+
# xW : Indent preprocessor blocks at brace level zero and immediately within a
58+
# namespace.
59+
# xf :
60+
# xh : Attach the return type to the function name (in deFinition and
61+
# declaration).
62+
# n : Do not retain a backup of the original file.
63+
# --dry-run -Q : only output file that would be formatted
64+
ASTYLE_PARAMS="-A10 -k3 -T -xC79 -xU -w -S -xt -xL -xf -xh -n"
65+
astyle $ASTYLE_PARAMS $ARGS
66+

devtools/githooks/pre-commit

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
# hook the script with:
4+
# $ git config core.hooksPath devtools/githooks/
5+
6+
7+
SRC_HOME=${SRC_HOME:-`git rev-parse --show-toplevel`}
8+
C_FORMATTER="$SRC_HOME/devtools/astyle-c.sh"
9+
CC_FORMATTER="$SRC_HOME/devtools/astyle-cc.sh --dry-run -Q"
10+
11+
# driver source files only
12+
DRV_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep 'driver.*\.[ch][c]\?$')
13+
# test source files only
14+
TEST_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep 'test.*\.[ch][c]\?$')
15+
16+
[ -z "$DRV_FILES" ] && [ -z "$TEST_FILES" ] && exit 0
17+
18+
function checkfmt() {
19+
[ $# -lt 2 ] && return 0
20+
FORMATTER=$1
21+
FILES=$2
22+
23+
unformatted=$($FORMATTER --dry-run -Q $FILES)
24+
[ -z "$unformatted" ] && return 0
25+
26+
echo >&2 "Source files must be formatted with astyle. Please run:"
27+
for src in $unformatted; do
28+
# astyle outputs "Formatted somefile.c"
29+
if [[ $src =~ \.[ch][c]?$ ]] ; then
30+
echo >&2 "$FORMATTER $src"
31+
fi
32+
done
33+
34+
return 1
35+
}
36+
37+
checkfmt $C_FORMATTER $DRV_FILES || exit 1
38+
# checkfmt $CC_FORMATTER $TEST_FILES || exit 1
39+
40+
41+
exit 0

0 commit comments

Comments
 (0)