source: mainline/tools/srepl@ eec201d

Last change on this file since eec201d was 2021428, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Improve tools/srepl.

  • option -d for removing the line entirely.
  • automatically deal with slashes in the pattern.
  • Property mode set to 100755
File size: 1.2 KB
Line 
1#!/bin/sh
2
3usage()
4{
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
14}
15
16check_unstaged()
17{
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
23 fi
24}
25
26_slashify()
27{
28 # Some shells have shoddy handling of backslashes in echo.
29 printf '%s\n' "$1" | sed 's/\([^\\]\)\//\1\\\//g'
30}
31
32if [ "$1" = "--" ]; then
33 usage
34elif [ "$1" = "-d" ]; then
35 check_unstaged
36
37 pattern=`_slashify "$2"`
38 shift
39 shift
40 git grep -I -l "$pattern" "$@" | xargs sed -i "/$pattern/d"
41
42elif [ "$#" -eq 1 ] || [ "$2" = "--" ]; then
43 pattern=`_slashify "$1"`
44 shift
45 git grep -I -n "$pattern" "$@"
46elif [ "$#" -eq 2 ] || [ "$3" = "--" ]; then
47 check_unstaged
48
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"
54else
55 usage
56fi
Note: See TracBrowser for help on using the repository browser.