Skip to content

Instantly share code, notes, and snippets.

@Kleptine
Created February 4, 2026 23:09
Show Gist options
  • Select an option

  • Save Kleptine/73c926940d02952653fcb3eec21153a5 to your computer and use it in GitHub Desktop.

Select an option

Save Kleptine/73c926940d02952653fcb3eec21153a5 to your computer and use it in GitHub Desktop.
git precommit hook for pontoco
#!/bin/sh
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
branch="$(git rev-parse --abbrev-ref HEAD)"
# Reject commits to the main branch. You could still do this by forcing it,
# but in 99% of cases this is a mistake, especially for non-engineers.
if [ "$branch" = "main" ]; then
echo "Error: You are currently on the 'main' branch."
echo "You must create a new branch in Fork before making a commit."
exit 1
fi
# Check for the edge case where this is the initial commit.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi
# Redirect output to stderr.
exec 1>&2
# Check for adding files with spaces in their path
# ================================================
# Grep over (A)dded and (R)enamed files looking for any space characters.
files_with_spaces=$(git diff --staged --name-only --diff-filter=AR $against | grep " ")
if [ "$files_with_spaces" ]
then
cat <<- EOF
Error: File and folder names cannot contain spaces.
Remove any spaces from these file paths and try the commit again:
$files_with_spaces
EOF
exit 1
fi
# Check for non-LFS large files
# ==============================
LARGE_FILE_THRESHOLD_KB=5000
# Loop over staged files, filtered to only (A)dded, (M)odified, and (R)enamed files.
files_missing_lfs=$(git diff --staged --name-only --diff-filter=AMR $against | while read -r staged_file
do
# Skip empty lines.
if [ -z "$staged_file" ]
then
continue
fi
# Skip directories. These appear in diffs when submodules are changed.
if [ -d "$staged_file" ]
then
continue
fi
# Skip '.unity' files, even though some of them are large.
if [ "${staged_file##*.}" = "unity" ]
then
continue
fi
# Check that the disk size of the file is under the threshold.
# There are more elegant ways to get file size, but this is the most portable.
file_size_kb=$(ls -s -k "$staged_file" | cut -d ' ' -f 1)
if [ $file_size_kb -lt $LARGE_FILE_THRESHOLD_KB ]
then
continue
fi
# For large files, check that the file has the 'filter' attribute set with the value 'lfs'.
file_lfs_attr=$(git check-attr filter -- "$staged_file" | grep "filter: lfs")
if [ -z "$file_lfs_attr" ]
then
echo "${staged_file} (${file_size_kb}kb)"
fi
done)
if [ "$files_missing_lfs" ]
then
cat <<- EOF
Error: Large files must be tracked in LFS.
The following files are over the 'large file' size (${LARGE_FILE_THRESHOLD_KB}kb) but are not tracked in LFS.
$files_missing_lfs
Run this command to track this file type in LFS, replacing 'ext' with the file extension:
git lfs track "*.ext"
Then stage your files and the modified '.gitattributes' file and try the commit again.
EOF
exit 1
fi
# Check for invalid FBX files
# =============================
fbx_files=$(git diff --staged --name-only --diff-filter=AMR $against | grep -i "\.fbx$" | tr '\n' " ")
OSTYPE=$(uname | tr '[:upper:]' '[:lower:]')
if [ -n "$fbx_files" ]; then
if echo "$OSTYPE" | grep -q "mingw"
then
./Tools/Hooks/fbx_sanitizer.exe $fbx_files
elif echo "$OSTYPE" | grep -q "linux"
then
./Tools/Hooks/fbx_sanitizer_linux $fbx_files
elif echo "$OSTYPE" | grep -q "darwin"
then
./Tools/Hooks/fbx_sanitizer_mac $fbx_files
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment