bcs: add -x argument to specify a file extension

GitOrigin-RevId: 0fa5ad849d0462d085c29665276bcbc5cdf4944a
This commit is contained in:
Colin McMillen 2020-02-13 23:41:51 -05:00
parent 5210ef7398
commit a84231582c

View File

@ -3,14 +3,17 @@
# Bad Code Search, with syntax highlighting.
#
# Sample usage:
# bcs.sh --file "\.cs$" Vector3
# bcs.sh --file "Line" Vector3
# bcs.sh -i new vector3
# bcs.sh -C 10 new Vector3
# bcs.sh -x py sprite
# bcs.sh -i -x cs -f World spritebatch
# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
POSITIONAL=()
GREP_FLAGS=""
CONTEXT=3
while [[ $# -gt 0 ]]
do
key="$1"
@ -21,6 +24,11 @@ case $key in
shift # past argument
shift # past value
;;
-x|--extension)
EXTENSION="\.$2\$"
shift # past argument
shift # past value
;;
-i|--ignore-case)
GREP_FLAGS="-i"
shift # past argument
@ -39,7 +47,7 @@ done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ -z $@ ]]; then
echo 'Usage: bcs.sh [-i] [-f/--file FILE_PATTERN] [-C/--context NUM_LINES] QUERY'
echo 'Usage: bcs.sh [-i] [-f/--file FILE_PATTERN] [-x/--extension EXTENSION] [-C/--context NUM_LINES] QUERY'
exit 1
fi
@ -47,6 +55,10 @@ fi
# We just turn spaces into "match anything", which kinda ignores the cruft.
QUERY=`echo $@ | sed -e "s/ /.*/g"`
if [[ -n $EXTENSION ]]; then
FILE="${FILE}.*${EXTENSION}"
fi
if [[ -n $FILE ]]; then
files=$(git ls-tree -r master --name-only | grep "${FILE}")
if [[ -z $files ]]; then