Skip to content
41 changes: 41 additions & 0 deletions tools/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
#
# Copyright 2025 SmartThings, Inc.
# Licensed under the Apache License, Version 2.0
#
# pre-commit
#
# - Soft link into .git/hooks/pre-commit
# - $ ln -s tools/pre-commit .git/hooks/pre-commit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's different with your version of ln, but usually the first argument is assigned to the symlink verbatim, rather than computed from where you invoked the command. So, it should be ../../tools/pre-commit.

# - Ensure executable

set -e

# Get changed files that are Added or Modified
files=$(git diff --staged --name-only --diff-filter=AM)

fail=0
for file in $files; do
# Only process .lua files
case "$file" in
*.lua)
# Skip if not a regular file
[ -f "$file" ] || continue

# Check if file is not empty, is a SmartThings driver and has the copyright header
if [ -s "$file" ] && [[ "$file" =~ "drivers/SmartThings" ]]
&& [[ ! $(grep $file -P -e "-{2,} Copyright \d{4}(?:-\d{4})? SmartThings") ]]; then
echo "$file: SmartThings Copyright missing from file"
fail=1
fi

# Check if file is non-empty and does NOT end with a newline
if [ -s "$file" ] && [ -n "$(tail -c 1 "$file")" ]; then
echo "$file: Missing newline at end of file"
fail=1
fi
;;
esac
done

exit $fail