# Port of Entry. Source this file from ~/.zshrc. # # Each time the shell enters a git repository from outside it, one line is # appended to $PORT_LOG: the local time with its offset, a tab, the root. # The hook prints nothing, starts no process and always returns 0. () { emulate -L zsh zmodload zsh/datetime autoload -Uz add-zsh-hook typeset -g PORT_HOME=${${(%):-%x}:A:h} typeset -gx PORT_LOG=${PORT_LOG:-${XDG_DATA_HOME:-$HOME/.local/share}/port/log.tsv} typeset -g PORT_ROOT=${PORT_ROOT:-} mkdir -p ${PORT_LOG:a:h} 2>/dev/null } _port_chpwd() { emulate -L zsh local ts dir=$PWD root= [[ $dir == *[$'\t\n']* ]] && return 0 while :; do if [[ -e $dir/.git ]]; then root=$dir break fi [[ $dir == / ]] && break dir=${dir:h} done # A dotfiles repository at $HOME would swallow every directory: outside. if [[ -z $root || $root == "${HOME%/}" ]]; then PORT_ROOT= return 0 fi [[ $root == "$PORT_ROOT" ]] && return 0 PORT_ROOT=$root strftime -s ts '%Y-%m-%dT%H:%M:%S%z' $EPOCHSECONDS { print -r "$ts"$'\t'"$root" >> "$PORT_LOG" } 2>/dev/null return 0 } port() { uv run --no-project --quiet "$PORT_HOME/port.py" "$@" } add-zsh-hook chpwd _port_chpwd _port_chpwd