Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

コミット時にclang formatでc,hファイルをコンテナ内でフォーマット #25

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
all:
HOOKS_DIR := hooks
GIT_HOOKS_DIR := .git/hooks
HOOKS := $(shell find $(HOOKS_DIR) -type f)
GIT_HOOKS := $(HOOKS:$(HOOKS_DIR)%=$(GIT_HOOKS_DIR)%)

all: $(GIT_HOOKS)
docker compose up --build

down:
Expand All @@ -12,4 +17,7 @@ backend-it:

re: down all

$(GIT_HOOKS_DIR)/%: $(HOOKS_DIR)/%
cp $< $@

.PHONY: all down re frontend-it backend-it
22 changes: 22 additions & 0 deletions hooks/Dockerfile_clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM alpine:3.17.1 as build

RUN apk add --no-cache clang-extra-tools


FROM alpine:3.17.1

WORKDIR /workdir

RUN apk add --no-cache \
clang15-libclang \
clang15-libs \
libgcc \
libstdc++ \
libxml2 \
llvm15-libs \
musl

COPY --from=build /usr/bin/clang-format /usr/bin/clang-format
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは、どこから、どこに コピーしてんだ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dockerのマルチステージビルドみたいなやつ
1行目のbuildって名前つけたやつから、6行目から始まるステージにコピーしてる


ENTRYPOINT [ "clang-format" ]
CMD [ "--help" ]
36 changes: 36 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
set -ue

# FIXME: frontのpre-commitを追加したとき衝突しそう

## == clang-format ==
target=`git diff --staged --name-only | grep -E ".+\.(c|h)"` || :
if [ -z $target ]; then
echo -e "\033[35mno target file\033[m"
exit 0
fi

echo -e "\033[34mclang-format target:" $target "\033[m"

# imageがないときbuildする
image=de-clang-format
if [ -z `docker images -q $image` ]; then
docker build -t $image -f hooks/Dockerfile_clang-format .
fi

docker run \
--rm \
-v "$(pwd):/workdir" \
-w /workdir \
--user "$(id -u):$(id -g)" \
$image \
-i $target

git add $target

if [ -z `git diff --staged --name-only`]; then
echo -e "\033[35mno staging file\033[m"
exit 1
fi

echo -e "\033[32mpre-commit format is finished\033[m"