Skip to content

Commit 8592704

Browse files
committed
chore: add recipes for linting Git commit messages
1 parent 1fb50c7 commit 8592704

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

tools/make/lib/lint/git/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,34 @@ ifeq ($(GIT_COMMIT_LINTER), commitlint)
3636
endif
3737

3838
.PHONY: lint-commit
39+
40+
#/
41+
# Lints a commit message.
42+
#
43+
# @param {string} GIT_COMMIT_MESSAGE - Git commit message text
44+
#
45+
# @example
46+
# make lint-commit-message
47+
#/
48+
lint-commit-message:
49+
ifeq ($(GIT_COMMIT_LINTER), commitlint)
50+
$(QUIET) NODE_ENV="$(NODE_ENV)" NODE_PATH="$(NODE_PATH)" GIT_COMMIT_MESSAGE="$(GIT_COMMIT_MESSAGE)" $(MAKE) -f $(this_file) commitlint-message
51+
endif
52+
53+
.PHONY: lint-commit-message
54+
55+
#/
56+
# Lints a list of files, each containing a commit message.
57+
#
58+
# @param {string} FILES - list of file paths
59+
# @param {*} [FAST_FAIL] - flag indicating whether to stop linting upon encountering a lint error
60+
#
61+
# @example
62+
# make lint-commit-files FILES='/foo/commit.txt'
63+
#/
64+
lint-commit-files:
65+
ifeq ($(GIT_COMMIT_LINTER), commitlint)
66+
$(QUIET) NODE_ENV="$(NODE_ENV)" NODE_PATH="$(NODE_PATH)" FILES="$(FILES)" $(MAKE) -f $(this_file) commitlint-files
67+
endif
68+
69+
.PHONY: lint-commit-files

tools/make/lib/lint/git/commitlint.mk

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ COMMITLINT ?= $(BIN_DIR)/commitlint
3333
COMMITLINT_FLAGS ?= \
3434
--cwd "$(ROOT_DIR)" \
3535
--config "$(CONFIG_DIR)/commitlint/.commitlintrc.js" \
36-
--edit
36+
--verbose
3737

3838

3939
# RULES #
4040

4141
#/
42-
# Lints a commit message.
42+
# Lints a commit.
4343
#
4444
# ## Notes
4545
#
@@ -52,7 +52,60 @@ COMMITLINT_FLAGS ?= \
5252
#/
5353
commitlint: $(NODE_MODULES)
5454
$(QUIET) mv $(ROOT_DIR)/tsconfig.json $(ROOT_DIR)/tsconfig.json.tmp
55-
$(QUIET) NODE_PATH="$(NODE_PATH)" $(NODE) "$(COMMITLINT)" $(COMMITLINT_FLAGS) || (mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1)
55+
$(QUIET) NODE_PATH="$(NODE_PATH)" $(NODE) "$(COMMITLINT)" $(COMMITLINT_FLAGS) --edit || ( mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1 )
5656
$(QUIET) mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json
5757

5858
.PHONY: commitlint
59+
60+
#/
61+
# Lints a commit message.
62+
#
63+
# ## Notes
64+
#
65+
# - We have to temporarily move the `tsconfig` file, as `commitlint` (erroneously) attempts to use the file for compiling TypeScript.
66+
#
67+
# @private
68+
#
69+
# @example
70+
# make commitlint-message
71+
#/
72+
commitlint-message: $(NODE_MODULES)
73+
$(QUIET) mv $(ROOT_DIR)/tsconfig.json $(ROOT_DIR)/tsconfig.json.tmp
74+
$(QUIET) ( printf "$(GIT_COMMIT_MESSAGE)" | NODE_PATH="$(NODE_PATH)" $(NODE) "$(COMMITLINT)" $(COMMITLINT_FLAGS) ) || ( mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1 )
75+
$(QUIET) mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json
76+
77+
.PHONY: commitlint-message
78+
79+
#/
80+
# Lints a list of files, each containing a commit message.
81+
#
82+
# ## Notes
83+
#
84+
# - We have to temporarily move the `tsconfig` file, as `commitlint` (erroneously) attempts to use the file for compiling TypeScript.
85+
#
86+
# @private
87+
#
88+
# @param {string} FILES - list of file paths
89+
# @param {*} [FAST_FAIL] - flag indicating whether to stop linting upon encountering a lint error
90+
#
91+
# @example
92+
# make commitlint-files FILES='/foo/commit.txt'
93+
#/
94+
commitlint-files: $(NODE_MODULES)
95+
$(QUIET) mv $(ROOT_DIR)/tsconfig.json $(ROOT_DIR)/tsconfig.json.tmp
96+
ifeq ($(FAIL_FAST), true)
97+
$(QUIET) for file in $(FILES); do \
98+
echo ''; \
99+
echo "Linting commit message:"; \
100+
( cat $$file | grep -v '^#' | $(NODE) "$(COMMITLINT)" $(COMMITLINT_FLAGS) ) || ( mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json && exit 1 ); \
101+
done
102+
else
103+
$(QUIET) for file in $(FILES); do \
104+
echo ''; \
105+
echo "Linting commit message"; \
106+
( cat $$file | grep -v '^#' | $(NODE) "$(COMMITLINT)" $(COMMITLINT_FLAGS) ) || echo 'Linting failed.'; \
107+
done
108+
endif
109+
$(QUIET) mv $(ROOT_DIR)/tsconfig.json.tmp $(ROOT_DIR)/tsconfig.json
110+
111+
.PHONY: commitlint-files

0 commit comments

Comments
 (0)