2 # https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh
4 # File to store confirmed authentication into.
5 # This handles the deprecated, old location(s).
6 if [[ -z $AUTOENV_AUTH_FILE ]]; then
7 if [[ -n $AUTOENV_ENV_FILENAME ]]; then
8 echo "zsh-autoenv: using deprecated setting for AUTOENV_AUTH_FILE from AUTOENV_ENV_FILENAME." >&2
9 echo "Please set AUTOENV_AUTH_FILE instead." >&2
10 AUTOENV_AUTH_FILE=$AUTOENV_ENV_FILENAME
12 if [[ -n $XDG_DATA_HOME ]]; then
13 AUTOENV_AUTH_FILE=$XDG_DATA_HOME/autoenv_auth
15 AUTOENV_AUTH_FILE=~/.local/share/autoenv_auth
17 if [[ -f ~/.env_auth ]]; then
18 echo "zsh-autoenv: using deprecated location for AUTOENV_AUTH_FILE." >&2
19 echo "Please move it: mv ~/.env_auth ${(D)AUTOENV_AUTH_FILE}." >&2
20 AUTOENV_AUTH_FILE=~/.env_auth
25 # Name of the file to look for when entering directories.
26 : ${AUTOENV_FILE_ENTER:=.autoenv.zsh}
28 # Name of the file to look for when leaving directories.
29 # Requires AUTOENV_HANDLE_LEAVE=1.
30 : ${AUTOENV_FILE_LEAVE:=.autoenv_leave.zsh}
32 # Look for zsh-autoenv "enter" files in parent dirs?
33 : ${AUTOENV_LOOK_UPWARDS:=1}
35 # Handle leave events when changing away from a subtree, where an "enter"
37 : ${AUTOENV_HANDLE_LEAVE:=1}
39 # Enable debugging. Multiple levels are supported (max 2).
42 # (Temporarily) disable zsh-autoenv. This gets looked at in the chpwd handler.
43 : ${AUTOENV_DISABLED:=0}
45 # Public helper functions, which can be used from your .autoenv.zsh files:
47 # Source the next .autoenv.zsh file from parent directories.
48 # This is useful if you want to use a base .autoenv.zsh file for a directory
50 autoenv_source_parent() {
51 local look_until=${1:-/}
52 local parent_env_file=$(_autoenv_get_file_upwards \
53 ${autoenv_env_file:h} ${AUTOENV_FILE_ENTER} $look_until)
55 if [[ -n $parent_env_file ]] \
56 && _autoenv_check_authorized_env_file $parent_env_file; then
57 _autoenv_debug "Calling autoenv_source_parent: parent_env_file:$parent_env_file"
58 _autoenv_source $parent_env_file enter
62 # Internal functions. {{{
63 # Internal: stack of entered (and handled) directories. {{{
64 typeset -g -a _autoenv_stack_entered
65 # -g: make it global, this is required when used with antigen.
66 typeset -g -A _autoenv_stack_entered_mtime
68 # Add an entry to the stack, and remember its mtime.
69 _autoenv_stack_entered_add() {
72 # Remove any existing entry.
73 _autoenv_stack_entered_remove $env_file
75 _autoenv_debug "[stack] adding: $env_file" 2
77 # Append it to the stack, and remember its mtime.
78 _autoenv_stack_entered+=($env_file)
79 _autoenv_stack_entered_mtime[$env_file]=$(_autoenv_get_file_mtime $env_file)
83 # zstat_mime helper, conditionally defined.
84 # Load zstat module, but only its builtin `zstat`.
85 if ! zmodload -F zsh/stat b:zstat 2>/dev/null; then
86 # If the module is not available, define a wrapper around `stat`, and use its
87 # terse output instead of format, which is not supported by busybox.
88 # Assume '+mtime' as $1.
89 _autoenv_get_file_mtime() {
90 # setopt localoptions pipefail
92 stat=$(stat -t $1 2>/dev/null)
93 if [[ -n $stat ]]; then
94 echo ${${(s: :)stat}[13]}
100 _autoenv_get_file_mtime() {
101 zstat +mtime $1 2>/dev/null || echo 0
106 # Remove an entry from the stack.
107 _autoenv_stack_entered_remove() {
109 _autoenv_debug "[stack] removing: $env_file" 2
110 _autoenv_stack_entered[$_autoenv_stack_entered[(i)$env_file]]=()
111 _autoenv_stack_entered_mtime[$env_file]=
114 # Is the given entry already in the stack?
115 # This checks for the env_file ($1) as-is and with symlinks resolved.
116 _autoenv_stack_entered_contains() {
119 if (( ${+_autoenv_stack_entered[(r)${env_file}]} )); then
123 for i in $_autoenv_stack_entered; do
124 if [[ ${i:A} == ${env_file:A} ]]; then
125 # Entry is in stack (compared with resolved symlinks).
132 if [[ $_autoenv_stack_entered_mtime[$f] == $(_autoenv_get_file_mtime $f) ]]; then
133 # Entry has the expected mtime.
141 # Internal function for debug output. {{{
143 local msg="$1" # Might trigger a bug in Zsh 5.0.5 with shwordsplit.
145 if [[ $AUTOENV_DEBUG -lt $level ]]; then
148 # Load zsh color support.
149 if [[ -z $color ]]; then
153 # Build $indent prefix.
155 if [[ $_autoenv_debug_indent -gt 0 ]]; then
156 for i in {1..${_autoenv_debug_indent}}; do
161 # Split $msg by \n (not newline).
162 lines=(${(ps:\\n:)msg})
163 for line in $lines; do
164 echo -n "${fg_bold[blue]}[autoenv]${fg_no_bold[default]} " >&2
165 echo ${indent}${line} >&2
171 # Generate hash pair for a given file ($1).
172 # A fixed hash value can be given as 2nd arg, but is used with tests only.
173 # The format is ":$file:$hash:$version".
174 _autoenv_hash_pair() {
175 local env_file=${1:A}
176 local env_shasum=${2:-}
177 if [[ -z $env_shasum ]]; then
178 if ! [[ -e $env_file ]]; then
179 echo "Missing file argument for _autoenv_hash_pair!" >&2
182 env_shasum=$(shasum $env_file | cut -d' ' -f1)
184 echo ":${env_file}:${env_shasum}:1"
187 _autoenv_authorized_env_file() {
189 local pair=$(_autoenv_hash_pair $env_file)
190 test -f $AUTOENV_AUTH_FILE \
191 && \grep -qF $pair $AUTOENV_AUTH_FILE
194 _autoenv_authorize() {
195 local env_file=${1:A}
196 _autoenv_deauthorize $env_file
197 _autoenv_hash_pair $env_file >>| $AUTOENV_AUTH_FILE
200 # Deauthorize a given filename, by removing it from the auth file.
201 # This uses `test -s` to only handle non-empty files, and a subshell to
202 # allow for writing to the same file again.
203 _autoenv_deauthorize() {
204 local env_file=${1:A}
205 if [[ -s $AUTOENV_AUTH_FILE ]]; then
206 echo "$(\grep -vF :${env_file}: $AUTOENV_AUTH_FILE)" >| $AUTOENV_AUTH_FILE
210 # This function can be mocked in tests
211 _autoenv_ask_for_yes() {
214 if [[ $answer == "yes" ]]; then
221 # Args: 1: absolute path to env file (resolved symlinks).
222 _autoenv_check_authorized_env_file() {
223 if ! [[ -f $1 ]]; then
226 if ! _autoenv_authorized_env_file $1; then
227 echo "Attempting to load unauthorized env file!" >&2
230 echo "**********************************************" >&2
234 echo "**********************************************" >&2
236 echo -n "Would you like to authorize it? (type 'yes') " >&2
237 # echo "Would you like to authorize it?"
238 # echo "('yes' to allow, 'no' to not being asked again; otherwise ignore it for the shell) "
240 if ! _autoenv_ask_for_yes; then
244 _autoenv_authorize $1
250 # Public API for the .autoenv.zsh script.
251 local autoenv_env_file=$1
252 local autoenv_event=$2
253 local autoenv_from_dir=$OLDPWD
254 local autoenv_to_dir=$PWD
256 # Source varstash library once.
257 if [[ -z "$functions[(I)autostash]" ]]; then
258 source ${${funcsourcetrace[1]%:*}:h}/lib/varstash
259 # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
260 # ${autoenv_env_file:h}.
263 # Source the env file.
264 _autoenv_debug "== SOURCE: ${bold_color:-}$autoenv_env_file${reset_color:-}\n PWD: $PWD"
265 : $(( _autoenv_debug_indent++ ))
266 source $autoenv_env_file
267 : $(( _autoenv_debug_indent-- ))
268 _autoenv_debug "== END SOURCE =="
270 if [[ $autoenv_event == enter ]]; then
271 _autoenv_stack_entered_add $autoenv_env_file
275 _autoenv_get_file_upwards() {
276 local look_from=${1:-$PWD}
277 local look_for=${2:-$AUTOENV_FILE_ENTER}
278 local look_until=${${3:-/}:A}
280 # Manually look in parent dirs. An extended Zsh glob should use Y1 for
281 # performance reasons, which is only available in zsh-5.0.5-146-g9381bb6.
283 local parent_dir="$look_from/.."
285 parent_dir=${parent_dir:A}
286 if [[ $parent_dir == $last ]]; then
289 parent_file="${parent_dir}/${look_for}"
291 if [[ -f $parent_file ]]; then
296 if [[ $parent_dir == $look_until ]]; then
300 parent_dir="${parent_dir}/.."
305 _autoenv_chpwd_handler() {
306 _autoenv_debug "Calling chpwd handler: PWD=$PWD"
308 if (( $AUTOENV_DISABLED )); then
309 _autoenv_debug "Disabled (AUTOENV_DISABLED)."
313 local env_file="$PWD/$AUTOENV_FILE_ENTER"
314 _autoenv_debug "env_file: $env_file"
316 # Handle leave event for previously sourced env files.
317 if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then
318 local prev_file prev_dir
319 for prev_file in ${_autoenv_stack_entered}; do
320 prev_dir=${prev_file:h}
321 if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
322 local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE
323 if _autoenv_check_authorized_env_file $env_file_leave; then
324 _autoenv_source $env_file_leave leave $prev_dir
327 # Unstash any autostashed stuff.
328 varstash_dir=$prev_dir autounstash
330 _autoenv_stack_entered_remove $prev_file
335 if ! [[ -f $env_file ]] && [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
336 env_file=$(_autoenv_get_file_upwards $PWD)
337 if [[ -z $env_file ]]; then
342 # Load the env file only once: check if $env_file is in the stack of entered
344 if _autoenv_stack_entered_contains $env_file; then
345 _autoenv_debug "Already in stack: $env_file"
349 if ! _autoenv_check_authorized_env_file $env_file; then
353 # Source the enter env file.
354 _autoenv_debug "Sourcing from chpwd handler: $env_file"
355 _autoenv_source $env_file enter
357 : $(( _autoenv_debug_indent++ ))
361 autoload -U add-zsh-hook
362 add-zsh-hook chpwd _autoenv_chpwd_handler
364 # Look in current directory already.
365 _autoenv_chpwd_handler