-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-pre-commit
More file actions
executable file
·29 lines (24 loc) · 989 Bytes
/
Copy pathgit-pre-commit
File metadata and controls
executable file
·29 lines (24 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bash
#
# git pre-commit hook: block a commit if it would introduce a broken Markdown link.
#
# Install:
# cp examples/git-pre-commit .git/hooks/pre-commit
# chmod +x .git/hooks/pre-commit
#
# It only checks the Markdown files you're actually committing (fast), using link-check.js.
# Bypass once with `git commit --no-verify` if you really must.
#
# Assumes link-check.js is at the repo root. Change SCRIPTS_DIR if it lives elsewhere.
set -euo pipefail
SCRIPTS_DIR="${SCRIPTS_DIR:-$(git rev-parse --show-toplevel)}"
# Staged, added/copied/modified Markdown files only.
mapfile -t staged < <(git diff --cached --name-only --diff-filter=ACM -- '*.md' '*.markdown')
if [ "${#staged[@]}" -eq 0 ]; then
exit 0
fi
echo "pre-commit: checking links in ${#staged[@]} staged Markdown file(s)…"
if ! node "$SCRIPTS_DIR/link-check.js" "${staged[@]}"; then
echo "✗ Broken Markdown link(s) found — commit blocked. Fix them or use 'git commit --no-verify'."
exit 1
fi