chiark / gitweb /
pull-mail: add --force option; extend running times
[bin.git] / wcgrep
1 #!/bin/bash
2
3 # Copyright 2004 Ben Reser <ben@reser.org>
4 # Licensed under the terms subversion ships under or GPLv2.
5
6 # Useful for greping in a subversion working copy.  
7 # Essentially it behaves the same way your grep command does (in fact it
8 # ultimately calls the grep command on your path) with a few exceptions.
9 # Ignores the subversion admin directories (.svn) and vi(m) backup files.
10 # Recursive is always on with or without -r.
11 # Always print filename and line numbers.
12 # Ignores binary files.
13 # If no path is given the current working directory is searched not stdin.
14 # Other than that it will take any parameter or pattern your standard grep
15 # does.
16
17 # This script requires GNU findutils and by default GNU grep (though that
18 # can be changed with environment variables).
19
20 # There are three environment variables you can set that modify the default
21 # behavior:
22 #
23 # WCGREP_GREP      Controls what command is used for the grep command.
24 #                  If unset or null wcgrep will use the command named grep.
25 # WCGREP_GREPARGS  Controls what arguments are always passed to the grep
26 #                  command before the arguments given on the command line.
27 #                  If unset or null it defaults to -HnI (always print file
28 #                  names, line numbers and ignore binary files).  If you wish
29 #                  to set no default args set the variable to a space (" ").
30 # WCGREP_IGNORE    Controls what files are ignored by the grep command.
31 #                  This is a regex that is passed to the find command with
32 #                  -regex so see find's man page for details.  If unset or
33 #                  null defaults to '.*~$\|.*/\.svn\(/\|$\)', which will
34 #                  ignore vim backup files and subversion admin dirs.
35
36
37 arg_count=$#
38 for (( i=1; i <= $arg_count; i++ )); do
39     arg="$1"
40     shift 1
41     if [ -z "$pattern" ]; then
42         if [ "$arg" == "--" ]; then
43             grepargs="$grepargs $arg"
44             pattern="$1"
45             shift 1
46             ((i++))
47         elif [ "${arg:0:1}" != "-" ]; then
48             pattern="$arg"
49         else
50             grepargs="$grepargs $arg"
51         fi  
52     else
53         pathargs="$pathargs $arg"
54     fi
55 done
56
57 find $pathargs -regex ${WCGREP_IGNORE:-'.*~$\|.*/\(\.svn\|\{arch\}\|\.bzr\|\.git\)\(/\|$\)'} -prune -o \
58     -type f -print0 | xargs -r0 ${WCGREP_GREP:-grep} ${WCGREP_GREPARGS:--HnI} \
59     $grepargs "$pattern"