From b6f2cfdb93627ca82ced24aaddb122b3a76a2638 Mon Sep 17 00:00:00 2001 From: Vital Kolas Date: Thu, 12 May 2016 10:40:24 +0300 Subject: [PATCH 1/6] Exclude .idea folder from grep search scope --- lib/grep.zsh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index abc1650a1..886e36a8d 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -10,13 +10,13 @@ if grep-flag-available --color=auto; then GREP_OPTIONS+=" --color=auto" fi -# ignore VCS folders (if the necessary grep flags are available) -VCS_FOLDERS="{.bzr,CVS,.git,.hg,.svn}" +# ignore these folders (if the necessary grep flags are available) +EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea}" if grep-flag-available --exclude-dir=.cvs; then - GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS" + GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS" elif grep-flag-available --exclude=.cvs; then - GREP_OPTIONS+=" --exclude=$VCS_FOLDERS" + GREP_OPTIONS+=" --exclude=$EXC_FOLDERS" fi # export grep settings @@ -24,5 +24,5 @@ alias grep="grep $GREP_OPTIONS" # clean up unset GREP_OPTIONS -unset VCS_FOLDERS +unset EXC_FOLDERS unfunction grep-flag-available From b4b50f20ac7a6645d04993ba8ca8695a41b8db7a Mon Sep 17 00:00:00 2001 From: Peter Vandenberk Date: Wed, 10 Oct 2018 17:21:06 +0100 Subject: [PATCH 2/6] Also set options for egrep and fgrep --- lib/grep.zsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index 886e36a8d..bd9dad41f 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -19,8 +19,10 @@ elif grep-flag-available --exclude=.cvs; then GREP_OPTIONS+=" --exclude=$EXC_FOLDERS" fi -# export grep settings +# export grep, egrep and fgrep settings alias grep="grep $GREP_OPTIONS" +alias egrep="egrep $GREP_OPTIONS" +alias fgrep="fgrep $GREP_OPTIONS" # clean up unset GREP_OPTIONS From a8ed1c4e7ab7e7df3a1e903cde30f94bf6da3e39 Mon Sep 17 00:00:00 2001 From: Shi Yan Date: Fri, 30 Nov 2018 15:44:41 +1100 Subject: [PATCH 3/6] Ignore .tox folder in grep --- lib/grep.zsh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index bd9dad41f..09042e13b 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -11,7 +11,7 @@ if grep-flag-available --color=auto; then fi # ignore these folders (if the necessary grep flags are available) -EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea}" +EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}" if grep-flag-available --exclude-dir=.cvs; then GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS" @@ -25,6 +25,5 @@ alias egrep="egrep $GREP_OPTIONS" alias fgrep="fgrep $GREP_OPTIONS" # clean up -unset GREP_OPTIONS -unset EXC_FOLDERS +unset GREP_OPTIONS EXC_FOLDERS unfunction grep-flag-available From 57b178102c0016f5c433054ce981a80dd4b4b73f Mon Sep 17 00:00:00 2001 From: mattmc3 Date: Sat, 30 Nov 2019 13:49:23 -0500 Subject: [PATCH 4/6] Performance enhancement for lib/grep - Use $ZSH_CACHE_DIR to store the grep alias with all the right features - Expire the cache after 24 hours - See issue #8444 --- lib/grep.zsh | 63 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index 09042e13b..df9146aa4 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -1,29 +1,40 @@ -# is x grep argument available? -grep-flag-available() { - echo | grep $1 "" >/dev/null 2>&1 -} +# see if we already cached the grep alias in past day +_grep_alias_cache=("$ZSH_CACHE_DIR"/grep_alias.zsh(Nm-24)) +if (( $#_grep_alias_cache )); then + source "$ZSH_CACHE_DIR"/grep_alias.zsh +else + # is x grep argument available? + grep-flag-available() { + echo | grep $1 "" >/dev/null 2>&1 + } -GREP_OPTIONS="" + GREP_OPTIONS="" -# color grep results -if grep-flag-available --color=auto; then - GREP_OPTIONS+=" --color=auto" + # color grep results + if grep-flag-available --color=auto; then + GREP_OPTIONS+=" --color=auto" + fi + + # ignore these folders (if the necessary grep flags are available) + EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}" + + if grep-flag-available --exclude-dir=.cvs; then + GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS" + elif grep-flag-available --exclude=.cvs; then + GREP_OPTIONS+=" --exclude=$EXC_FOLDERS" + fi + + { + # export grep, egrep and fgrep settings + echo alias grep="'grep $GREP_OPTIONS'" + echo alias egrep="'egrep $GREP_OPTIONS'" + echo alias fgrep="'fgrep $GREP_OPTIONS'" + } > "$ZSH_CACHE_DIR/grep_alias.zsh" + + source "$ZSH_CACHE_DIR/grep_alias.zsh" + + # clean up + unset GREP_OPTIONS EXC_FOLDERS + unfunction grep-flag-available fi - -# ignore these folders (if the necessary grep flags are available) -EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}" - -if grep-flag-available --exclude-dir=.cvs; then - GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS" -elif grep-flag-available --exclude=.cvs; then - GREP_OPTIONS+=" --exclude=$EXC_FOLDERS" -fi - -# export grep, egrep and fgrep settings -alias grep="grep $GREP_OPTIONS" -alias egrep="egrep $GREP_OPTIONS" -alias fgrep="fgrep $GREP_OPTIONS" - -# clean up -unset GREP_OPTIONS EXC_FOLDERS -unfunction grep-flag-available +unset _grep_alias_cache From 8d814fdff6f57e83a2b0c9430f61f7093a95bd04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 1 Mar 2020 14:02:06 +0100 Subject: [PATCH 5/6] Fast algorithm to determine grep alias flags This version tries whether grep supports all the flags together and progressively checks older flags if the grep test fails. This means only one grep call if all flags are supported, and one additional call for every flag that's not supported, up to a maximum of 3 calls. --- lib/grep.zsh | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index df9146aa4..933de2990 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -4,37 +4,36 @@ if (( $#_grep_alias_cache )); then source "$ZSH_CACHE_DIR"/grep_alias.zsh else # is x grep argument available? - grep-flag-available() { - echo | grep $1 "" >/dev/null 2>&1 + grep-flags-available() { + echo | grep "$@" "" >/dev/null 2>&1 } GREP_OPTIONS="" - # color grep results - if grep-flag-available --color=auto; then - GREP_OPTIONS+=" --color=auto" - fi - # ignore these folders (if the necessary grep flags are available) EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}" - if grep-flag-available --exclude-dir=.cvs; then - GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS" - elif grep-flag-available --exclude=.cvs; then - GREP_OPTIONS+=" --exclude=$EXC_FOLDERS" + if grep-flags-available --color=auto --exclude-dir=.cvs; then + GREP_OPTIONS+="--color=auto --exclude-dir=$EXC_FOLDERS" + elif grep-flags-available --color=auto --exclude=.cvs; then + GREP_OPTIONS+="--color=auto --exclude=$EXC_FOLDERS" + elif grep-flags-available --color=auto; then + GREP_OPTIONS+="--color=auto" fi { - # export grep, egrep and fgrep settings - echo alias grep="'grep $GREP_OPTIONS'" - echo alias egrep="'egrep $GREP_OPTIONS'" - echo alias fgrep="'fgrep $GREP_OPTIONS'" + if [[ -n "$GREP_OPTIONS" ]]; then + # export grep, egrep and fgrep settings + echo alias grep="'grep $GREP_OPTIONS'" + echo alias egrep="'egrep $GREP_OPTIONS'" + echo alias fgrep="'fgrep $GREP_OPTIONS'" + fi } > "$ZSH_CACHE_DIR/grep_alias.zsh" source "$ZSH_CACHE_DIR/grep_alias.zsh" # clean up unset GREP_OPTIONS EXC_FOLDERS - unfunction grep-flag-available + unfunction grep-flags-available fi unset _grep_alias_cache From dc190d872aa37dc0440284bee6c6bd7744007bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sun, 1 Mar 2020 20:17:38 +0100 Subject: [PATCH 6/6] Refactor grep.zsh file - Move grep-alias path to variable. - Use <<< "" instead of piped echo to check grep flags. - Remove check for --color only since it's the same release as --exclude. --- lib/grep.zsh | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/lib/grep.zsh b/lib/grep.zsh index 933de2990..1d94eb978 100644 --- a/lib/grep.zsh +++ b/lib/grep.zsh @@ -1,39 +1,37 @@ -# see if we already cached the grep alias in past day -_grep_alias_cache=("$ZSH_CACHE_DIR"/grep_alias.zsh(Nm-24)) -if (( $#_grep_alias_cache )); then - source "$ZSH_CACHE_DIR"/grep_alias.zsh -else - # is x grep argument available? +__GREP_CACHE_FILE="$ZSH_CACHE_DIR"/grep-alias + +# See if there's a cache file modified in the last day +__GREP_ALIAS_CACHES=("$__GREP_CACHE_FILE"(Nm-1)) +if [[ -z "$__GREP_ALIAS_CACHES" ]]; then grep-flags-available() { - echo | grep "$@" "" >/dev/null 2>&1 + command grep "$@" "" &>/dev/null <<< "" } - GREP_OPTIONS="" - - # ignore these folders (if the necessary grep flags are available) + # Ignore these folders (if the necessary grep flags are available) EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}" + # Check for --exclude-dir, otherwise check for --exclude. If --exclude + # isn't available, --color won't be either (they were released at the same + # time (v2.5): http://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007 if grep-flags-available --color=auto --exclude-dir=.cvs; then - GREP_OPTIONS+="--color=auto --exclude-dir=$EXC_FOLDERS" + GREP_OPTIONS="--color=auto --exclude-dir=$EXC_FOLDERS" elif grep-flags-available --color=auto --exclude=.cvs; then - GREP_OPTIONS+="--color=auto --exclude=$EXC_FOLDERS" - elif grep-flags-available --color=auto; then - GREP_OPTIONS+="--color=auto" + GREP_OPTIONS="--color=auto --exclude=$EXC_FOLDERS" fi { if [[ -n "$GREP_OPTIONS" ]]; then # export grep, egrep and fgrep settings - echo alias grep="'grep $GREP_OPTIONS'" - echo alias egrep="'egrep $GREP_OPTIONS'" - echo alias fgrep="'fgrep $GREP_OPTIONS'" + echo "alias grep='grep $GREP_OPTIONS'" + echo "alias egrep='egrep $GREP_OPTIONS'" + echo "alias fgrep='fgrep $GREP_OPTIONS'" fi - } > "$ZSH_CACHE_DIR/grep_alias.zsh" + } > "$__GREP_CACHE_FILE" - source "$ZSH_CACHE_DIR/grep_alias.zsh" - - # clean up + # Clean up unset GREP_OPTIONS EXC_FOLDERS unfunction grep-flags-available fi -unset _grep_alias_cache + +source "$__GREP_CACHE_FILE" +unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES