@matheusbsbs / matheus-bagliano / commits / e1622bfcba
Fork 0

Protect scripted Porcelains from GREP_OPTIONS insanity

If the user has exported the GREP_OPTIONS environment variable, the output from "grep" and "egrep" in scripted Porcelains may be different from what they expect. For example, we may want to count number of matching lines, by "grep" piped to "wc -l", and GREP_OPTIONS=-C3 will break such use. The approach taken by this change to address this issue is to protect only our own use of grep/egrep. Because we do not unset it at the beginning of our scripts, hook scripts run from the scripted Porcelains are exposed to the same insanity this environment variable causes when grep/egrep is used to implement logic (e.g. "grep | wc -l"), and it is entirely up to the hook scripts to protect themselves. On the other hand, applypatch-msg hook may want to show offending words in the proposed commit log message using grep to the end user, and the user might want to set GREP_OPTIONS=--color to paint the match more visibly. The approach to protect only our own use without unsetting the environment variable globally will allow this use case. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 23, 2009 at 15:56 UTC e1622bfcbad680225ad5c337e4778df88389227e
8 files changed +26 -18
git-am.sh
+2 -2
@@ -205,7 +205,7 @@ check_patch_format () {
205 # and see if it looks like that they all begin with the
206 # header field names...
207 sed -n -e '/^$/q' -e '/^[ ]/d' -e p "$1" |
208 - LC_ALL=C egrep -v '^[!-9;-~]+:' >/dev/null ||
208 + sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
209 patch_format=mbox
210 fi
211 } < "$1" || clean_abort
@@ -554,7 +554,7 @@ do
554 stop_here $this
555
556 # skip pine's internal folder data
557 - grep '^Author: Mail System Internal Data$' \
557 + sane_grep '^Author: Mail System Internal Data$' \
558 <"$dotest"/info >/dev/null &&
559 go_next && continue
560
git-bisect.sh
+2 -2
@@ -393,7 +393,7 @@ bisect_run () {
393
394 cat "$GIT_DIR/BISECT_RUN"
395
396 - if grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
396 + if sane_grep "first bad commit could be any of" "$GIT_DIR/BISECT_RUN" \
397 > /dev/null; then
398 echo >&2 "bisect run cannot continue any more"
399 exit $res
@@ -405,7 +405,7 @@ bisect_run () {
405 exit $res
406 fi
407
408 - if grep "is the first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
408 + if sane_grep "is the first bad commit" "$GIT_DIR/BISECT_RUN" > /dev/null; then
409 echo "bisect run success"
410 exit 0;
411 fi
git-filter-branch.sh
+1 -1
@@ -457,7 +457,7 @@ if [ "$filter_tag_name" ]; then
457 git mktag) ||
458 die "Could not create new tag object for $ref"
459 if git cat-file tag "$ref" | \
460 - grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
460 + sane_grep '^-----BEGIN PGP SIGNATURE-----' >/dev/null 2>&1
461 then
462 warn "gpg signature stripped from tag object $sha1t"
463 fi
git-instaweb.sh
+4 -4
@@ -41,7 +41,7 @@ resolve_full_httpd () {
41 case "$httpd" in
42 *apache2*|*lighttpd*)
43 # ensure that the apache2/lighttpd command ends with "-f"
44 - if ! echo "$httpd" | grep -- '-f *$' >/dev/null 2>&1
44 + if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
45 then
46 httpd="$httpd -f"
47 fi
@@ -297,8 +297,8 @@ EOF
297
298 # check to see if Dennis Stosberg's mod_perl compatibility patch
299 # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
300 - if test -f "$module_path/mod_perl.so" && grep 'MOD_PERL' \
301 - "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
300 + if test -f "$module_path/mod_perl.so" &&
301 + sane_grep 'MOD_PERL' "$GIT_DIR/gitweb/gitweb.cgi" >/dev/null
302 then
303 # favor mod_perl if available
304 cat >> "$conf" <<EOF
@@ -316,7 +316,7 @@ EOF
316 # plain-old CGI
317 resolve_full_httpd
318 list_mods=$(echo "$full_httpd" | sed "s/-f$/-l/")
319 - $list_mods | grep 'mod_cgi\.c' >/dev/null 2>&1 || \
319 + $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
320 echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
321 cat >> "$conf" <<EOF
322 AddHandler cgi-script .cgi
git-rebase--interactive.sh
+5 -5
@@ -106,8 +106,8 @@ mark_action_done () {
106 sed -e 1q < "$TODO" >> "$DONE"
107 sed -e 1d < "$TODO" >> "$TODO".new
108 mv -f "$TODO".new "$TODO"
109 - count=$(grep -c '^[^#]' < "$DONE")
110 - total=$(($count+$(grep -c '^[^#]' < "$TODO")))
109 + count=$(sane_grep -c '^[^#]' < "$DONE")
110 + total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
111 if test "$last_count" != "$count"
112 then
113 last_count=$count
@@ -147,7 +147,7 @@ die_abort () {
147 }
148
149 has_action () {
150 - grep '^[^#]' "$1" >/dev/null
150 + sane_grep '^[^#]' "$1" >/dev/null
151 }
152
153 pick_one () {
@@ -731,7 +731,7 @@ first and then run 'git rebase --continue' again."
731 git rev-list $REVISIONS |
732 while read rev
733 do
734 - if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
734 + if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
735 then
736 # Use -f2 because if rev-list is telling us this commit is
737 # not worthwhile, we don't want to track its multiple heads,
@@ -739,7 +739,7 @@ first and then run 'git rebase --continue' again."
739 # be rebasing on top of it
740 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
741 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
742 - grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
742 + sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
743 rm "$REWRITTEN"/$rev
744 fi
745 done
git-rebase.sh
+1 -1
@@ -467,7 +467,7 @@ orig_head=$branch
467 mb=$(git merge-base "$onto" "$branch")
468 if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
469 # linear history?
470 - ! (git rev-list --parents "$onto".."$branch" | grep " .* ") > /dev/null
470 + ! (git rev-list --parents "$onto".."$branch" | sane_grep " .* ") > /dev/null
471 then
472 if test -z "$force_rebase"
473 then
git-sh-setup.sh
+8
@@ -114,6 +114,14 @@ git_editor() {
114 eval "${GIT_EDITOR:=vi}" '"$@"'
115 }
116
117 +sane_grep () {
118 + GREP_OPTIONS= LC_ALL=C grep "$@"
119 +}
120 +
121 +sane_egrep () {
122 + GREP_OPTIONS= LC_ALL=C egrep "$@"
123 +}
124 +
125 is_bare_repository () {
126 git rev-parse --is-bare-repository
127 }
git-submodule.sh
+3 -3
@@ -57,7 +57,7 @@ resolve_relative_url ()
57 #
58 module_list()
59 {
60 - git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
60 + git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
61 }
62
63 #
@@ -567,7 +567,7 @@ cmd_summary() {
567 cd_to_toplevel
568 # Get modified modules cared by user
569 modules=$(git $diff_cmd $cached --raw $head -- "$@" |
570 - egrep '^:([0-7]* )?160000' |
570 + sane_egrep '^:([0-7]* )?160000' |
571 while read mod_src mod_dst sha1_src sha1_dst status name
572 do
573 # Always show modules deleted or type-changed (blob<->module)
@@ -581,7 +581,7 @@ cmd_summary() {
581 test -z "$modules" && return
582
583 git $diff_cmd $cached --raw $head -- $modules |
584 - egrep '^:([0-7]* )?160000' |
584 + sane_egrep '^:([0-7]* )?160000' |
585 cut -c2- |
586 while read mod_src mod_dst sha1_src sha1_dst status name
587 do