Skip to content

Commit 8eb4129

Browse files
authored
Replace clang-format with pre-commit (#123)
1 parent 8870c58 commit 8eb4129

File tree

10 files changed

+186
-108
lines changed

10 files changed

+186
-108
lines changed

.github/workflows/ci.yml

Lines changed: 124 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ on:
1515

1616
# Based on https://docs.docker.com/build/ci/github-actions/multi-platform/
1717
env:
18-
REGISTRY_IMAGE: libfn/ci-build
18+
REGISTRY_IMAGE_BUILD: libfn/ci-build
19+
REGISTRY_IMAGE_PRE_COMMIT: libfn/ci-pre-commit
1920

2021
jobs:
22+
2123
build:
2224
runs-on: ubuntu-latest
2325
strategy:
@@ -45,8 +47,10 @@ jobs:
4547
id: meta
4648
uses: docker/metadata-action@v5
4749
with:
48-
images: ${{ env.REGISTRY_IMAGE }}-${{ env.COMPILER }}
49-
tags: type=raw,value=${{ env.RELEASE }}
50+
images: ${{ env.REGISTRY_IMAGE_BUILD }}-${{ env.COMPILER }}
51+
tags: |
52+
type=raw,value=${{ env.RELEASE }}
53+
type=edge
5054
5155
- name: Set up QEMU
5256
uses: docker/setup-qemu-action@v3
@@ -68,10 +72,10 @@ jobs:
6872
build-args: |
6973
GCC_RELEASE=${{ env.RELEASE }}
7074
CLANG_RELEASE=${{ env.RELEASE }}
71-
context: "{{defaultContext}}:ci/build"
72-
file: Dockerfile.${{ env.COMPILER }}
75+
context: "{{defaultContext}}:ci/build/${{ env.COMPILER }}/"
76+
file: Dockerfile
7377
labels: ${{ steps.meta.outputs.labels }}
74-
outputs: type=image,name=${{ env.REGISTRY_IMAGE }}-${{ env.COMPILER }},push-by-digest=true,name-canonical=true,push=true
78+
outputs: type=image,name=${{ env.REGISTRY_IMAGE_BUILD }}-${{ env.COMPILER }},push-by-digest=true,name-canonical=true,push=true
7579
sbom: true
7680
provenance: mode=max
7781

@@ -84,12 +88,12 @@ jobs:
8488
- name: Upload digest
8589
uses: actions/upload-artifact@v4
8690
with:
87-
name: digests-${{ env.COMPILER}}-${{ env.RELEASE }}-${{ env.PLATFORM_PAIR }}
91+
name: digests-build-${{ env.COMPILER}}-${{ env.RELEASE }}-${{ env.PLATFORM_PAIR }}
8892
path: /tmp/digests/*
8993
if-no-files-found: error
9094
retention-days: 1
9195

92-
merge:
96+
merge-build:
9397
runs-on: ubuntu-latest
9498
needs:
9599
- build
@@ -112,7 +116,111 @@ jobs:
112116
uses: actions/download-artifact@v4
113117
with:
114118
path: /tmp/digests
115-
pattern: digests-${{ env.COMPILER}}-${{ env.RELEASE }}-*
119+
pattern: digests-build-${{ env.COMPILER}}-${{ env.RELEASE }}-*
120+
merge-multiple: true
121+
122+
- name: Set up Docker Buildx
123+
uses: docker/setup-buildx-action@v3
124+
125+
- name: Docker meta
126+
id: meta
127+
uses: docker/metadata-action@v5
128+
with:
129+
images: ${{ env.REGISTRY_IMAGE_BUILD }}-${{ env.COMPILER }}
130+
tags: |
131+
type=raw,value=${{ env.RELEASE }}
132+
type=edge
133+
134+
- name: Login to Docker Hub
135+
uses: docker/login-action@v3
136+
with:
137+
username: ${{ secrets.DOCKERHUB_USERNAME }}
138+
password: ${{ secrets.DOCKERHUB_TOKEN }}
139+
140+
- name: Create manifest list and push
141+
working-directory: /tmp/digests
142+
run: |
143+
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
144+
$(printf '${{ env.REGISTRY_IMAGE_BUILD }}-${{ env.COMPILER }}@sha256:%s ' *)
145+
146+
- name: Inspect image
147+
run: |
148+
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE_BUILD }}-${{ env.COMPILER }}:${{ steps.meta.outputs.version }}
149+
150+
pre-commit:
151+
runs-on: ubuntu-latest
152+
strategy:
153+
fail-fast: false
154+
matrix:
155+
platform:
156+
- linux/amd64
157+
- linux/arm64
158+
159+
steps:
160+
- name: Prepare
161+
run: |
162+
platform=${{ matrix.platform }}
163+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
164+
165+
- name: Docker meta
166+
id: meta
167+
uses: docker/metadata-action@v5
168+
with:
169+
images: ${{ env.REGISTRY_IMAGE_PRE_COMMIT }}
170+
tags: |
171+
type=schedule,pattern={{date 'YYYYMMDD'}}
172+
type=raw,value=latest,enable={{is_default_branch}}
173+
type=edge
174+
175+
- name: Set up QEMU
176+
uses: docker/setup-qemu-action@v3
177+
178+
- name: Set up Docker Buildx
179+
uses: docker/setup-buildx-action@v3
180+
181+
- name: Login to Docker Hub
182+
uses: docker/login-action@v3
183+
with:
184+
username: ${{ secrets.DOCKERHUB_USERNAME }}
185+
password: ${{ secrets.DOCKERHUB_TOKEN }}
186+
187+
- name: Build and push by digest
188+
id: build
189+
uses: docker/build-push-action@v6
190+
with:
191+
platforms: ${{ matrix.platform }}
192+
context: "{{defaultContext}}:ci/pre-commit/"
193+
file: Dockerfile
194+
labels: ${{ steps.meta.outputs.labels }}
195+
outputs: type=image,name=${{ env.REGISTRY_IMAGE_PRE_COMMIT }},push-by-digest=true,name-canonical=true,push=true
196+
sbom: true
197+
provenance: mode=max
198+
199+
- name: Export digest
200+
run: |
201+
mkdir -p /tmp/digests
202+
digest="${{ steps.build.outputs.digest }}"
203+
touch "/tmp/digests/${digest#sha256:}"
204+
205+
- name: Upload digest
206+
uses: actions/upload-artifact@v4
207+
with:
208+
name: digests-pre-commit-${{ env.PLATFORM_PAIR }}
209+
path: /tmp/digests/*
210+
if-no-files-found: error
211+
retention-days: 1
212+
213+
merge-pre-commit:
214+
runs-on: ubuntu-latest
215+
needs:
216+
- pre-commit
217+
218+
steps:
219+
- name: Download digests
220+
uses: actions/download-artifact@v4
221+
with:
222+
path: /tmp/digests
223+
pattern: digests-pre-commit-*
116224
merge-multiple: true
117225

118226
- name: Set up Docker Buildx
@@ -122,8 +230,11 @@ jobs:
122230
id: meta
123231
uses: docker/metadata-action@v5
124232
with:
125-
images: ${{ env.REGISTRY_IMAGE }}-${{ env.COMPILER }}
126-
tags: type=raw,value=${{ env.RELEASE }}
233+
images: ${{ env.REGISTRY_IMAGE_PRE_COMMIT }}
234+
tags: |
235+
type=schedule,pattern={{date 'YYYYMMDD'}}
236+
type=raw,value=latest,enable={{is_default_branch}}
237+
type=edge
127238
128239
- name: Login to Docker Hub
129240
uses: docker/login-action@v3
@@ -135,8 +246,8 @@ jobs:
135246
working-directory: /tmp/digests
136247
run: |
137248
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
138-
$(printf '${{ env.REGISTRY_IMAGE }}-${{ env.COMPILER }}@sha256:%s ' *)
249+
$(printf '${{ env.REGISTRY_IMAGE_PRE_COMMIT }}@sha256:%s ' *)
139250
140251
- name: Inspect image
141252
run: |
142-
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}-${{ env.COMPILER }}:${{ steps.meta.outputs.version }}
253+
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE_PRE_COMMIT }}:${{ steps.meta.outputs.version }}

.github/workflows/clang-format.yml

Lines changed: 0 additions & 86 deletions
This file was deleted.

.github/workflows/pre-commit.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: pre-commit
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
checks:
13+
runs-on:
14+
group: default
15+
strategy:
16+
fail-fast: false
17+
container: libfn/ci-pre-commit:latest
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
22+
- name: Prepare
23+
run: |
24+
# Required because of https://github.com/actions/checkout/issues/766
25+
git config --global --add safe.directory "${GITHUB_WORKSPACE}"
26+
pip freeze --local
27+
28+
- name: Apply pre-commit
29+
run: |
30+
pre-commit run --show-diff-on-failure --color=always --all-files

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.idea/
22
.idea_modules/
33
.build*/
4-
build*/
54
result
65
.clangd
76
.cache/

.pre-commit-config.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,17 @@
2020
# See https://pre-commit.com/hooks.html for more hooks
2121
repos:
2222
- repo: https://github.com/pre-commit/pre-commit-hooks
23-
rev: v4.5.0
23+
rev: v5.0.0
2424
hooks:
2525
- id: end-of-file-fixer
2626
- id: trailing-whitespace
27+
- id: forbid-submodules
28+
- id: mixed-line-ending
29+
- id: check-case-conflict
30+
- id: check-merge-conflict
31+
- id: check-yaml
32+
- id: destroyed-symlinks
33+
- id: detect-private-key
2734
- repo: https://github.com/pre-commit/mirrors-clang-format
2835
rev: v18.1.8
2936
hooks:
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ RUN set -ex ;\
1515
| tee /etc/apt/sources.list.d/llvm.list ;\
1616
apt-get update ;\
1717
apt-get install -t llvm-toolchain-${CODENAME}-${CLANG_RELEASE} -y --no-install-recommends \
18-
clang-${CLANG_RELEASE} clang-format-${CLANG_RELEASE} \
19-
libc++-${CLANG_RELEASE}-dev libc++abi-${CLANG_RELEASE}-dev ;\
18+
clang++-${CLANG_RELEASE} clang-${CLANG_RELEASE} libc++-${CLANG_RELEASE}-dev libc++abi-${CLANG_RELEASE}-dev ;\
2019
apt-get clean
2120

2221
RUN set -ex ;\
2322
update-alternatives --install \
2423
/usr/bin/clang clang /usr/bin/clang-${CLANG_RELEASE} 100 \
2524
--slave /usr/bin/clang++ clang++ /usr/bin/clang++-${CLANG_RELEASE} ;\
26-
update-alternatives --install \
27-
/usr/bin/clang-format clang-format /usr/bin/clang-format-${CLANG_RELEASE} 100 ;\
28-
update-alternatives --auto clang ;\
29-
update-alternatives --auto clang-format
25+
update-alternatives --auto clang
3026

3127
ENV CC=/usr/bin/clang
3228
ENV CXX=/usr/bin/clang++

ci/pre-commit/Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM debian:bookworm
2+
3+
ENV DEBIAN_FRONTEND=noninteractive
4+
RUN set -ex ;\
5+
apt-get update ;\
6+
apt-get install -y --no-install-recommends \
7+
python3 python3-pip python3-venv \
8+
ca-certificates wget vim ;\
9+
apt-get clean
10+
11+
COPY requirements.txt /requirements.txt
12+
ENV VENV=/venv
13+
ENV PATH=${VENV}/bin:${PATH}
14+
RUN set -ex ;\
15+
python3 -m venv ${VENV} ;\
16+
pip --no-cache-dir install -r requirements.txt
17+
18+
WORKDIR /work

ci/pre-commit/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pre-commit<5
2+
# NOTE versions listed below must match .pre-commit-config.yaml
3+
clang-format==18.1.8
4+
pre-commit-hooks==5.0.0

docs/toc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ pack
2828
index
2929
sum
3030
index
31-

0 commit comments

Comments
 (0)