I rely heavily on pre-commit for my projects, including this blog
. When I commit changes, it ensures my files follow my syntax standards, compresses images, and embeds external files - like the installation script below.
pre-commit is an excellent tool and one of the first things I set up on new projects. However, it has one significant drawback: speed. It isn’t especially fast, and it noticeably slows down my commits. In projects with many linters and verifiers, execution times can exceed one minute. I wanted to optimize this and discovered prek .
I’m usually skeptical of project marketing claims like “it’s written in Rust, so it’s safe and fast,” but my recent experience with uv changed my perspective after years using poetry. I decided to try prek. The best part? It’s a drop-in replacement. I don’t need to change my configs - they stay exactly as they are - but everything loads and runs faster. Since I maintain dozens of repositories, I wanted to switch them all at once. I wrote the script below to automate this. It recursively searches the current directory, finds all .pre-commit-config.yaml files, and runs prek install -f in each directory.
# install-prek.sh
#!/bin/bash
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Find all .pre-commit-config.yaml files and get unique directories
echo -e "${BLUE}Searching for .pre-commit-config.yaml files...${NC}"
directories=$(find . -name ".pre-commit-config.yaml" -type f -exec dirname {} \; | sort -u)
if [ -z "$directories" ]; then
echo -e "${RED}No .pre-commit-config.yaml files found${NC}"
exit 1
fi
# Count directories
count=$(echo "$directories" | wc -l)
echo -e "${BLUE}Found $count director(ies) with .pre-commit-config.yaml${NC}"
echo ""
# Track results
success_count=0
failure_count=0
failed_dirs=()
# Iterate over each directory
while IFS= read -r dir; do
echo -e "${BLUE}Processing: $dir${NC}"
if (cd "$dir" && prek install -f); then
echo -e "${GREEN}✓ Successfully installed prek in $dir${NC}"
((success_count++))
else
echo -e "${RED}✗ Failed to install prek in $dir${NC}"
failed_dirs+=("$dir")
((failure_count++))
fi
echo ""
done <<< "$directories"
# Summary
echo -e "${BLUE}========== Summary ==========${NC}"
echo -e "${GREEN}Successful: $success_count${NC}"
echo -e "${RED}Failed: $failure_count${NC}"
if [ $failure_count -gt 0 ]; then
echo -e "\n${RED}Failed directories:${NC}"
for dir in "${failed_dirs[@]}"; do
echo " - $dir"
done
exit 1
fi
echo -e "\n${GREEN}All installations completed successfully!${NC}"
exit 0
You can download the script directly
. Add chmod +x install-prek.sh and you’re ready to convert your repositories.
