forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreformat-c
executable file
·51 lines (45 loc) · 1.53 KB
/
reformat-c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh
#
# @author Markus Raab <[email protected]>
# @brief Reformats the whole source code
# @date 18.02.2016
# @tags reformat
SCRIPTS_DIR=$(dirname "$0")
# shellcheck disable=SC1091 source=include-common
. "${SCRIPTS_DIR}/include-common"
MIN_VERSION=6
MAX_VERSION=7
CLANG_FORMAT=$(command -v clang-format-$MIN_VERSION.0 || command -v clang-format-$MAX_VERSION || command -v clang-format)
if [ -n "$CLANG_FORMAT" ]; then
LOCATION="$CLANG_FORMAT"
VERSION=$("$CLANG_FORMAT" --version 2> /dev/null)
MAJOR_VERSION=$(printf '%s' "$VERSION" | sed -E 's/.* ([0-9]+)\.[0-9].[0-9][ -].*/\1/')
if [ "${MAJOR_VERSION:-0}" -lt $MIN_VERSION ] || [ "${MAJOR_VERSION:-0}" -gt $MAX_VERSION ]; then
unset CLANG_FORMAT
fi
fi
if [ -z "${CLANG_FORMAT}" ]; then
printf >&2 'ClangFormat: %s\n' "$LOCATION"
printf >&2 'Version Info: %s\n' "$VERSION"
printf >&2 'Major Version: %s\n' "$MAJOR_VERSION"
printf >&2 'Please install clang-format %s or clang-format %s' "$MIN_VERSION" "$MAX_VERSION"
exit 1
fi
cd "$SOURCE" || {
printf >&2 'Unable to change into source directory'
exit 1
}
if [ $# -gt 0 ]; then
source_files=$(printf "%s\n" "$@" |
grep -x -E '.*\.(c|h|cpp|hpp|c\.in|h\.in)' |
grep -v '^src/tools/pythongen.*' |
grep -v '^tests/shell/gen.*')
if [ -z "$source_files" ]; then
exit 0
fi
else
source_files=$(git ls-files '*.c' '*.h' '*.cpp' '*.hpp' '*.c.in' '*.h.in' |
grep -v '^src/tools/pythongen.*' |
grep -v '^tests/shell/gen.*')
fi
printf "%s\n" "$source_files" | sed -nE 's/(.*)/'"'"'\1'"'"'/p' | xargs "$CLANG_FORMAT" -style=file -i