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}
33 _autoenv_stack_entered_add $parent_env_file
35 _autoenv_source $parent_env_file enter $parent_env_dir
40 # Internal: stack of entered (and handled) directories. {{{
41 _autoenv_stack_entered=()
42 typeset -A _autoenv_stack_entered_mtime
43 _autoenv_stack_entered_mtime=()
45 # Add an entry to the stack, and remember its mtime.
46 _autoenv_stack_entered_add() {
49 # Remove any existing entry.
50 _autoenv_stack_entered_remove $env_file
52 # Append it to the stack, and remember its mtime.
53 _autoenv_stack_entered+=($env_file)
54 _autoenv_stack_entered_mtime[$env_file]=$(_autoenv_get_file_mtime $env_file)
57 _autoenv_get_file_mtime() {
65 # Remove an entry from the stack.
66 _autoenv_stack_entered_remove() {
68 _autoenv_stack_entered[$_autoenv_stack_entered[(i)$env_file]]=()
69 _autoenv_stack_entered_mtime[$env_file]=
72 # Is the given entry already in the stack?
73 _autoenv_stack_entered_contains() {
75 if (( ${+_autoenv_stack_entered[(r)${env_file}]} )); then
77 if [[ $_autoenv_stack_entered_mtime[$env_file] == $(_autoenv_get_file_mtime $env_file) ]]; then
78 # Entry has the expected mtime.
86 # Load zstat module, but only its builtin `zstat`.
87 zmodload -F zsh/stat b:zstat
90 _autoenv_hash_pair() {
95 env_shasum=$(shasum $env_file | cut -d' ' -f1)
97 echo "$env_file:$env_shasum:1"
100 _autoenv_authorized_env_file() {
102 local pair=$(_autoenv_hash_pair $env_file)
103 test -f $AUTOENV_ENV_FILENAME \
104 && \grep -qF $pair $AUTOENV_ENV_FILENAME
107 _autoenv_authorize() {
109 _autoenv_deauthorize $env_file
110 _autoenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
113 _autoenv_deauthorize() {
115 if [[ -f $AUTOENV_ENV_FILENAME ]]; then
116 echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
120 # This function can be mocked in tests
121 _autoenv_read_answer() {
123 read $=_AUTOENV_TEST_READ_ARGS -q answer
127 # Args: 1: absolute path to env file (resolved symlinks).
128 _autoenv_check_authorized_env_file() {
129 if ! [[ -f $1 ]]; then
132 if ! _autoenv_authorized_env_file $1; then
133 echo "Attempting to load unauthorized env file: $1"
135 echo "**********************************************"
139 echo "**********************************************"
141 echo -n "Would you like to authorize it? [y/N] "
143 local answer=$(_autoenv_read_answer)
145 if [[ $answer != 'y' ]]; then
149 _autoenv_authorize $1
154 # Get directory of this file (absolute, with resolved symlinks).
155 _autoenv_this_dir=${0:A:h}
160 local _autoenv_envfile_dir=$3
162 _autoenv_from_dir=$_autoenv_chpwd_prev_dir
165 # Source varstash library once.
166 if [[ -z "$functions[(I)autostash]" ]]; then
167 source $_autoenv_this_dir/lib/varstash
168 # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
172 # Change to directory of env file, source it and cd back.
174 builtin cd -q $_autoenv_envfile_dir
176 builtin cd -q $new_dir
178 # Unset vars set for enter/leave scripts.
179 # This should not get done for recursion (via autoenv_source_parent),
180 # and can be useful to have in general after autoenv was used.
181 # unset _autoenv_event _autoenv_from_dir _autoenv_to_dir
184 _autoenv_get_file_upwards() {
185 local look_from=${1:-$PWD}
186 local look_for=${2:-$AUTOENV_FILE_ENTER}
188 # Manually look in parent dirs. An extended Zsh glob should use Y1 for
189 # performance reasons, which is only available in zsh-5.0.5-146-g9381bb6.
191 local parent_dir="$look_from/.."
193 parent_dir=${parent_dir:A}
194 if [[ $parent_dir == $last ]]; then
197 parent_file="${parent_dir}/${look_for}"
199 if [[ -f $parent_file ]]; then
205 parent_dir="${parent_dir}/.."
210 _autoenv_chpwd_prev_dir=$PWD
211 _autoenv_chpwd_handler() {
212 local env_file="$PWD/$AUTOENV_FILE_ENTER"
214 # Handle leave event for previously sourced env files.
215 if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then
216 local prev_file prev_dir
217 for prev_file in ${_autoenv_stack_entered}; do
218 prev_dir=${prev_file:A:h}
219 if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
220 local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE
221 if _autoenv_check_authorized_env_file $env_file_leave; then
222 _autoenv_source $env_file_leave leave $prev_dir
224 _autoenv_stack_entered_remove $prev_file
229 if ! [[ -f $env_file ]] && [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
230 env_file=$(_autoenv_get_file_upwards $PWD)
231 if [[ -z $env_file ]]; then
232 _autoenv_chpwd_prev_dir=$PWD
237 # Load the env file only once: check if $env_file is in the stack of entered
239 if _autoenv_stack_entered_contains $env_file; then
240 _autoenv_chpwd_prev_dir=$PWD
244 if ! _autoenv_check_authorized_env_file $env_file; then
245 _autoenv_chpwd_prev_dir=$PWD
249 _autoenv_stack_entered_add $env_file
251 # Source the enter env file.
252 _autoenv_source $env_file enter $PWD
254 _autoenv_chpwd_prev_dir=$PWD
257 autoload -U add-zsh-hook
258 add-zsh-hook chpwd _autoenv_chpwd_handler
260 # Look in current directory already.
261 _autoenv_chpwd_handler