forked from viniciusferrao/cloysterhpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-changed.sh
More file actions
executable file
·135 lines (106 loc) · 3.54 KB
/
format-changed.sh
File metadata and controls
executable file
·135 lines (106 loc) · 3.54 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage: ./format-changed.sh [--check] [<base-ref>]
Format or validate changed C/C++ files under include/ and src/ against a base
reference. The default base reference is origin/master.
The repository pins the required clang-format major version in
.clang-format-version. Set CLANG_FORMAT_BIN to a matching executable when your
default clang-format is a different major version.
Examples:
./format-changed.sh
./format-changed.sh --check
./format-changed.sh origin/master
EOF
}
mode="format"
if [[ "${1:-}" == "--check" ]]; then
mode="check"
shift
fi
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
usage
exit 0
fi
base_ref="${1:-origin/master}"
version_file=".clang-format-version"
required_version=""
if [[ -f "${version_file}" ]]; then
required_version="$(tr -d '[:space:]' < "${version_file}")"
fi
resolve_clang_format_bin() {
local candidate
local candidates=()
if [[ -n "${CLANG_FORMAT_BIN:-}" ]]; then
echo "${CLANG_FORMAT_BIN}"
return 0
fi
if [[ -n "${required_version}" ]]; then
candidates+=("clang-format-${required_version}")
candidates+=("/opt/homebrew/opt/llvm@${required_version}/bin/clang-format")
fi
candidates+=("clang-format")
for candidate in "${candidates[@]}"; do
if [[ "${candidate}" == /* ]]; then
if [[ -x "${candidate}" ]]; then
echo "${candidate}"
return 0
fi
continue
fi
if command -v "${candidate}" >/dev/null 2>&1; then
command -v "${candidate}"
return 0
fi
done
return 1
}
extract_major_version() {
local version_line
version_line="$("$1" --version 2>/dev/null | head -n 1)"
if [[ "${version_line}" =~ ([0-9]+)(\.[0-9]+)+ ]]; then
echo "${BASH_REMATCH[1]}"
return 0
fi
return 1
}
CLANG_FORMAT_BIN="$(resolve_clang_format_bin || true)"
if [[ -z "${CLANG_FORMAT_BIN}" ]]; then
echo "clang-format is not installed" >&2
exit 1
fi
detected_version="$(extract_major_version "${CLANG_FORMAT_BIN}" || true)"
if [[ -n "${required_version}" && "${CLANG_FORMAT_ALLOW_VERSION_MISMATCH:-0}" != "1" ]]; then
if [[ -z "${detected_version}" ]]; then
echo "Unable to determine the clang-format major version for '${CLANG_FORMAT_BIN}'" >&2
exit 1
fi
if [[ "${detected_version}" != "${required_version}" ]]; then
echo "clang-format major version ${detected_version} does not match the repository requirement ${required_version}" >&2
echo "Set CLANG_FORMAT_BIN to a compatible executable or set CLANG_FORMAT_ALLOW_VERSION_MISMATCH=1 to override." >&2
exit 1
fi
fi
echo "Using ${CLANG_FORMAT_BIN} ($("${CLANG_FORMAT_BIN}" --version | head -n 1))"
if ! git rev-parse --verify "${base_ref}" >/dev/null 2>&1; then
echo "Base ref '${base_ref}' was not found" >&2
exit 1
fi
merge_base="$(git merge-base "${base_ref}" HEAD)"
mapfile -t files < <(
git diff --name-only --diff-filter=ACMR "${merge_base}" HEAD -- include src \
| grep -E '\.(h|hpp|c|cc|cpp)$' || true
)
if [[ "${#files[@]}" -eq 0 ]]; then
echo "No changed C/C++ files under include/ or src/."
exit 0
fi
if [[ "${mode}" == "check" ]]; then
printf '%s\0' "${files[@]}" \
| xargs -0 "${CLANG_FORMAT_BIN}" --dry-run --Werror
echo "clang-format check passed for ${#files[@]} files."
exit 0
fi
printf '%s\0' "${files[@]}" | xargs -0 "${CLANG_FORMAT_BIN}" -i
echo "Formatted ${#files[@]} files."