/
check_formatting.sh
executable file
·78 lines (60 loc) · 2.95 KB
/
check_formatting.sh
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@INCLUDE_COMMON@
echo
echo ELEKTRA CHECK FORMATTING
echo
command -v git > /dev/null 2>&1 || {
printf >&2 'This test requires the `git` command, aborting test!\n\n'
exit 0
}
cd "@CMAKE_SOURCE_DIR@"
if ! git diff --quiet; then
printf >&2 'Source is already modified, aborting test!\n\n'
exit 0
fi
reformat() {
reformat_command=$1
reformat_command_output="$(scripts/$reformat_command 2>&1)" || {
printf >&2 -- '————————————————————————————————————————————————————————————\n'
printf >&2 -- 'Warning — Reformatting command `%s` failed\n' "$reformat_command"
printf >&2 -- '\n%s\n' "$reformat_command_output"
printf >&2 -- '————————————————————————————————————————————————————————————\n\n'
}
}
reformat reformat-source &
reformat reformat-cmake &
reformat reformat-markdown &
reformat reformat-shfmt &
wait
error_message="$(
cat << 'EOF'
The reformatting check detected code that **does not** fit the guidelines given in `doc/CODING.md`.
If you see this message on one of the build servers, you can either install one or multiple of the following tools:
- [`clang-format`](https://clang.llvm.org/docs/ClangFormat.html) to format C and C++ source code,
- [`cmake_format`](https://github.com/cheshirekow/cmake_format) to format CMake code,
- [`prettier`](https://prettier.io) to format Markdown code, and
- [`shfmt`](https://github.com/mvdan/sh) to format Shell code
. Afterwards you can use the following scripts to fix the formatting problems
- `reformat-source` to format C/C++ source files,
- `reformat-cmake` to format CMake files,
- `reformat-markdown` to format Markdown files, and
- `reformat-shfmt` to format files that contain shell code
. If you do not want to install any of the tools listed above you can also use the `patch` command after this message
to fix the formatting problems. For that please
1. copy the lines between the long dashes (`—`),
2. store them in a file called `format.patch` **in the root of the repository**
. After that use the following command to apply the changes:
sh -c '
line_prefix="$(head -n1 format.patch | sed -nE '"'"'s/(^[0-9]+:).*/\1_/p'"'"' | wc -c | sed -E '"'"'s/[ ]*//g'"'"')"
{ test "$line_prefix" -gt 1 && cut -c"$line_prefix"- format.patch || cat format.patch ; } | patch -p1
'
.
EOF
)"
git diff --quiet
succeed_if "$error_message"
git diff --quiet || {
printf '\n\n————————————————————————————————————————————————————————————\n\n'
git diff -p
printf '\n\n————————————————————————————————————————————————————————————\n\n'
}
end_script