-
Notifications
You must be signed in to change notification settings - Fork 2.5k
187 lines (163 loc) · 6.86 KB
/
Copy pathci-clang-format.yml
File metadata and controls
187 lines (163 loc) · 6.86 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright 2021-present StarRocks, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: CI - Clang Format Auto Fix
on:
pull_request_target:
types: [opened, ready_for_review, synchronize]
# Only trigger when backend C/C++ source files are touched.
paths:
- 'be/src/**'
- 'be/test/**'
permissions:
contents: write
jobs:
clang-format:
name: Clang-Format
runs-on: ubuntu-latest
steps:
- name: Checkout PR head branch
uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
token: ${{ secrets.PAT }}
- name: Set up base branch reference
env:
BASE_REF: ${{ github.base_ref }}
BASE_REPO: ${{ github.repository }}
run: |
git remote add upstream "https://github.com/${BASE_REPO}.git"
git fetch upstream "${BASE_REF}" main
- name: Install clang-format-10
run: |
wget https://github.com/muttleyxd/clang-tools-static-binaries/releases/download/master-5b56bb49/clang-format-10_linux-amd64 -O /usr/local/bin/clang-format-10
chmod +x /usr/local/bin/clang-format-10
- name: Auto-fix formatting on changed C++ files
env:
CLANG_FORMAT_BINARY: /usr/local/bin/clang-format-10
FORMAT_BASE_REF: upstream/${{ github.base_ref }}
BASE_REPO: ${{ github.repository }}
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
run: |
if git grep -l '^<<<<<<<' -- be/src be/test 2>/dev/null | grep -q .; then
echo "Merge conflict markers found; skipping clang-format."
exit 0
fi
fetched_files=(
"build-support/format_changed_files.py"
"build-support/run_clang_format.py"
"build-support/lintutils.py"
"build-support/excludes"
)
helper_dir=$(mktemp -d)
cleanup() {
rm -rf "${helper_dir}"
}
trap cleanup EXIT
base_ref_exists=true
if ! git rev-parse --verify "${FORMAT_BASE_REF}" >/dev/null 2>&1; then
base_ref_exists=false
fi
pr_changed_paths=""
if [[ "${base_ref_exists}" == "true" ]]; then
pr_changed_paths=$(
git diff --name-only --diff-filter=ACMR "${FORMAT_BASE_REF}...HEAD" | sort -u
)
fi
for path in "${fetched_files[@]}"; do
mkdir -p "$(dirname "${helper_dir}/${path}")"
if [[ "${HEAD_REPO}" == "${BASE_REPO}" ]] && printf '%s\n' "${pr_changed_paths}" | grep -Fxq "${path}"; then
if [[ -L "${path}" ]]; then
echo "::error::Refusing to load helper from symlinked path ${path}"
exit 1
fi
if [[ ! -f "${path}" ]]; then
echo "::error::Expected helper file ${path} to exist in the PR workspace"
exit 1
fi
cp "${path}" "${helper_dir}/${path}"
else
git show "upstream/main:${path}" > "${helper_dir}/${path}"
fi
done
excludes_file="${helper_dir}/build-support/excludes"
source_dirs="${GITHUB_WORKSPACE}/be/src,${GITHUB_WORKSPACE}/be/test"
if [[ "${base_ref_exists}" != "true" ]]; then
echo "${FORMAT_BASE_REF} not found; formatting all C++ files."
python3 "${helper_dir}/build-support/run_clang_format.py" \
--clang_format_binary="${CLANG_FORMAT_BINARY}" \
--fix \
--source_dirs="${source_dirs}" \
--exclude_globs="${excludes_file}"
exit 0
fi
changed_files=$(
{
git diff --name-only --diff-filter=ACMR "${FORMAT_BASE_REF}...HEAD"
git diff --name-only --diff-filter=ACMR --cached -- be
git diff --name-only --diff-filter=ACMR -- be
} | sort -u
)
if [[ -z "${changed_files}" ]]; then
echo "No files changed since ${FORMAT_BASE_REF}."
exit 0
fi
tmpfile=$(mktemp)
trap 'rm -f "${tmpfile}"; rm -rf "${helper_dir}"' EXIT
python3 "${helper_dir}/build-support/format_changed_files.py" \
--repo_root "${GITHUB_WORKSPACE}" \
--exclude_globs "${excludes_file}" \
--source_dirs "${source_dirs}" \
--null < <(printf '%s\n' "${changed_files}") > "${tmpfile}"
if [[ ! -s "${tmpfile}" ]]; then
echo "No changed C++ files to format since ${FORMAT_BASE_REF}."
exit 0
fi
echo "C++ files to format:"
while IFS= read -r -d '' filename; do
echo "${filename}"
done < "${tmpfile}"
xargs -0 -n 16 "${CLANG_FORMAT_BINARY}" -style=file -i < "${tmpfile}"
- name: Commit and push formatting fixes
env:
HEAD_REF: ${{ github.event.pull_request.head.ref }}
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
PAT: ${{ secrets.PAT }}
run: |
if git diff --quiet; then
echo "No formatting changes needed."
exit 0
fi
git config --global user.name "wanpengfei-git";
git config --global user.email "wanpengfei91@163.com";
git add -u
git commit -m "style: auto-fix clang-format issues"
if git push https://x-access-token:${PAT}@github.com/${HEAD_REPO}.git HEAD:${HEAD_REF}; then
echo "Formatting fixes pushed successfully."
else
git reset HEAD~1
echo "::error::Auto-push failed. Files that need formatting:"
git diff --name-only
echo "::group::Full clang-format diff (apply locally with 'git apply' to fix)"
git --no-pager diff
echo "::endgroup::"
if [ "${HEAD_REPO}" != "${{ github.repository }}" ]; then
echo "::error::This is a fork PR. Please enable 'Allow edits from maintainers' on your PR."
else
echo "::error::Same-repo push failed unexpectedly. Check repository permissions."
fi
echo "::error::To fix locally: CLANG_FORMAT_BINARY=clang-format-10 bash build-support/clang-format.sh"
exit 1
fi