the static git browser that builds this site
git clone https://git.lucas.co/gitsite.git
pre-commit-scan (1.6K)
1 #!/bin/sh
2 # pre-commit hook: refuse a commit whose staged changes look like a secret.
3 #
4 # Installed by install-hooks.sh next to post-commit-push, and it exists
5 # because of that hook: a commit here is on GitHub seconds later, so there is
6 # no window to notice a pasted credential before it is public. Only the
7 # staged diff is scanned, so it costs well under a second.
8 #
9 # Uses gitleaks when it is on PATH. Without it the commit goes through, with a
10 # warning on stderr -- a fresh machine must still be able to commit -- so
11 # install it to get the protection: `pacman -S gitleaks` on Arch, `brew
12 # install gitleaks` on macOS, or a binary from
13 # https://github.com/gitleaks/gitleaks/releases.
14 #
15 # A finding blocks the commit and prints where it is, value redacted. For a
16 # true false positive, allowlist it in the repo's .gitleaks.toml or pass
17 # --no-verify to that one commit -- remembering that the push hook publishes
18 # whatever gets past this one.
19
20 if ! command -v gitleaks >/dev/null 2>&1; then
21 echo "pre-commit: gitleaks is not installed -- staged changes were NOT scanned for secrets" >&2
22 exit 0
23 fi
24
25 # `gitleaks git --pre-commit` is the 8.19+ spelling; older releases have
26 # `protect` instead. Both scan only what is staged.
27 if gitleaks git --help >/dev/null 2>&1; then
28 gitleaks git --pre-commit --staged --redact --verbose --no-banner
29 else
30 gitleaks protect --staged --redact --verbose --no-banner
31 fi
32 status=$?
33 if [ "$status" -ne 0 ]; then
34 echo "pre-commit: the staged changes look like they contain a secret -- commit refused." >&2
35 echo " false positive? allowlist it in .gitleaks.toml, or git commit --no-verify" >&2
36 fi
37 exit "$status"