Created
June 21, 2023 16:38
-
-
Save eevmanu/501f2fb1edaff8b32b301caf485a6e3e to your computer and use it in GitHub Desktop.
block git commit if any item from a list of strings (patterns) is contained in (match) any tracked files (or markdown files)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Replace with the actual directory path to search in | |
search_dir=$(git rev-parse --show-toplevel 2>/dev/null || git rev-parse --git-dir 2>/dev/null | sed 's/\.git$//') | |
# Replace with your list of strings | |
search_strings=("< pattern 1 >" "< pattern 2 >" "< pattern 3 >") | |
# Create a pattern by joining the search strings with a pipe (|) as the separator | |
pattern=$(printf "%s\\|" "${search_strings[@]}") | |
# Remove the trailing pipe | |
pattern=${pattern%\\|} | |
# Find all files matching the pattern "*.md" in the search directory and loop over them | |
while IFS= read -r -d '' file; do | |
matches=$(grep -i "$pattern" "$file") | |
if [ -n "$matches" ]; then | |
IFS=$'\n' read -r -d '' -a match_list <<< "$matches" | |
for match in "${match_list[@]}"; do | |
echo "file: $file match found: $match" | |
done | |
if [ "${#match_list[@]}" -gt 0 ]; then | |
echo "clean matches" | |
exit 1 | |
fi | |
fi | |
done < <(find "$search_dir" -name "*.md" -type f -print0) | |
# done < <(git ls-tree --name-only --full-tree -r HEAD) | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment