Skip to content

Instantly share code, notes, and snippets.

@W-A-James
Last active December 2, 2022 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save W-A-James/b5670db92746ad9a24b0b701452aa892 to your computer and use it in GitHub Desktop.
Save W-A-James/b5670db92746ad9a24b0b701452aa892 to your computer and use it in GitHub Desktop.
Checks for todos in the current repository
#!/usr/bin/env bash
set -e
todo_message=""
directory="."
has_ripgrep() {
which "rg" | grep "not found"
case $? in
0) # no ripgrep found
echo "0"
;;
1) # ripgrep present
echo "1"
;;
*) # grep error
echo "Error!" 1>&2
exit 1
;;
esac
}
is_git_repo() {
git status 2>&1 | grep "fatal"
case $? in
0) # not git repo
echo "0"
;;
1) # is git repo
echo "1"
;;
*) # grep error
echo "grep error!" 1>&2
exit 1
;;
esac
}
usage() {
echo "Usage: $0 [ OPTIONS ]"
echo
echo "OPTIONS:"
echo " -m MESSAGE Check for messages of form (TODO|FIXME)\(MESSAGE\)"
echo " when this is not set, it sets mesage to the current git branch"
echo " -d DIRECTORY Recursively search files starting from <DIRECTORY>. Defaults to current directory"
echo " -h Print this help message"
}
#### MAIN ####
while getopts "hm:d:" arg; do
case $arg in
m)
todo_message=${OPTARG}
;;
h)
usage
exit 0
;;
d)
directory=${OPTARG}
;;
*)
usage
exit 1
;;
esac
done
cd $directory
if [ -z $todo_message ]; then
# Check if this is a valid git repostory
if [ "$(is_git_repo)" == "1" ]; then
todo_message=$(git status | head -1 | awk -F ' ' '{ print $3 }')
else
echo "'$directory' not a git repository and is not child of a git repository"
exit 1
fi
fi
if [ "$(has_ripgrep)" == "1" ]; then
rg -i "(TODO|FIXME)\($todo_message\)" .
else
grep -i -r "(TODO|FIXME)\($todo_message\)" .
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment