File: pre-commit

package info (click to toggle)
node-stdlib 0.0.96%2Bds1%2B~cs0.0.429-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 421,476 kB
  • sloc: javascript: 1,562,831; ansic: 109,702; lisp: 49,823; cpp: 27,224; python: 7,871; sh: 6,807; makefile: 6,089; fortran: 3,102; awk: 387
file content (365 lines) | stat: -rw-r--r-- 10,936 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2017 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# A Git hook called by `git commit`. If this scripts exits with a non-zero status, the commit will be aborted.
#
# This hook is called with no arguments.

# shellcheck disable=SC2181


# VARIABLES #

# Determine root directory:
root=$(git rev-parse --show-toplevel)

# Define the path to a utility for linting filenames:
lint_filenames="${root}/lib/node_modules/@stdlib/_tools/lint/filenames/bin/cli"

# Define the path to a utility for linting package.json files:
lint_package_json="${root}/lib/node_modules/@stdlib/_tools/lint/pkg-json/bin/cli"

# Define the path to a utility for linting REPL help files:
lint_repl_help="${root}/lib/node_modules/@stdlib/_tools/lint/repl-txt/bin/cli"

# Define the path to ESLint configuration file for linting examples:
eslint_examples_conf="${root}/etc/eslint/.eslintrc.examples.js"

# Define the path to ESLint configuration file for linting tests:
eslint_tests_conf="${root}/etc/eslint/.eslintrc.tests.js"

# Define the path to ESLint configuration file for linting benchmarks:
eslint_benchmarks_conf="${root}/etc/eslint/.eslintrc.benchmarks.js"

# Define the path to cppcheck configuration file for linting examples:
cppcheck_examples_suppressions_list="${root}/etc/cppcheck/suppressions.examples.txt"

# Define the path to cppcheck configuration file for linting test fixtures:
cppcheck_tests_fixtures_suppressions_list="${root}/etc/cppcheck/suppressions.tests_fixtures.txt"

# Define the path to cppcheck configuration file for linting benchmarks:
cppcheck_benchmarks_suppressions_list="${root}/etc/cppcheck/suppressions.benchmarks.txt"


# FUNCTIONS #

# Defines an error handler.
#
# $1 - error status
on_error() {
	cleanup
	exit "$1"
}

# Runs clean-up tasks.
cleanup() {
	echo '' >&2
}

# Runs initialization tasks.
init() {
	return 0
}

# Checks for non-ASCII filenames (to ensure cross platform portability).
check_filenames() {
	local num_files
	local against
	local commit

	commit=$(git rev-parse --verify HEAD)
	if [[ -z "${commit}" ]]; then
		# This is the initial commit, so we diff against an empty tree object:
		against='4b825dc642cb6eb9a060e54bf8d69288fbee4904'
	else
		against='HEAD'
	fi
	# We exploit the fact that the printable range starts with the space character and ends with the tilde. Note that the use of brackets around a `tr` range is okay here, (for portability to Solaris 10's /usr/bin/tr, it's even required), since the square bracket bytes happen to fall in the designated range.
	num_files=$(git diff --cached --name-only --diff-filter=ACR -z "${against}" | LC_ALL=C tr -d '[ -~]\0' | wc -c)

	if [[ "${num_files}" -ne 0 ]]; then
		echo 'Error: Attempting to add a non-ASCII filename. Non-ASCII filenames limit cross-platform portability. Please rename offending files before committing.' >&2
		return 1
	fi
	return 0
}

