Skip to content

Commit cd79a28

Browse files
committed
feat: add pre-commit lfs audit
1 parent 312e4ee commit cd79a28

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎lefthook.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pre-commit:
22
parallel: true
33
jobs:
4+
- name: "lfs: audit staged files"
5+
run: bash tools/check-lfs.sh
46
- name: "lint"
57
run: mvn -B -q checkstyle:check
68
- name: "format"

‎tools/check-lfs.sh‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
# Fails if any staged file carries the git-lfs filter attribute but is stored
3+
# in the index as a raw blob rather than an LFS pointer.
4+
#
5+
# Audit-only: never modifies the index or working tree.
6+
set -euo pipefail
7+
8+
raw=$(
9+
git diff --cached --name-only --diff-filter=ACMR \
10+
| while IFS= read -r f; do
11+
[[ -f "$f" ]] || continue
12+
attr=$(git check-attr filter -- "$f" | awk -F': ' '{print $NF}')
13+
[[ "$attr" == "lfs" ]] || continue
14+
if ! git cat-file -p ":$f" 2>/dev/null \
15+
| head -c 100 \
16+
| grep -q '^version https://git-lfs'; then
17+
echo "$f"
18+
fi
19+
done
20+
)
21+
22+
if [[ -n "$raw" ]]; then
23+
echo "ERROR: these staged files should be in git-lfs but are stored as raw blobs:" >&2
24+
printf '%s\n' "$raw" | sed 's/^/ /' >&2
25+
echo >&2
26+
echo "Fix: git add --renormalize . && git commit" >&2
27+
exit 1
28+
fi
29+
30+
exit 0

0 commit comments

Comments
 (0)