Skip to content

Commit ad0923f

Browse files
authored
Merge pull request #2601 from SmartThingsCommunity/CHAD-17043-pre-commit-newline-copyright-checks
CHAD-17043: Created pre-commit script to check for newline on all lua files, and copyright on all SmartThings lua driver files.
2 parents 52c9490 + ed4e8cd commit ad0923f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tools/pre-commit

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright 2025 SmartThings, Inc.
4+
# Licensed under the Apache License, Version 2.0
5+
#
6+
# pre-commit
7+
#
8+
# - Soft link into .git/hooks/pre-commit
9+
# - $ ln -s ../../tools/pre-commit .git/hooks/pre-commit
10+
# - Ensure executable
11+
12+
set -e
13+
14+
# Get changed files that are Added or Modified
15+
if [[ "$1" ]]; then
16+
files=$(find $1 -type f -follow -print)
17+
else
18+
files=$(git diff --staged --name-only --diff-filter=AM)
19+
fi
20+
21+
fail=0
22+
for file in $files; do
23+
# Only process .lua files
24+
case "$file" in
25+
*.lua)
26+
# Skip if not a regular file
27+
[ -f "$file" ] || continue
28+
29+
# Check if file is not empty, is a SmartThings driver and has the copyright header
30+
if [ -s "$file" ] && [[ "$file" =~ "drivers/SmartThings" ]] \
31+
&& [[ ! $(grep $file -P -e "-{2,} Copyright \d{4}(?:-\d{4})? SmartThings") ]]; then
32+
echo "$file: SmartThings Copyright missing from file"
33+
fail=1
34+
fi
35+
36+
# Check if file is non-empty and does NOT end with a newline
37+
if [ -s "$file" ] && [ -n "$(tail -c 1 "$file")" ]; then
38+
echo "$file: Missing newline at end of file"
39+
fail=1
40+
fi
41+
;;
42+
esac
43+
done
44+
45+
exit $fail

0 commit comments

Comments
 (0)