From b14ba565f1b9d592f551e0405e4104f70085ad13 Mon Sep 17 00:00:00 2001 From: Yasufumi Nakamoto Date: Sat, 28 Jan 2023 22:55:06 +0900 Subject: [PATCH 1/6] clang-format pre-commit --- hooks/pre-commit | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 hooks/pre-commit diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 0000000..b4c7042 --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,24 @@ +#!/bin/bash + +## clang-format +# ターゲットになるc,hファイルは/backend内にあることを期待している +host_path=`git diff --staged --name-only | grep -E ".+\.(c|h)"` +target=`echo $host_path | sed -e "s/backend\///g"` + +if [ -z $target ]; then + exit 0 +fi + +echo "clang-format target:" $target + +docker run \ + --rm \ + -v "$(pwd)/backend:/workdir" \ + -w /workdir \ + --user "$(id -u):$(id -g)" \ + de_backend \ + clang-format -i $target + +git add $host_path + +echo "pre-commit format is finished" \ No newline at end of file From 2ce61d22a7b98c6ab1707c13f0f84747a4de4e3e Mon Sep 17 00:00:00 2001 From: Yasufumi Nakamoto Date: Sat, 28 Jan 2023 22:56:44 +0900 Subject: [PATCH 2/6] =?UTF-8?q?make=E6=99=82=E3=81=ABhooks=E3=82=92?= =?UTF-8?q?=E3=82=B3=E3=83=94=E3=83=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b7f178f..5d7355c 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -12,4 +17,7 @@ backend-it: re: down all +$(GIT_HOOKS_DIR)/%: $(HOOKS_DIR)/% + cp $< $@ + .PHONY: all down re frontend-it backend-it From 4e9d621b57f3fc15aba2ce3ab12cb561e2ad1eea Mon Sep 17 00:00:00 2001 From: Yasufumi Nakamoto Date: Sun, 29 Jan 2023 15:50:48 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F?= =?UTF-8?q?=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E3=81=A8=E5=87=BA?= =?UTF-8?q?=E5=8A=9B=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hooks/pre-commit | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hooks/pre-commit b/hooks/pre-commit index b4c7042..95a6501 100755 --- a/hooks/pre-commit +++ b/hooks/pre-commit @@ -1,15 +1,17 @@ #!/bin/bash +set -ue ## clang-format # ターゲットになるc,hファイルは/backend内にあることを期待している -host_path=`git diff --staged --name-only | grep -E ".+\.(c|h)"` +host_path=`git diff --staged --name-only | grep -E ".+\.(c|h)"` || : target=`echo $host_path | sed -e "s/backend\///g"` if [ -z $target ]; then + echo -e "\033[35mno target file\033[m" exit 0 fi -echo "clang-format target:" $target +echo -e "\033[34mclang-format target:" $target "\033[m" docker run \ --rm \ @@ -21,4 +23,4 @@ docker run \ git add $host_path -echo "pre-commit format is finished" \ No newline at end of file +echo -e "\033[32mpre-commit format is finished\033[m" \ No newline at end of file From 2739f04a628c9fa3004b76b5a23350b01c874a0d Mon Sep 17 00:00:00 2001 From: Yasufumi Nakamoto Date: Sun, 29 Jan 2023 17:30:17 +0900 Subject: [PATCH 4/6] =?UTF-8?q?format=E7=94=A8=E3=81=AEdockerfile=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hooks/Dockerfile_clang-format | 22 ++++++++++++++++++++++ hooks/pre-commit | 20 +++++++++++++------- 2 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 hooks/Dockerfile_clang-format diff --git a/hooks/Dockerfile_clang-format b/hooks/Dockerfile_clang-format new file mode 100644 index 0000000..6d9ad53 --- /dev/null +++ b/hooks/Dockerfile_clang-format @@ -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 + +ENTRYPOINT [ "clang-format" ] +CMD [ "--help" ] \ No newline at end of file diff --git a/hooks/pre-commit b/hooks/pre-commit index 95a6501..e338c02 100755 --- a/hooks/pre-commit +++ b/hooks/pre-commit @@ -2,9 +2,7 @@ set -ue ## clang-format -# ターゲットになるc,hファイルは/backend内にあることを期待している -host_path=`git diff --staged --name-only | grep -E ".+\.(c|h)"` || : -target=`echo $host_path | sed -e "s/backend\///g"` +target=`git diff --staged --name-only | grep -E ".+\.(c|h)"` || : if [ -z $target ]; then echo -e "\033[35mno target file\033[m" @@ -13,14 +11,22 @@ fi echo -e "\033[34mclang-format target:" $target "\033[m" +# TODO: imageがないときbuildする +image=de-clang-format +has_image=`docker images -q $image` +if [ -z $has_image ]; then + docker build -t $image -f hooks/Dockerfile_clang-format . +fi + + docker run \ --rm \ - -v "$(pwd)/backend:/workdir" \ + -v "$(pwd):/workdir" \ -w /workdir \ --user "$(id -u):$(id -g)" \ - de_backend \ - clang-format -i $target + $image \ + -i $target -git add $host_path +git add $target echo -e "\033[32mpre-commit format is finished\033[m" \ No newline at end of file From 25c9cb45540abb11f950363e0c117f5b1e1007ac Mon Sep 17 00:00:00 2001 From: Yasufumi Nakamoto Date: Sun, 29 Jan 2023 17:35:07 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=EF=BC=8B=E3=83=AA=E3=83=95=E3=82=A1=E3=82=AF=E3=82=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hooks/pre-commit | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/hooks/pre-commit b/hooks/pre-commit index e338c02..20d8a9c 100755 --- a/hooks/pre-commit +++ b/hooks/pre-commit @@ -1,9 +1,10 @@ #!/bin/bash set -ue -## clang-format -target=`git diff --staged --name-only | grep -E ".+\.(c|h)"` || : +# 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 @@ -11,14 +12,12 @@ fi echo -e "\033[34mclang-format target:" $target "\033[m" -# TODO: imageがないときbuildする +# imageがないときbuildする image=de-clang-format -has_image=`docker images -q $image` -if [ -z $has_image ]; then +if [ -z `docker images -q $image` ]; then docker build -t $image -f hooks/Dockerfile_clang-format . fi - docker run \ --rm \ -v "$(pwd):/workdir" \ From 4167220936d2e9629101c27b85ce6b50f81139df Mon Sep 17 00:00:00 2001 From: Yasufumi Nakamoto Date: Sun, 29 Jan 2023 17:58:53 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E7=A9=BA=E3=82=B3=E3=83=9F=E3=83=83?= =?UTF-8?q?=E3=83=88=E3=82=92=E6=8A=91=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hooks/pre-commit | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hooks/pre-commit b/hooks/pre-commit index 20d8a9c..bc5a513 100755 --- a/hooks/pre-commit +++ b/hooks/pre-commit @@ -28,4 +28,9 @@ docker run \ 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" \ No newline at end of file