Skip to content

Instantly share code, notes, and snippets.

@mslinn
Created February 20, 2012 15:42
Show Gist options
  • Save mslinn/1869710 to your computer and use it in GitHub Desktop.
Save mslinn/1869710 to your computer and use it in GitHub Desktop.
Find the file in a git repo with the most occurrences of a string
#!/bin/bash
# Author: Mike Slinn
function FILES {
git grep -c $1|sort -t : -k 2 -n -r
}
ACTION=
while getopts "h1n" opt; do
case $opt in
1) ACTION=TOP ;;
n) ACTION=NOTEPAD ;;
*) ACTION=HELP ;;
esac
done
shift $(($OPTIND-1))
if [ "$ACTION" == TOP ]; then
echo `FILES $1|head -1`
elif [ "$ACTION" == NOTEPAD ]; then
notepad `FILES $1|head -1|awk '{print $1}'`
elif [ "$ACTION" == HELP ]; then
echo "Find the file in a git repo with the most occurrences of a string"
echo "Usage: $0 [option] searchString, where option is one of:" >&2
echo " -1 only show file with the most hits" >&2
echo " -n edit file with the most hits" >&2
else
FILES $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment