]> Sergey Matveev's repositories - zsh-autoenv.git/commitdiff
Only source varstash lib if it's being used
authorDaniel Hahler <git@thequod.de>
Tue, 26 Apr 2016 18:07:33 +0000 (20:07 +0200)
committerDaniel Hahler <git@thequod.de>
Thu, 28 Apr 2016 18:44:05 +0000 (20:44 +0200)
I have noticed that `autostash` called an (accidentally)
defined/overwritten `stash` function.

While it's not possible to put this into a local scope, this will at
least make it less polluting by default (when autostashing is not used).

Additionally we could source it always (not just once), but then it
would only re-overwrite the function.

A better fix would be to prefix the functions explicitly, e.g.
`autoenv_autostash` etc.

Closes https://github.com/Tarrasch/zsh-autoenv/pull/42.

autoenv.zsh
tests/varstash.t

index 12e058d463a34090cf94ac7c0392a6192a382bd0..ee0daa0a47af18d0a659971135d42c27e339f190 100644 (file)
@@ -260,8 +260,12 @@ _autoenv_source() {
   local autoenv_to_dir=$PWD
 
   # Source varstash library once.
+  # XXX: pollutes environment with e.g. `stash`, and `autostash` will cause
+  # an overwritten `stash` function to be called!
   if [[ -z "$functions[(I)autostash]" ]]; then
-    source ${${funcsourcetrace[1]%:*}:h}/lib/varstash
+    if \grep -qE '\b(autostash|autounstash|stash)\b' $autoenv_env_file; then
+      source ${${funcsourcetrace[1]%:*}:h}/lib/varstash
+    fi
     # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
     # ${autoenv_env_file:h}.
   fi
@@ -331,7 +335,9 @@ _autoenv_chpwd_handler() {
         fi
 
         # Unstash any autostashed stuff.
-        varstash_dir=$prev_dir autounstash
+        if [[ -n "$functions[(I)autostash]" ]]; then
+          varstash_dir=$prev_dir autounstash
+        fi
 
         _autoenv_stack_entered_remove $prev_file
       fi
index b75aa59690fc51c2d84166b2cbeb11c9f1124bc1..dd730c468ed3d348d7d6f72ddec1cdc09ab210ff 100644 (file)
@@ -6,11 +6,22 @@ Setup test environment.
 
   $ mkdir sub
   $ cd sub
-  $ echo 'echo ENTER; autostash FOO=changed' > $AUTOENV_FILE_ENTER
-  $ echo 'echo LEAVE; autounstash' > $AUTOENV_FILE_LEAVE
 
-Manually create auth file
+The varstash library should not get loaded always.
 
+  $ echo 'echo ENTER' > $AUTOENV_FILE_ENTER
+  $ echo 'echo LEAVE' > $AUTOENV_FILE_LEAVE
+  $ test_autoenv_auth_env_files
+  $ cd .
+  ENTER
+  $ type -w autostash
+  autostash: none
+  [1]
+
+Now on to some stashing.
+
+  $ echo 'echo ENTER; autostash FOO=changed' > $AUTOENV_FILE_ENTER
+  $ echo 'echo LEAVE; autounstash' > $AUTOENV_FILE_LEAVE
   $ test_autoenv_auth_env_files
 
 Set environment variable.
@@ -19,8 +30,12 @@ Set environment variable.
 
 Activating the env stashes it and applies a new value.
 
-  $ cd .
+  $ cd ..
+  LEAVE
+  $ cd sub
   ENTER
+  $ type -w autostash
+  autostash: function
   $ echo $FOO
   changed