mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 18:51:59 +00:00
@chrsmith suggested this idea, which I like very much as well. Pretty straightforward: - if you're adding something you really don't want to commit and suspect your future self to forget about it, you can add `FORBIDCOMMIT` anywhere in your changes, and precommit will prevent you from accidentally committing it. - check is case insensitive. I went for this instead of `NOCOMMIT` because it could be legitimately be used for a var with the number of commits for example. And that's not really something we want to add a pragma to disable the string itself for either. ## Test plan 
33 lines
999 B
Bash
Executable File
33 lines
999 B
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -eu
|
|
|
|
# Select the files to inspect. We don't want to list files which are deleted, as it makes no sense
|
|
# to look for a token being committed in those.
|
|
#
|
|
# We use --diff-filter, that tells git to only include in the diff files that were:
|
|
# - "A" added
|
|
# - "C" copied
|
|
# - "M" modified
|
|
# - "R" renamed
|
|
files=$(git diff --name-only --staged --diff-filter ACMR)
|
|
|
|
function check() {
|
|
local file="$1"
|
|
if [[ "$file" == "dev/forbidcommit.sh" || "$file" == ".pre-commit-config.yaml" ]]; then
|
|
exit 0
|
|
fi
|
|
|
|
# -i means case-insensitive
|
|
# -F means searsch for a fixed string, i.e. no pattern matching.
|
|
if grep -iF 'FORBIDCOMMIT' "$file"; then
|
|
echo "🔴 Found a FORBIDCOMMIT string in git staged file: $file."
|
|
echo "You most likely added this yourself to prevent yourself from accidentally committing that file."
|
|
echo "Please look at your changes carefully before removing the FORBIDCOMMIT pragma."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
export -f check
|
|
parallel check ::: "$files"
|