Last active
October 30, 2024 21:49
-
-
Save celina-lopez/ae334d698ad38bcee55af1d62dc253f0 to your computer and use it in GitHub Desktop.
Commit Todo's to README - pre-commit
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 | |
# add this file to your .githooks folder, name it pre-commit | |
# in your .gitconfig file, add the below: | |
# [core] | |
# hooksPath = .githooks | |
# after run the following command in console: git config --local include.path .gitconfig | |
# Optionally, add ## TODOs: to the end of your README.md | |
# Start committing! | |
# Example: | |
# - [ ] (celina) Rspec testing needed - app/jobs/apply_program_job.rb | |
NEW_TODOS="" | |
# Get the list of staged files and filter for .rb and .erb extensions | |
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep -E '\.(rb|erb)$') | |
# Set IFS to handle filenames with spaces correctly | |
IFS=$'\n' | |
# Iterate over each staged file | |
for FILE in $STAGED_FILES; do | |
if [ -f "$FILE" ]; then | |
# Get the staged diff for the file and search for lines containing 'TODO' | |
NEW_TODO=$(git diff --cached -- "$FILE" --no-ext-diff --unified=0 -a --no-prefix | \ | |
grep -E "^\+.*TODO" | sed -E "s/^\+.*TODO:\s*(.*)/- [ ] \1 - ${FILE//\//\\/}/" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') | |
if [ -n "$NEW_TODO" ]; then | |
# Append the new TODOs to the NEW_TODOS variable | |
NEW_TODOS="${NEW_TODOS}${NEW_TODO}\n" | |
fi | |
else | |
echo "Warning: $FILE does not exist or is not a regular file." | |
fi | |
done | |
# Restore default IFS | |
unset IFS | |
# Exit if no new TODOs were found | |
if [ -z "$NEW_TODOS" ]; then | |
exit 0 | |
fi | |
# Append NEW_TODOS to README.md under a TODO section | |
echo -e "\n$NEW_TODOS" >> README.md | |
# Add README.md to the commit | |
git add README.md | |
echo -e "\n✅ README.md updated with new TODOs" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment