|
|
@ -3,11 +3,14 @@ |
|
|
|
# Bad Code Search, with syntax highlighting. |
|
|
|
# |
|
|
|
# Sample usage: |
|
|
|
# bcs.sh --file Lines.*cs Vector3 |
|
|
|
# bcs.sh --file "\.cs$" Vector3 |
|
|
|
# bcs.sh -i new vector3 |
|
|
|
# bcs.sh -C 10 new Vector3 |
|
|
|
|
|
|
|
# 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" |
|
|
@ -22,6 +25,11 @@ case $key in |
|
|
|
GREP_FLAGS="-i" |
|
|
|
shift # past argument |
|
|
|
;; |
|
|
|
-C|--context) |
|
|
|
CONTEXT="$2" |
|
|
|
shift # past argument |
|
|
|
shift # past value |
|
|
|
;; |
|
|
|
*) # unknown option |
|
|
|
POSITIONAL+=("$1") # save it in an array for later |
|
|
|
shift # past argument |
|
|
@ -31,7 +39,7 @@ done |
|
|
|
set -- "${POSITIONAL[@]}" # restore positional parameters |
|
|
|
|
|
|
|
if [[ -z $@ ]]; then |
|
|
|
echo 'Usage: bcs.sh [-i] [-f/--file FILE_PATTERN] QUERY' |
|
|
|
echo 'Usage: bcs.sh [-i] [-f/--file FILE_PATTERN] [-C/--context NUM_LINES] QUERY' |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
@ -40,23 +48,23 @@ fi |
|
|
|
QUERY=`echo $@ | sed -e "s/ /.*/g"` |
|
|
|
|
|
|
|
if [[ -n $FILE ]]; then |
|
|
|
files=$(git ls-tree -r master --name-only | grep "$FILE") |
|
|
|
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`; |
|
|
|
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:% |
|
|
|
grep -C ${CONTEXT} ${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)` |
|
|
|
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:% |
|
|
|
grep -C ${CONTEXT} ${GREP_FLAGS} "${QUERY}" --label=$file | perl -pne s%^--\$%$file:% |
|
|
|
done |
|
|
|
fi |