Просмотр исходного кода

build: add a makefile target to format only changed files

zmstone 1 год назад
Родитель
Сommit
7806c7477e
2 измененных файлов с 16 добавлено и 10 удалено
  1. 4 0
      Makefile
  2. 12 10
      scripts/git-hook-pre-commit.sh

+ 4 - 0
Makefile

@@ -332,6 +332,10 @@ fmt: $(REBAR)
 	@$(SCRIPTS)/erlfmt -w 'bin/nodetool'
 	@mix format
 
+.PHONY: fmt-diff
+fmt-diff:
+	@env ERLFMT_WRITE=true ./scripts/git-hook-pre-commit.sh
+
 .PHONY: clean-test-cluster-config
 clean-test-cluster-config:
 	@rm -f apps/emqx_conf/data/configs/cluster.hocon || true

+ 12 - 10
scripts/git-hook-pre-commit.sh

@@ -6,20 +6,22 @@ if [ -n "${FORCE:-}" ]; then
     exit 0
 fi
 
-OPT="${1:--c}"
-
-# mix format check is quite fast
-which mix && mix format --check-formatted
-
 files_dirty="$(git diff --name-only | grep -E '.*\.erl' || true)"
 files_cached="$(git diff --cached --name-only | grep -E '.*\.erl' || true)"
 if [[ "${files_dirty}" == '' ]] && [[ "${files_cached}" == '' ]]; then
     exit 0
 fi
 files="$(echo -e "${files_dirty} \n ${files_cached}" | xargs)"
-# shellcheck disable=SC2086
-if ! (./scripts/erlfmt $OPT $files); then
-    echo "EXECUTE 'make fmt' to fix" >&2
-    exit 1
+if [ "${ERLFMT_WRITE:-false}" = 'true' ]; then
+    # shellcheck disable=SC2086
+    ./scripts/erlfmt -w $files
+else
+    # mix format check is quite fast
+    which mix && mix format --check-formatted
+    # shellcheck disable=SC2086
+    if ! (./scripts/erlfmt -c $files); then
+        echo "EXECUTE 'make fmt-diff' to fix" >&2
+        exit 1
+    fi
+    ./scripts/apps-version-check.sh
 fi
-./scripts/apps-version-check.sh