bcs: add ability to set context lines; clean up some expansions
GitOrigin-RevId: 718fc9bb92eb9a2643f262739a2017a6341cde1f
This commit is contained in:
parent
2789c0d1b4
commit
5210ef7398
@ -3,11 +3,14 @@
|
|||||||
# Bad Code Search, with syntax highlighting.
|
# Bad Code Search, with syntax highlighting.
|
||||||
#
|
#
|
||||||
# Sample usage:
|
# Sample usage:
|
||||||
# bcs.sh --file Lines.*cs Vector3
|
# bcs.sh --file "\.cs$" Vector3
|
||||||
# bcs.sh -i new 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
|
# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
|
||||||
POSITIONAL=()
|
POSITIONAL=()
|
||||||
|
GREP_FLAGS=""
|
||||||
|
CONTEXT=3
|
||||||
while [[ $# -gt 0 ]]
|
while [[ $# -gt 0 ]]
|
||||||
do
|
do
|
||||||
key="$1"
|
key="$1"
|
||||||
@ -22,6 +25,11 @@ case $key in
|
|||||||
GREP_FLAGS="-i"
|
GREP_FLAGS="-i"
|
||||||
shift # past argument
|
shift # past argument
|
||||||
;;
|
;;
|
||||||
|
-C|--context)
|
||||||
|
CONTEXT="$2"
|
||||||
|
shift # past argument
|
||||||
|
shift # past value
|
||||||
|
;;
|
||||||
*) # unknown option
|
*) # unknown option
|
||||||
POSITIONAL+=("$1") # save it in an array for later
|
POSITIONAL+=("$1") # save it in an array for later
|
||||||
shift # past argument
|
shift # past argument
|
||||||
@ -31,7 +39,7 @@ done
|
|||||||
set -- "${POSITIONAL[@]}" # restore positional parameters
|
set -- "${POSITIONAL[@]}" # restore positional parameters
|
||||||
|
|
||||||
if [[ -z $@ ]]; then
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@ -40,23 +48,23 @@ fi
|
|||||||
QUERY=`echo $@ | sed -e "s/ /.*/g"`
|
QUERY=`echo $@ | sed -e "s/ /.*/g"`
|
||||||
|
|
||||||
if [[ -n $FILE ]]; then
|
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
|
if [[ -z $files ]]; then
|
||||||
echo "no files matched"
|
echo "no files matched"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for file in `grep -l $GREP_FLAGS "$QUERY" $files`;
|
for file in `grep -l ${GREP_FLAGS} "${QUERY}" $files`;
|
||||||
do
|
do
|
||||||
echo $file:
|
echo $file:
|
||||||
highlight --force --line-numbers -O xterm256 --stdout $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
|
done
|
||||||
else
|
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
|
do
|
||||||
echo $file:
|
echo $file:
|
||||||
highlight --force --line-numbers -O xterm256 --stdout $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
|
done
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user