File indexing completed on 2024-04-21 05:42:00

0001 #!/bin/sh
0002 
0003 # Copyright 2004 Ben Reser <ben@reser.org>
0004 # Licensed under the terms subversion ships under or GPLv2.
0005 
0006 # Useful for greping in a subversion working copy.  
0007 # Essentially it behaves the same way your grep command does (in fact it
0008 # ultimately calls the grep command on your path) with a few exceptions.
0009 # Ignores the subversion admin directories (.svn) and vi(m) backup files.
0010 # Recursive is always on with or without -r.
0011 # Always print filename and line numbers.
0012 # Ignores binary files.
0013 # If no path is given the current working directory is searched not stdin.
0014 # Other than that it will take any parameter or pattern your standard grep
0015 # does.
0016 # 
0017 # This script requires GNU findutils and by default GNU grep (though that
0018 # can be changed with environment variables).
0019 # 
0020 # There are three environment variables you can set that modify the default
0021 # behavior:
0022 #
0023 # WCGREP_GREP      Controls what command is used for the grep command.
0024 #                  If unset or null wcgrep will check if ggrep (GNU grep)
0025 #                  is available and use it; if "ggrep" could not be found
0026 #                  in the path, "grep" will be used.
0027 # WCGREP_GREPARGS  Controls what arguments are always passed to the grep
0028 #                  command before the arguments given on the command line.
0029 #                  If unset or null it defaults to -HnI (always print file
0030 #                  names, line numbers and ignore binary files).  If you wish
0031 #                  to set no default args set the variable to a space (" ").
0032 # WCGREP_IGNORE    Controls what files are ignored by the grep command.
0033 #                  This is a regex that is passed to the find command with
0034 #                  -regex so see find's man page for details.  If unset or
0035 #                  null defaults to '.*~$\|.*/\.svn\(/\|$\)', which will
0036 #                  ignore vim backup files and subversion admin dirs.
0037 
0038 
0039 grepargs=
0040 pattern=
0041 
0042 # When loop completes, $@ should contain only list of paths to traverse.
0043 # If empty, set it to "." for compatibility (path list is not optional everywhere).
0044 # This way we can use "$@" feature when calling find(1).
0045 #
0046 # XXX The grepargs handling is broken, unlike the handling of path arguments.
0047 
0048 usage() {
0049     echo "usage: $(basename \"$0\") [grep-argument [...] [--]] pattern" >&2
0050     echo "       [path ...]" >&2
0051     exit 1
0052 }
0053 
0054 while [ $# -gt 0 ]; do
0055     if [ X"$1" = X-- ]; then
0056         grepargs="$grepargs $1"
0057         pattern="$2"
0058         shift 2
0059         break
0060     elif [ X"${1#-}" = X"$1" ]; then
0061         pattern="$1"
0062         shift
0063         break
0064     else
0065         grepargs="$grepargs $1"
0066     fi  
0067     shift
0068 done
0069 test -z "$pattern" && usage
0070 if [ $# -eq 0 ]; then
0071         set -- .
0072 fi
0073 
0074 WCGREP_IGNORE=${WCGREP_IGNORE:-'.*~$\|.*/\.git/.*\|.*/\.svn\(/\|$\)'}
0075 WCGREP_GREPARGS="${WCGREP_GREPARGS:--HnI}"
0076 
0077 # Some OSes have GNU tools with "g" prefix, try that first
0078 # TODO: check if:
0079 #  a) this tool is still needed;
0080 #  b) it is okay to break backward compatibility in future for
0081 #     portability reasons (avoid GNU toolchain dependency).
0082 
0083 WCGREP_GREP="${WCGREP_GREP:-$(command -v ggrep)}"
0084 WCGREP_GREP="${WCGREP_GREP:-grep}"
0085 
0086 WCGREP_FIND="$(command -v gfind)"
0087 WCGREP_FIND="${WCGREP_FIND:-find}"
0088 
0089 ${WCGREP_FIND} "$@" -regex "${WCGREP_IGNORE}" -prune -o -type f -print0 | \
0090     xargs -r0 ${WCGREP_GREP} ${WCGREP_GREPARGS} $grepargs "$pattern"