unity_template/.githooks/git-sync.sh

36 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env sh
# git-sync: push the current branch, then release my LFS locks on files that are fully pushed.
# Mirrors the "Push & release my locks" button in the Unity Git Locks window.
#
# Installed as `git sync` by setup-git-locks.sh. Run it from anywhere inside the repo.
#
# A lock is released only if its file has a clean working tree (nothing uncommitted),
# i.e. the version on the server is the one you finished. Files you still have changes
# on keep their lock. Locks owned by other people are skipped (and the server enforces it anyway).
set -e
cd "$(git rev-parse --show-toplevel)"
branch=$(git rev-parse --abbrev-ref HEAD)
printf 'Pushing %s...\n' "$branch"
git push origin HEAD
# Optional: only touch my own locks if a username is configured (matches the LFS lock owner).
me=$(git config --get wayLocks.username || true)
printf 'Releasing locks on fully pushed files...\n'
tab=$(printf '\t')
git lfs locks 2>/dev/null | while IFS="$tab" read -r path owner rest; do
[ -z "$path" ] && continue
if [ -n "$me" ] && [ "$owner" != "$me" ]; then
continue
fi
if [ -z "$(git status --porcelain -- "$path")" ]; then
if git lfs unlock "$path" >/dev/null 2>&1; then
printf ' released %s\n' "$path"
fi
fi
done
printf 'Done.\n'