# Lints staged files.
run_lint() {
	local changed_files
	local files

	# Get the set of changed files (added, copied, modified, and renamed):
	changed_files=$(git diff --name-only --cached --diff-filter ACMR)

	# Lint filenames:
	echo "${changed_files}" | "${lint_filenames}"
	if [[ "$?" -ne 0 ]]; then
		echo '' >&2
		echo 'Filename lint errors.' >&2
		return 1
	fi

	# Lint Markdown files...
	files=$(echo "${changed_files}" | grep '\.md$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make FILES="${files}" lint-markdown-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'Markdown lint errors.' >&2
			return 1
		fi
	fi

	# Lint package.json files...
	files=$(echo "${changed_files}" | grep 'package\.json$' | grep -v 'datapackage\.json$' )
	if [[ -n "${files}" ]]; then
		echo "${files}" | "${lint_package_json}" >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'Package.json lint errors.' >&2
			return 1
		fi
	fi

	# Lint REPL help files...
	files=$(echo "${changed_files}" | grep 'repl\.txt$' )
	if [[ -n "${files}" ]]; then
		echo "${files}" | "${lint_repl_help}" >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'REPL help lint errors.' >&2
			return 1
		fi
	fi

	# Lint JavaScript source files...
	files=$(echo "${changed_files}" | grep '\.js$' | grep -v -e '/examples' -e '/test' -e '/benchmark' -e '^dist/' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make FILES="${files}" lint-javascript-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'JavaScript lint errors for source files.' >&2
			return 1
		fi
	fi

	# Lint JavaScript command-line interfaces...
	files=$(echo "${changed_files}" | grep '/bin/cli$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make FILES="${files}" lint-javascript-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'JavaScript lint errors for command-line interface files.' >&2
			return 1
		fi
	fi

	# Lint JavaScript examples files...
	files=$(echo "${changed_files}" | grep '/examples/.*\.js$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make JAVASCRIPT_LINTER=eslint ESLINT_CONF="${eslint_examples_conf}" FILES="${files}" lint-javascript-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'JavaScript lint errors for example files.' >&2
			return 1
		fi
	fi

	# Lint JavaScript test files...
	files=$(echo "${changed_files}" | grep '/test/.*\.js$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make JAVASCRIPT_LINTER=eslint ESLINT_CONF="${eslint_tests_conf}" FILES="${files}" lint-javascript-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'JavaScript lint errors for test files.' >&2
			return 1
		fi
	fi

	# Lint JavaScript benchmark files...
	files=$(echo "${changed_files}" | grep '/benchmark/.*\.js$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make JAVASCRIPT_LINTER=eslint ESLINT_CONF="${eslint_benchmarks_conf}" FILES="${files}" lint-javascript-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'JavaScript lint errors for benchmark files.' >&2
			return 1
		fi
	fi

	# Lint Python files...
	files=$(echo "${changed_files}" | grep '\.py$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make check-python-linters > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'Unable to lint Python files. Ensure that linters are installed.' >&2
		else
			make FILES="${files}" lint-python-files > /dev/null >&2
			if [[ "$?" -ne 0 ]]; then
				echo '' >&2
				echo 'Python lint errors.' >&2
				return 1
			fi
		fi
	fi

	# Lint R files...
	files=$(echo "${changed_files}" | grep '\.R$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make FILES="${files}" lint-r-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'R lint errors.' >&2
			return 1
		fi
	fi

	# Lint C source files...
	files=$(echo "${changed_files}" | grep '\.c$' | grep -v -e '/examples' -e '/test' -e '/benchmark' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make check-c-linters > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'Unable to lint C files. Ensure that linters are installed.' >&2
		else
			make FILES="${files}" lint-c-files > /dev/null >&2
			if [[ "$?" -ne 0 ]]; then
				echo '' >&2
				echo 'C lint errors for source files.' >&2
				return 1
			fi
		fi
	fi

	# Lint C examples files...
	files=$(echo "${changed_files}" | grep '/examples/.*\.c$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make check-c-linters > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'Unable to lint C files. Ensure that linters are installed.' >&2
		else
			make C_LINTER=cppcheck CPPCHECK_SUPPRESSIONS_LIST="${cppcheck_examples_suppressions_list}" FILES="${files}" lint-c-files > /dev/null >&2
			if [[ "$?" -ne 0 ]]; then
				echo '' >&2
				echo 'C lint errors for examples files.' >&2
				return 1
			fi
		fi
	fi

	# Lint C benchmark files...
	files=$(echo "${changed_files}" | grep '/benchmark/.*\.c$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make check-c-linters > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'Unable to lint C files. Ensure that linters are installed.' >&2
		else
			make C_LINTER=cppcheck CPPCHECK_SUPPRESSIONS_LIST="${cppcheck_benchmarks_suppressions_list}" FILES="${files}" lint-c-files > /dev/null >&2
			if [[ "$?" -ne 0 ]]; then
				echo '' >&2
				echo 'C lint errors for benchmark files.' >&2
				return 1
			fi
		fi
	fi

	# Lint C test fixtures files...
	files=$(echo "${changed_files}" | grep '/test/fixtures/.*\.c$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make check-c-linters > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'Unable to lint C files. Ensure that linters are installed.' >&2
		else
			make C_LINTER=cppcheck CPPCHECK_SUPPRESSIONS_LIST="${cppcheck_tests_fixtures_suppressions_list}" FILES="${files}" lint-c-files > /dev/null >&2
			if [[ "$?" -ne 0 ]]; then
				echo '' >&2
				echo 'C lint errors for test fixtures files.' >&2
				return 1
			fi
		fi
	fi

	# Lint shell script files...
	files=$(echo "${changed_files}" | while read -r file; do head -n1 "$file" | grep -q '^\#\!/usr/bin/env bash' && echo "$file"; done | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make check-shell-linters > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'Unable to lint shell script files. Ensure that linters are installed.' >&2
		else
			make FILES="${files}" lint-shell-files > /dev/null >&2
			if [[ "$?" -ne 0 ]]; then
				echo '' >&2
				echo 'Shell script lint errors.' >&2
				return 1
			fi
		fi
	fi

	# Lint TypeScript declaration files...
	files=$(echo "${changed_files}" | grep '\.d\.ts$' | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make TYPESCRIPT_DECLARATIONS_LINTER=dtslint FILES="${files}" lint-typescript-declarations-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'TypeScript declaration file lint errors.' >&2
			return 1
		fi
	fi

	# Lint license headers...
	files=$(echo "${changed_files}" | tr '\n' ' ')
	if [[ -n "${files}" ]]; then
		make FILES="${files}" lint-license-headers-files > /dev/null >&2
		if [[ "$?" -ne 0 ]]; then
			echo '' >&2
			echo 'License header lint errors.' >&2
			return 1
		fi
	fi

	# TODO: if datapackage.json, validate via schema

	return 0
}

# Main execution sequence.
main() {
	init
	if [[ "$?" -ne 0 ]]; then
		on_error 1
	fi
	check_filenames
	if [[ "$?" -ne 0 ]]; then
		on_error 1
	fi
	run_lint
	if [[ "$?" -ne 0 ]]; then
		on_error 1
	fi
	cleanup
	exit 0
}

# Run main:
main