2 # https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh
4 export AUTOENV_ENV_FILENAME=$HOME/.env_auth
6 # Name of file to look for when entering directories.
7 : ${AUTOENV_FILE_ENTER:=.env}
9 # Name of file to look for when leaving directories.
10 # Requires AUTOENV_HANDLE_LEAVE=1.
11 : ${AUTOENV_FILE_LEAVE:=.env.leave}
13 # Look for .env in parent dirs?
14 : ${AUTOENV_LOOK_UPWARDS:=1}
16 # Handle leave events when changing away from a subtree, where an "enter"
18 : ${AUTOENV_HANDLE_LEAVE:=1}
21 # Public helper functions, which can be used from your .env files:
23 # Source the next .env file from parent directories.
24 # This is useful if you want to use a base .env file for a directory subtree.
25 autoenv_source_parent() {
26 local parent_env_file=$(_autoenv_get_file_upwards $PWD)
28 if [[ -n $parent_env_file ]] \
29 && _autoenv_check_authorized_env_file $parent_env_file; then
31 local parent_env_dir=${parent_env_file:A:h}
32 _autoenv_source $parent_env_file enter $parent_env_dir
37 # Internal: stack of entered (and handled) directories. {{{
38 _autoenv_stack_entered=()
39 typeset -A _autoenv_stack_entered_mtime
40 _autoenv_stack_entered_mtime=()
42 # Add an entry to the stack, and remember its mtime.
43 _autoenv_stack_entered_add() {
46 # Remove any existing entry.
47 _autoenv_stack_entered[$_autoenv_stack_entered[(i)$1]]=()
49 # Append it to the stack, and remember its mtime.
50 _autoenv_stack_entered+=($env_file)
51 _autoenv_stack_entered_mtime[$env_file]=$(_autoenv_get_file_mtime $env_file)
54 _autoenv_get_file_mtime() {
62 # Remove an entry from the stack.
63 _autoenv_stack_entered_remove() {
65 _autoenv_stack_entered=(${_autoenv_stack_entered#$env_file})
66 _autoenv_stack_entered_mtime[$env_file]=
69 # Is the given entry already in the stack?
70 _autoenv_stack_entered_contains() {
72 if (( ${+_autoenv_stack_entered[(r)${env_file}]} )); then
74 if [[ $_autoenv_stack_entered_mtime[$env_file] == $(_autoenv_get_file_mtime $env_file) ]]; then
75 # Entry has the expected mtime.
83 # Load zstat module, but only its builtin `zstat`.
84 zmodload -F zsh/stat b:zstat
87 _autoenv_hash_pair() {
92 env_shasum=$(shasum $env_file | cut -d' ' -f1)
94 echo "$env_file:$env_shasum:1"
97 _autoenv_authorized_env_file() {
99 local pair=$(_autoenv_hash_pair $env_file)
100 test -f $AUTOENV_ENV_FILENAME \
101 && \grep -qF $pair $AUTOENV_ENV_FILENAME
104 _autoenv_authorize() {
106 _autoenv_deauthorize $env_file
107 _autoenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
110 _autoenv_deauthorize() {
112 if [[ -f $AUTOENV_ENV_FILENAME ]]; then
113 echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
117 # This function can be mocked in tests
118 _autoenv_read_answer() {
120 read $=_AUTOENV_TEST_READ_ARGS -q answer
124 # Args: 1: absolute path to env file (resolved symlinks).
125 _autoenv_check_authorized_env_file() {
126 if ! [[ -f $1 ]]; then
129 if ! _autoenv_authorized_env_file $1; then
130 echo "Attempting to load unauthorized env file: $1"
132 echo "**********************************************"
136 echo "**********************************************"
138 echo -n "Would you like to authorize it? [y/N] "
140 local answer=$(_autoenv_read_answer)
142 if [[ $answer != 'y' ]]; then
146 _autoenv_authorize $1
151 # Get directory of this file (absolute, with resolved symlinks).
152 _autoenv_this_dir=${0:A:h}
157 local _autoenv_envfile_dir=$3
159 _autoenv_from_dir=$_autoenv_chpwd_prev_dir
162 # Source varstash library once.
163 if [[ -z "$functions[(I)autostash]" ]]; then
164 source $_autoenv_this_dir/lib/varstash
165 # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
169 # Change to directory of env file, source it and cd back.
171 builtin cd -q $_autoenv_envfile_dir
173 builtin cd -q $new_dir
175 # Unset vars set for enter/leave scripts.
176 # This should not get done for recursion (via autoenv_source_parent),
177 # and can be useful to have in general after autoenv was used.
178 # unset _autoenv_event _autoenv_from_dir _autoenv_to_dir
181 _autoenv_get_file_upwards() {
182 local look_from=${1:-$PWD}
183 local look_for=${2:-$AUTOENV_FILE_ENTER}
185 # Manually look in parent dirs. An extended Zsh glob should use Y1 for
186 # performance reasons, which is only available in zsh-5.0.5-146-g9381bb6.
188 local parent_dir="$look_from/.."
190 parent_dir=${parent_dir:A}
191 if [[ $parent_dir == $last ]]; then
194 parent_file="${parent_dir}/${look_for}"
196 if [[ -f $parent_file ]]; then
202 parent_dir="${parent_dir}/.."
207 _autoenv_chpwd_prev_dir=$PWD
208 _autoenv_chpwd_handler() {
209 local env_file="$PWD/$AUTOENV_FILE_ENTER"
211 # Handle leave event for previously sourced env files.
212 if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then
213 local prev_file prev_dir
214 for prev_file in ${_autoenv_stack_entered}; do
215 prev_dir=${prev_file:A:h}
216 if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
217 local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE
218 if _autoenv_check_authorized_env_file $env_file_leave; then
219 _autoenv_source $env_file_leave leave $prev_dir
221 _autoenv_stack_entered_remove $prev_dir
226 if ! [[ -f $env_file ]] && [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
227 env_file=$(_autoenv_get_file_upwards $PWD)
228 if [[ -z $env_file ]]; then
229 _autoenv_chpwd_prev_dir=$PWD
234 # Load the env file only once: check if $env_file is in the stack of entered
236 if _autoenv_stack_entered_contains $env_file; then
237 _autoenv_chpwd_prev_dir=$PWD
241 if ! _autoenv_check_authorized_env_file $env_file; then
242 _autoenv_chpwd_prev_dir=$PWD
246 _autoenv_stack_entered_add $env_file
248 # Source the enter env file.
249 _autoenv_source $env_file enter $PWD
251 _autoenv_chpwd_prev_dir=$PWD
254 autoload -U add-zsh-hook
255 add-zsh-hook chpwd _autoenv_chpwd_handler
257 # Look in current directory already.
258 _autoenv_chpwd_handler