Merge changes to benchmarking tool (PR #151)
[helenos.git] / tools / srepl
blob60b20e12e7a5e42e9448ba7ee68fb8924c336d02
1 #!/bin/sh
3 usage()
5 echo "Usage:"
6 echo
7 echo " $0 search_pattern [-- <pathspec> ]"
8 echo " $0 search_pattern replacement [-- <pathspec> ]"
9 echo
10 echo "Pattern should be a basic regexp as accepted by grep and sed."
11 echo "For information on pathspec syntax, see git documentation."
12 echo
13 exit 1
16 check_unstaged()
18 if ( git status --porcelain | grep '^.[^ ]' ); then
19 echo "You have unstaged changes in your tree."
20 echo "Either stage them with 'git add', commit them,"
21 echo "or discard them with 'git checkout -- .'"
22 exit 1
26 _slashify()
28 # Some shells have shoddy handling of backslashes in echo.
29 printf '%s\n' "$1" | sed 's/\([^\\]\)\//\1\\\//g'
32 if [ "$1" = "--" ]; then
33 usage
34 elif [ "$1" = "-d" ]; then
35 check_unstaged
37 pattern=`_slashify "$2"`
38 shift
39 shift
40 git grep -I -l "$pattern" "$@" | xargs sed -i "/$pattern/d"
42 elif [ "$#" -eq 1 ] || [ "$2" = "--" ]; then
43 pattern=`_slashify "$1"`
44 shift
45 git grep -I -n "$pattern" "$@"
46 elif [ "$#" -eq 2 ] || [ "$3" = "--" ]; then
47 check_unstaged
49 pattern=`_slashify "$1"`
50 replacement=`_slashify "$2"`
51 shift
52 shift
53 git grep -I -l "$pattern" "$@" | xargs sed -i "s/$pattern/$replacement/g"
54 else
55 usage