Skip to content

Commit e1622bf

Browse files
committed
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 <[email protected]>
1 parent 7b10422 commit e1622bf

File tree

8 files changed

+26
-18
lines changed

8 files changed

+26
-18
lines changed

git-am.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ check_patch_format () {
205205
# and see if it looks like that they all begin with the
206206
# header field names...
207207
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 ||
209209
patch_format=mbox
210210
fi
211211
} < "$1" || clean_abort
@@ -554,7 +554,7 @@ do
554554
stop_here $this
555555

556556
# skip pine's internal folder data
557-
grep '^Author: Mail System Internal Data$' \
557+
sane_grep '^Author: Mail System Internal Data$' \
558558
<"$dotest"/info >/dev/null &&
559559
go_next && continue
560560

git-bisect.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ bisect_run () {
393393

394394
cat "$GIT_DIR/BISECT_RUN"
395395

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" \
397397
> /dev/null; then
398398
echo >&2 "bisect run cannot continue any more"
399399
exit $res
@@ -405,7 +405,7 @@ bisect_run () {
405405
exit $res
406406
fi
407407

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
409409
echo "bisect run success"
410410
exit 0;
411411
fi

git-filter-branch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ if [ "$filter_tag_name" ]; then
457457
git mktag) ||
458458
die "Could not create new tag object for $ref"
459459
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
461461
then
462462
warn "gpg signature stripped from tag object $sha1t"
463463
fi

git-instaweb.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ resolve_full_httpd () {
4141
case "$httpd" in
4242
*apache2*|*lighttpd*)
4343
# 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
4545
then
4646
httpd="$httpd -f"
4747
fi
@@ -297,8 +297,8 @@ EOF
297297

298298
# check to see if Dennis Stosberg's mod_perl compatibility patch
299299
# (<[email protected]>) 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
302302
then
303303
# favor mod_perl if available
304304
cat >> "$conf" <<EOF
@@ -316,7 +316,7 @@ EOF
316316
# plain-old CGI
317317
resolve_full_httpd
318318
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 || \
320320
echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
321321
cat >> "$conf" <<EOF
322322
AddHandler cgi-script .cgi

git-rebase--interactive.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ mark_action_done () {
106106
sed -e 1q < "$TODO" >> "$DONE"
107107
sed -e 1d < "$TODO" >> "$TODO".new
108108
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")))
111111
if test "$last_count" != "$count"
112112
then
113113
last_count=$count
@@ -147,7 +147,7 @@ die_abort () {
147147
}
148148

149149
has_action () {
150-
grep '^[^#]' "$1" >/dev/null
150+
sane_grep '^[^#]' "$1" >/dev/null
151151
}
152152

153153
pick_one () {
@@ -731,15 +731,15 @@ first and then run 'git rebase --continue' again."
731731
git rev-list $REVISIONS |
732732
while read rev
733733
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)" = ""
735735
then
736736
# Use -f2 because if rev-list is telling us this commit is
737737
# not worthwhile, we don't want to track its multiple heads,
738738
# just the history of its first-parent for others that will
739739
# be rebasing on top of it
740740
git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
741741
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"
743743
rm "$REWRITTEN"/$rev
744744
fi
745745
done

git-rebase.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ orig_head=$branch
467467
mb=$(git merge-base "$onto" "$branch")
468468
if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
469469
# linear history?
470-
! (git rev-list --parents "$onto".."$branch" | grep " .* ") > /dev/null
470+
! (git rev-list --parents "$onto".."$branch" | sane_grep " .* ") > /dev/null
471471
then
472472
if test -z "$force_rebase"
473473
then

git-sh-setup.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ git_editor() {
114114
eval "${GIT_EDITOR:=vi}" '"$@"'
115115
}
116116
117+
sane_grep () {
118+
GREP_OPTIONS= LC_ALL=C grep "$@"
119+
}
120+
121+
sane_egrep () {
122+
GREP_OPTIONS= LC_ALL=C egrep "$@"
123+
}
124+
117125
is_bare_repository () {
118126
git rev-parse --is-bare-repository
119127
}

git-submodule.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ resolve_relative_url ()
5757
#
5858
module_list()
5959
{
60-
git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
60+
git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
6161
}
6262

6363
#
@@ -567,7 +567,7 @@ cmd_summary() {
567567
cd_to_toplevel
568568
# Get modified modules cared by user
569569
modules=$(git $diff_cmd $cached --raw $head -- "$@" |
570-
egrep '^:([0-7]* )?160000' |
570+
sane_egrep '^:([0-7]* )?160000' |
571571
while read mod_src mod_dst sha1_src sha1_dst status name
572572
do
573573
# Always show modules deleted or type-changed (blob<->module)
@@ -581,7 +581,7 @@ cmd_summary() {
581581
test -z "$modules" && return
582582

583583
git $diff_cmd $cached --raw $head -- $modules |
584-
egrep '^:([0-7]* )?160000' |
584+
sane_egrep '^:([0-7]* )?160000' |
585585
cut -c2- |
586586
while read mod_src mod_dst sha1_src sha1_dst status name
587587
do

0 commit comments

Comments
 (0)