Skip to content

Instantly share code, notes, and snippets.

@murtaza-u
Last active December 18, 2023 06:27
Show Gist options
  • Save murtaza-u/2975194eb48833846dfd9dc5e62de69c to your computer and use it in GitHub Desktop.
Save murtaza-u/2975194eb48833846dfd9dc5e62de69c to your computer and use it in GitHub Desktop.
Pre-commit Git hook to prevent accidentally committing `go.mod` with replace directive in it.
#!/bin/bash
root="$(git rev-parse --show-toplevel)"
cd "$root" || exit 1
if [[ ! -r go.mod ]]; then
exit 0
fi
mod="$(grep -E "^replace\s+.+\s{1}=>\s{1}.+$" go.mod 2>/dev/null)"
if [[ -z "$mod" ]]; then
exit 0
fi
echo "replace directive found in go.mod. Remove it before committing"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment