d3dfc1371693d2b64ccec249511ea9178b088015

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

lib: use grep-alias cache only if ZSH_CACHE_DIR is writable

Fixes #8693

Diff

 1diff --git a/lib/grep.zsh b/lib/grep.zsh
 2index 1d94eb97895bbf94f567c2f164d16caac921fe34..a725e0f2620e750dd4bb03a6a4cf179df56164d8 100644
 3--- a/lib/grep.zsh
 4+++ b/lib/grep.zsh
 5@@ -2,7 +2,9 @@ __GREP_CACHE_FILE="$ZSH_CACHE_DIR"/grep-alias
 6 
 7 # See if there's a cache file modified in the last day
 8 __GREP_ALIAS_CACHES=("$__GREP_CACHE_FILE"(Nm-1))
 9-if [[ -z "$__GREP_ALIAS_CACHES" ]]; then
10+if [[ -n "$__GREP_ALIAS_CACHES" ]]; then
11+    source "$__GREP_CACHE_FILE"
12+else
13     grep-flags-available() {
14         command grep "$@" "" &>/dev/null <<< ""
15     }
16@@ -12,26 +14,28 @@ if [[ -z "$__GREP_ALIAS_CACHES" ]]; then
17 
18     # Check for --exclude-dir, otherwise check for --exclude. If --exclude
19     # isn't available, --color won't be either (they were released at the same
20-    # time (v2.5): http://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007
21+    # time (v2.5): https://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007
22     if grep-flags-available --color=auto --exclude-dir=.cvs; then
23         GREP_OPTIONS="--color=auto --exclude-dir=$EXC_FOLDERS"
24     elif grep-flags-available --color=auto --exclude=.cvs; then
25         GREP_OPTIONS="--color=auto --exclude=$EXC_FOLDERS"
26     fi
27 
28-    {
29-        if [[ -n "$GREP_OPTIONS" ]]; then
30-            # export grep, egrep and fgrep settings
31-            echo "alias grep='grep $GREP_OPTIONS'"
32-            echo "alias egrep='egrep $GREP_OPTIONS'"
33-            echo "alias fgrep='fgrep $GREP_OPTIONS'"
34+    if [[ -n "$GREP_OPTIONS" ]]; then
35+        # export grep, egrep and fgrep settings
36+        alias grep="grep $GREP_OPTIONS"
37+        alias egrep="egrep $GREP_OPTIONS"
38+        alias fgrep="fgrep $GREP_OPTIONS"
39+
40+        # write to cache file if cache directory is writable
41+        if [[ -w "$ZSH_CACHE_DIR" ]]; then
42+            alias -L grep egrep fgrep >| "$__GREP_CACHE_FILE"
43         fi
44-    } > "$__GREP_CACHE_FILE"
45+    fi
46 
47     # Clean up
48     unset GREP_OPTIONS EXC_FOLDERS
49     unfunction grep-flags-available
50 fi
51 
52-source "$__GREP_CACHE_FILE"
53 unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES