Skip to content

Instantly share code, notes, and snippets.

@qapquiz
Created January 28, 2020 12:18
Show Gist options
  • Save qapquiz/c75e26eb1e842d5818a60bb79e2659ef to your computer and use it in GitHub Desktop.
Save qapquiz/c75e26eb1e842d5818a60bb79e2659ef to your computer and use it in GitHub Desktop.
Unity Git Hooks -> There are three files
#!/bin/bash
#
# An example hook script to verify what is about to be committed.
# 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.
#
# To enable this hook, rename this file to "pre-commit".
ASSETS_DIR="$(git config --get unity3d.assets-dir || echo "Assets")"
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
git diff --cached --name-only --diff-filter=A -z $against -- "$ASSETS_DIR" | while read -d $'\0' f; do
ext="${f##*.}"
base="${f%.*}"
if [ "$ext" = "meta" ]; then
if [ $(git ls-files --cached -- "$base" | wc -l) = 0 ]; then
cat <<EOF
Error: Redudant meta file.
Meta file \`$f' is added, but \`$base' is not in the git index.
Please add \`$base' to git as well.
EOF
exit 1
fi
else
p="$f"
while [ "$p" != "$ASSETS_DIR" ]; do
if [ $(git ls-files --cached -- "$p.meta" | wc -l) = 0 ]; then
cat <<EOF
Error: Missing meta file.
Asset \`$f' is added, but \`$p.meta' is not in the git index.
Please add \`$p.meta' to git as well.
EOF
exit 1
fi
p="${p%/*}"
done
fi
done
ret="$?"
if [ "$ret" != 0 ]; then
exit "$ret"
fi
git diff --cached --name-only --diff-filter=D -z $against -- "$ASSETS_DIR" | while read -d $'\0' f; do
ext="${f##*.}"
base="${f%.*}"
if [ "$ext" = "meta" ]; then
if [ $(git ls-files --cached -- "$base" | wc -l) != 0 ]; then
cat <<EOF
Error: Redudant meta file.
Meta file \`$f' is removed, but \`$base' is still in the git index.
Please remove \`$base' from git as well.
EOF
exit 1
fi
else
p="$f"
while [ "$p" != "$ASSETS_DIR" ]; do
if [ $(git ls-files --cached -- "$p" | wc -l) = 0 ] && [ $(git ls-files --cached -- "$p.meta" | wc -l) != 0 ]; then
cat <<EOF
Error: Missing meta file.
Asset \`$f' is removed, but \`$p.meta' is still in the git index.
Please remove \`$p.meta' from git as well.
EOF
exit 1
fi
p="${p%/*}"
done
fi
done
ret="$?"
if [ "$ret" != 0 ]; then
exit "$ret"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment