forked from LibrePCB/LibrePCB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_code.sh
executable file
·94 lines (82 loc) · 2.71 KB
/
format_code.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
#!/usr/bin/env bash
set -euo pipefail
# Formats files according our coding style with clang-format. And if Python is
# available, *.pro project files will be sorted with sort_qmake_file_entries.py.
#
# Usage:
# - Make sure the executables "clang-format" and "git" are available in PATH.
# - Run the command "./dev/format_code.sh" in the root of the repository.
# - To run clang-format in a docker-container, use the "--docker" parameter.
# - To run docker with sudo, use the "--sudo" parameter.
# - To format all files (instead of only modified ones), add the "--all"
# parameter. This is intended only for LibrePCB maintainers, don't use it!
DOCKER=""
DOCKER_CMD="docker"
ALL=""
for i in "$@"
do
case $i in
--docker)
DOCKER="--docker"
shift
;;
--sudo)
DOCKER_CMD="sudo docker"
shift
;;
--all)
ALL="--all"
shift
;;
esac
done
echo "Formatting files with clang-format..."
REPO_ROOT=$(git rev-parse --show-toplevel)
if [ "$DOCKER" == "--docker" ]; then
DOCKER_IMAGE=librepcb/clang-format:6
if [ "$($DOCKER_CMD images -q $DOCKER_IMAGE | wc -l)" == "0" ]; then
echo "Building clang-format container..."
$DOCKER_CMD build "$REPO_ROOT/dev/clang-format" -t librepcb/clang-format:6
fi
echo "[Re-running format_code.sh inside Docker container]"
$DOCKER_CMD run --rm -t -i --user "$(id -u):$(id -g)" \
-v "$REPO_ROOT:/code" \
$DOCKER_IMAGE \
/bin/bash -c "cd /code && dev/format_code.sh $ALL"
echo "[Docker done.]"
exit 0
fi
COUNTER=0
for dir in apps/ libs/librepcb/ tests/unittests/
do
if [ "$ALL" == "--all" ]; then
TRACKED=$(git ls-files -- "${dir}**.cpp" "${dir}**.hpp" "${dir}**.h")
else
# Only files which differ from the master branch
TRACKED=$(git diff --name-only master -- "${dir}**.cpp" "${dir}**.hpp" "${dir}**.h")
fi
UNTRACKED=$(git ls-files --others --exclude-standard -- "${dir}**.cpp" "${dir}**.hpp" "${dir}**.h")
for file in $TRACKED $UNTRACKED
do
if [ -f "$file" ]; then
# Note: Do NOT use in-place edition of clang-format because this causes
# "make" to detect the files as changed every time, even if the content was
# not modified! So we only overwrite the files if their content has changed.
OLD_CONTENT=$(cat "$file")
NEW_CONTENT=$(clang-format -style=file "$file")
if [ "$NEW_CONTENT" != "$OLD_CONTENT" ]
then
printf "%s\n" "$NEW_CONTENT" > "$file"
echo "[M] $file"
COUNTER=$((COUNTER+1))
else
echo "[ ] $file"
fi
fi
done
done
echo "Finished: $COUNTER files modified."
# Also run sort_qmake_file_entries.py if Python is available
if [ -x "$(command -v python)" ]; then
python "$REPO_ROOT/dev/sort_qmake_file_entries.py"
fi