-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathformat
More file actions
executable file
·77 lines (65 loc) · 2.19 KB
/
format
File metadata and controls
executable file
·77 lines (65 loc) · 2.19 KB
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
#!/bin/sh
set -e
# Determine the script directory and repository root
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
REPO_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)
# Go to the repository root directory.
cd "$REPO_ROOT"
# clang-format's behavior changes between versions, even given the same
# configuration.
# This project fixes the version (based on the maintainers' setup) and
# occasionally bumps the required version, reformatting everything.
version=14
formatter=clang-format-$version
formatter_options="--style=file -i $*"
find_sources() {
find binding/ examples/ fuzz/ include/ src/ test/ -type f \( -name '*.h' -o -name '*.cpp' \) "$@"
}
# If the correct version of clang-format is installed, then use it and quit.
if >/dev/null command -v "$formatter"; then
# shellcheck disable=SC2086
find_sources -print0 | xargs -0 "$formatter" $formatter_options
exit
fi
if ! >/dev/null command -v docker; then
>&2 echo "Neither $formatter nor docker is installed. Unable to format code."
exit 1
fi
# If they haven't downloaded the clang docker image before, then prompt the
# user before telling docker to download it (it's large).
image=silkeh/clang:$version
if [ "$(docker image ls --quiet $image | wc -l)" -eq 0 ]; then
printf '%s is not installed. Download docker image %s instead? [Y/n] ' "$formatter" "$image"
read -r response
case "$response" in
y|Y|yes|Yes|YES|'') ;;
*) exit ;;
esac
fi
mount_path=/mnt/repo
# File paths passed to the dockerized clang-format need to be relative to
# the repository root and prefixed with the bind mount path.
#
# Non-path arguments (flags) are left alone.
process_arg() {
case "$1" in
-*) printf '%s\0' "$1" ;;
*) printf '%s/%s\0' "$mount_path" "$1" ;;
esac
}
docker_clang_format() {
# shellcheck disable=SC2086
find_sources | while IFS= read -r arg; do
process_arg "$arg"
done | xargs -0 \
docker run \
--interactive \
--rm \
--volume /etc/passwd:/etc/passwd:ro \
--volume /etc/group:/etc/group:ro \
--user "$(id -u):$(id -g)" \
--mount "type=bind,source=$REPO_ROOT,destination=$mount_path" \
"$image" \
clang-format $formatter_options
}
docker_clang_format