This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathcheck_formatting.sh
executable file
·98 lines (76 loc) · 3.79 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh
@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@" || exit
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/dev/"$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-c &
reformat reformat-cmake &
reformat reformat-java &
reformat reformat-javascript &
reformat reformat-markdown &
reformat reformat-shell &
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 JavaScript & Markdown code, and
- [`shfmt`](https://github.com/mvdan/sh) to format Shell code
- [`google-java-format`](https://google.github.io/styleguide/javaguide.html) to format Java source code
. Afterwards you can use the following scripts to fix the formatting problems
- `reformat-c` to format C/C++ source files,
- `reformat-cmake` to format CMake files,
- `reformat-java` to format Java files,
- `reformat-javascript` to format JavaScript files,
- `reformat-markdown` to format Markdown files, and
- `reformat-shell` 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_output="$(git diff -p 2>&1)"
if [ $? -ne 0 ]; then
error_message="$(printf 'Unable to create diff: %s' "$git_diff_output" 2>&1)"
false
exit_if_fail "$error_message"
fi
if [ -n "$git_diff_output" ]; then
false
succeed_if "$error_message"
printf '\n\n————————————————————————————————————————————————————————————\n\n'
printf '%s' "$git_diff_output"
printf '\n\n————————————————————————————————————————————————————————————\n\n'
fi
# We stash working directory and staged changes. This operation should only remove formatting modifications, since we already checked for a
# clean state at the beginning of this script. We stash all file changes, since otherwise the test will always succeed on the second
# invocation. See also: https://issues.libelektra.org/3281
git stash push -m 'Formatting Updates'
end_script