Skip to content

Commit 0ebc834

Browse files
Guard custom pre-commit hooks against untracked files (#725)
closes #707
1 parent 863bb4d commit 0ebc834

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

tools/check-image-names.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,30 @@
22

33
import sys
44
import os
5+
import subprocess
6+
7+
8+
def is_tracked(path: str) -> bool:
9+
"""
10+
Return True if the given path is tracked by git, False otherwise.
11+
12+
This guards the hook against local, untracked files that pre-commit
13+
might pick up when running outside of the normal commit workflow.
14+
"""
15+
result = subprocess.run(
16+
["git", "ls-files", "--error-unmatch", path],
17+
stdout=subprocess.DEVNULL,
18+
stderr=subprocess.DEVNULL,
19+
)
20+
return result.returncode == 0
21+
522

623
problems = False
724
for file in sys.argv[1:]:
25+
# Skip files that are not tracked by git
26+
if not is_tracked(file):
27+
continue
28+
829
parts = file.split(os.sep)
930
# Ignore non-interesting files
1031
if len(parts) != 3 or parts[1] != "images":

tools/check-size.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ echo "Limit for gifs: ${MAXIMUMGIFSIZE} kb"
1818
for tutorial in $tutorials; do
1919
images=$(find ./"${tutorial}"/images -type f 2> /dev/null | sed "s/^.\///")
2020
for img in $images; do
21+
# Only check files that are tracked by git
22+
git ls-files --error-unmatch "$img" >/dev/null 2>&1 || continue
2123
actualsize=$(du -k "$img" | cut -f 1)
2224
# Check gifs
2325
if [[ "${img}" == *.gif || "${img}" == *.webp || "${img}" == *.webm ]]; then

tools/check.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ for tutorial in $tutorials; do
1111
# Check permalinks
1212
docs=$(find "./$tutorial" -maxdepth 1 -type f -name "*.md" -print0 | xargs -0 grep -l "permalink:" | sed "s/^.\///")
1313
for doc in $docs; do
14+
# Only check files that are tracked by git
15+
git ls-files --error-unmatch "$doc" >/dev/null 2>&1 || continue
16+
1417
link=$(grep "permalink:" "$doc" | sed "s/permalink: \+//")
1518
prefix="tutorials-$tutorial"
1619

@@ -28,6 +31,8 @@ for tutorial in $tutorials; do
2831
images=$(find "./$tutorial/images" -type f 2> /dev/null | sed "s/^.\///")
2932
prefix="tutorials-$tutorial-"
3033
for img in $images; do
34+
# Only check files that are tracked by git
35+
git ls-files --error-unmatch "$img" >/dev/null 2>&1 || continue
3136
if ! [[ $img =~ ^$tutorial/images/$prefix ]]; then
3237
echo "$img: error: wrong filename"
3338
echo "$img: note: expected prefix \"$prefix\""
@@ -42,6 +47,8 @@ done
4247
# Check quickstart
4348
docs=$(find ./quickstart -maxdepth 1 -type f -name "*.md" -print0 | xargs -0 grep -l "permalink:" | sed "s/^.\///")
4449
for doc in $docs; do
50+
# Only check files that are tracked by git
51+
git ls-files --error-unmatch "$doc" >/dev/null 2>&1 || continue
4552
link=$(grep "permalink:" "$doc" | sed "s/permalink: \+//")
4653
prefix="quickstart"
4754

@@ -59,6 +66,8 @@ done
5966
images=$(find ./quickstart/images -type f 2> /dev/null | sed "s/^.\///")
6067
prefix="quickstart-"
6168
for img in $images; do
69+
# Only check files that are tracked by git
70+
git ls-files --error-unmatch "$img" >/dev/null 2>&1 || continue
6271
if ! [[ $img =~ ^quickstart/images/$prefix ]]; then
6372
echo "$img: error: wrong filename"
6473
echo "$img: note: expected prefix \"$prefix\""

0 commit comments

Comments
 (0)