From 2789c0d1b4bf333af811d94ad6e4d99b924e6b8d Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Thu, 13 Feb 2020 23:10:39 -0500 Subject: [PATCH] add bcs.sh (Bad Code Search) fixes #22. GitOrigin-RevId: f13bee397fc4bfc957f07cb89d1766ee55ae0415 --- tools/scripts/bcs.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tools/scripts/bcs.sh diff --git a/tools/scripts/bcs.sh b/tools/scripts/bcs.sh new file mode 100644 index 0000000..0bf85e0 --- /dev/null +++ b/tools/scripts/bcs.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +# Bad Code Search, with syntax highlighting. +# +# Sample usage: +# bcs.sh --file Lines.*cs Vector3 +# bcs.sh -i new vector3 + +# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash +POSITIONAL=() +while [[ $# -gt 0 ]] +do +key="$1" + +case $key in + -f|--file) + FILE="$2" + shift # past argument + shift # past value + ;; + -i|--ignore-case) + GREP_FLAGS="-i" + shift # past argument + ;; + *) # unknown option + POSITIONAL+=("$1") # save it in an array for later + shift # past argument + ;; +esac +done +set -- "${POSITIONAL[@]}" # restore positional parameters + +if [[ -z $@ ]]; then + echo 'Usage: bcs.sh [-i] [-f/--file FILE_PATTERN] QUERY' + exit 1 +fi + +# highlight(1) sticks some ANSI cruft into the output. +# We just turn spaces into "match anything", which kinda ignores the cruft. +QUERY=`echo $@ | sed -e "s/ /.*/g"` + +if [[ -n $FILE ]]; then + files=$(git ls-tree -r master --name-only | grep "$FILE") + if [[ -z $files ]]; then + echo "no files matched" + exit 0 + fi + + for file in `grep -l $GREP_FLAGS "$QUERY" $files`; + do + echo $file: + highlight --force --line-numbers -O xterm256 --stdout $file | \ + grep -C 5 $GREP_FLAGS "$QUERY" --label=$file | perl -pne s%^--\$%$file:% + done +else + for file in `grep -l $GREP_FLAGS "$QUERY" $(git ls-tree -r master --name-only)` + do + echo $file: + highlight --force --line-numbers -O xterm256 --stdout $file | \ + grep -C 5 $GREP_FLAGS "$QUERY" --label=$file | perl -pne s%^--\$%$file:% + done +fi