diff --git a/gitstatus/gitstatus.plugin.zsh b/gitstatus/gitstatus.plugin.zsh index fb32e106..f806abb9 100644 --- a/gitstatus/gitstatus.plugin.zsh +++ b/gitstatus/gitstatus.plugin.zsh @@ -51,8 +51,9 @@ [[ ! -o 'no_brace_expand' ]] || _gitstatus_opts+=('no_brace_expand') 'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand' -autoload -Uz add-zsh-hook -zmodload zsh/datetime zsh/system +autoload -Uz add-zsh-hook || return +zmodload zsh/datetime zsh/system || return +zmodload -F zsh/files b:zf_rm || return typeset -g _gitstatus_plugin_dir=${${(%):-%x}:A:h} @@ -138,147 +139,201 @@ typeset -g _gitstatus_plugin_dir=${${(%):-%x}:A:h} # It's illegal to call gitstatus_query if the last asynchronous call with the same NAME hasn't # completed yet. If you need to issue concurrent requests, use different NAME arguments. function gitstatus_query() { - emulate -L zsh - setopt err_return no_unset + emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent - local opt - local dir - local callback + unset VCS_STATUS_RESULT + + local opt dir callback OPTARG + local -i no_diff OPTIND local -F timeout=-1 - local no_diff=0 - while true; do - getopts "d:c:t:p" opt || break + while getopts ":d:c:t:p" opt; do case $opt in - d) dir=$OPTARG;; - c) callback=$OPTARG;; - t) timeout=$OPTARG;; - p) no_diff=1;; - ?) return 1;; - done) break;; + +p) no_diff=0;; + p) no_diff=1;; + d) dir=$OPTARG;; + c) callback=$OPTARG;; + t) + if [[ $OPTARG != (|+)<->(|.<->)(|[eE](|-|+)<->) ]]; then + print -ru2 -- "gitstatus_query: invalid -t argument: $OPTARG" + return 1 + fi + timeout=OPTARG + ;; + \?) print -ru2 -- "gitstatus_query: invalid option: $OPTARG" ; return 1;; + :) print -ru2 -- "gitstatus_query: missing required argument: $OPTARG"; return 1;; + *) print -ru2 -- "gitstatus_query: invalid option: $opt" ; return 1;; esac done - (( OPTIND == ARGC )) || { echo "usage: gitstatus_query [OPTION]... NAME" >&2; return 1 } - local name=${*[$OPTIND]} - local daemon_pid_var=GITSTATUS_DAEMON_PID_${name} - (( ${(P)daemon_pid_var:-0} > 0 )) + if (( OPTIND != ARGC )); then + print -ru2 -- "gitstatus_start: exactly one positional argument is required" + return 1 + fi + + local name=$*[OPTIND] + if [[ $name != [[:IDENT:]]## ]]; then + print -ru2 -- "gitstatus_start: invalid positional argument: $name" + return 1 + fi - # Verify that gitstatus_query is running in the same process that ran gitstatus_start. - local client_pid_var=_GITSTATUS_CLIENT_PID_${name} - [[ ${(P)client_pid_var} == $$ ]] + (( _GITSTATUS_STATE_$name == 2 )) || return - [[ -z ${GIT_DIR:-} ]] && { + if [[ -z $GIT_DIR ]]; then [[ $dir == /* ]] || dir=${(%):-%/}/$dir - } || { + else [[ $GIT_DIR == /* ]] && dir=:$GIT_DIR || dir=:${(%):-%/}/$GIT_DIR - } + fi + + local -i req_fd=${(P)${:-_GITSTATUS_REQ_FD_$name}} + local req_id=$EPOCHREALTIME + print -rnu $req_fd -- $req_id' '$callback$'\x1f'$dir$'\x1f'$no_diff$'\x1e' || return + + (( ++_GITSTATUS_NUM_INFLIGHT_$name )) + + if (( timeout == 0 )); then + typeset -g VCS_STATUS_RESULT=tout + _gitstatus_clear + else + while true; do + _gitstatus_process_response $name $timeout $req_id || return + [[ $VCS_STATUS_RESULT == *-async ]] || break + done + fi + + [[ $VCS_STATUS_RESULT != tout || -n $callback ]] +} + +function gitstatus_process_results() { + emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent + local opt OPTARG + local -i OPTIND + local -F timeout=-1 + while getopts ":t:" opt; do + case $opt in + t) + if [[ $OPTARG != (|+)<->(|.<->)(|[eE](|-|+)<->) ]]; then + print -ru2 -- "gitstatus_process_results: invalid -t argument: $OPTARG" + return 1 + fi + timeout=OPTARG + ;; + \?) print -ru2 -- "gitstatus_process_results: invalid option: $OPTARG" ; return 1;; + :) print -ru2 -- "gitstatus_process_results: missing required argument: $OPTARG"; return 1;; + *) print -ru2 -- "gitstatus_process_results: invalid option: $opt" ; return 1;; + esac + done + + if (( OPTIND != ARGC )); then + print -ru2 -- "gitstatus_process_results: exactly one positional argument is required" + return 1 + fi + + local name=$*[OPTIND] + if [[ $name != [[:IDENT:]]## ]]; then + print -ru2 -- "gitstatus_process_results: invalid positional argument: $name" + return 1 + fi - local req_fd_var=_GITSTATUS_REQ_FD_${name} - local -i req_fd=${(P)req_fd_var} - local -r req_id="$EPOCHREALTIME" - echo -nE $req_id' '$callback$'\x1f'$dir$'\x1f'$no_diff$'\x1e' >&$req_fd + (( _GITSTATUS_STATE_$name == 2 )) || return - while true; do - _gitstatus_process_response $name $timeout $req_id + while (( _GITSTATUS_NUM_INFLIGHT_$name )); do + _gitstatus_process_response $name $timeout '' || return [[ $VCS_STATUS_RESULT == *-async ]] || break done - [[ $VCS_STATUS_RESULT != tout || -n $callback ]] + return 0 } -function _gitstatus_process_response() { - emulate -L zsh - setopt err_return no_unset +function _gitstatus_clear() { + unset VCS_STATUS_{WORKDIR,COMMIT,LOCAL_BRANCH,REMOTE_BRANCH,REMOTE_NAME,REMOTE_URL,ACTION,INDEX_SIZE,NUM_STAGED,NUM_UNSTAGED,NUM_CONFLICTED,NUM_UNTRACKED,HAS_STAGED,HAS_UNSTAGED,HAS_CONFLICTED,HAS_UNTRACKED,COMMITS_AHEAD,COMMITS_BEHIND,STASHES,TAG,NUM_UNSTAGED_DELETED,NUM_STAGED_NEW,NUM_STAGED_DELETED,PUSH_REMOTE_NAME,PUSH_REMOTE_URL,PUSH_COMMITS_AHEAD,PUSH_COMMITS_BEHIND,NUM_SKIP_WORKTREE,NUM_ASSUME_UNCHANGED} +} - local name=$1 - local -F timeout=$2 - local req_id=$3 - local resp_fd_var=_GITSTATUS_RESP_FD_${name} - local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name} - - typeset -g VCS_STATUS_RESULT - (( timeout >= 0 )) && local -a t=(-t $timeout) || local -a t=() - local -a resp - local IFS=$'\x1f' - read -rd $'\x1e' -u ${(P)resp_fd_var} $t -A resp || { - VCS_STATUS_RESULT=tout - return +function _gitstatus_process_response() { + local name=$1 timeout req_id=$3 buf + local -i resp_fd=_GITSTATUS_RESP_FD_$name + local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_$name + + (( $2 >= 0 )) && timeout=-t$2 && [[ -t $resp_fd ]] + sysread $timeout -i $resp_fd 'buf[$#buf+1]' || { + if (( $? == 4 )); then + if [[ -n $req_id ]]; then + typeset -g VCS_STATUS_RESULT=tout + _gitstatus_clear + fi + return 0 + else + gitstatus_stop $name + return 1 + fi } + while [[ $buf != *$'\x1e' ]]; do + if ! sysread -i $resp_fd 'buf[$#buf+1]'; then + gitstatus_stop $name + return 1 + fi + done - local -a header=("${(@Q)${(z)resp[1]}}") - [[ ${header[1]} == $req_id ]] && local -i ours=1 || local -i ours=0 - shift header - [[ ${resp[2]} == 1 ]] && { - (( ours )) && VCS_STATUS_RESULT=ok-sync || VCS_STATUS_RESULT=ok-async - typeset -g VCS_STATUS_WORKDIR="${resp[3]}" - typeset -g VCS_STATUS_COMMIT="${resp[4]}" - typeset -g VCS_STATUS_LOCAL_BRANCH="${resp[5]}" - typeset -g VCS_STATUS_REMOTE_BRANCH="${resp[6]}" - typeset -g VCS_STATUS_REMOTE_NAME="${resp[7]}" - typeset -g VCS_STATUS_REMOTE_URL="${resp[8]}" - typeset -g VCS_STATUS_ACTION="${resp[9]}" - typeset -gi VCS_STATUS_INDEX_SIZE="${resp[10]}" - typeset -gi VCS_STATUS_NUM_STAGED="${resp[11]}" - typeset -gi VCS_STATUS_NUM_UNSTAGED="${resp[12]}" - typeset -gi VCS_STATUS_NUM_CONFLICTED="${resp[13]}" - typeset -gi VCS_STATUS_NUM_UNTRACKED="${resp[14]}" - typeset -gi VCS_STATUS_COMMITS_AHEAD="${resp[15]}" - typeset -gi VCS_STATUS_COMMITS_BEHIND="${resp[16]}" - typeset -gi VCS_STATUS_STASHES="${resp[17]}" - typeset -g VCS_STATUS_TAG="${resp[18]}" - typeset -gi VCS_STATUS_NUM_UNSTAGED_DELETED="${resp[19]}" - typeset -gi VCS_STATUS_NUM_STAGED_NEW="${resp[20]:-0}" - typeset -gi VCS_STATUS_NUM_STAGED_DELETED="${resp[21]:-0}" - typeset -g VCS_STATUS_PUSH_REMOTE_NAME="${resp[22]:-}" - typeset -g VCS_STATUS_PUSH_REMOTE_URL="${resp[23]:-}" - typeset -gi VCS_STATUS_PUSH_COMMITS_AHEAD="${resp[24]:-0}" - typeset -gi VCS_STATUS_PUSH_COMMITS_BEHIND="${resp[25]:-0}" - typeset -gi VCS_STATUS_NUM_SKIP_WORKTREE="${resp[26]:-0}" - typeset -gi VCS_STATUS_NUM_ASSUME_UNCHANGED="${resp[27]:-0}" - typeset -gi VCS_STATUS_HAS_STAGED=$((VCS_STATUS_NUM_STAGED > 0)) - (( dirty_max_index_size >= 0 && VCS_STATUS_INDEX_SIZE > dirty_max_index_size )) && { - typeset -gi VCS_STATUS_HAS_UNSTAGED=-1 - typeset -gi VCS_STATUS_HAS_CONFLICTED=-1 - typeset -gi VCS_STATUS_HAS_UNTRACKED=-1 - } || { - typeset -gi VCS_STATUS_HAS_UNSTAGED=$((VCS_STATUS_NUM_UNSTAGED > 0)) - typeset -gi VCS_STATUS_HAS_CONFLICTED=$((VCS_STATUS_NUM_CONFLICTED > 0)) - typeset -gi VCS_STATUS_HAS_UNTRACKED=$((VCS_STATUS_NUM_UNTRACKED > 0)) - } - } || { - (( ours )) && VCS_STATUS_RESULT=norepo-sync || VCS_STATUS_RESULT=norepo-async - unset VCS_STATUS_WORKDIR - unset VCS_STATUS_COMMIT - unset VCS_STATUS_LOCAL_BRANCH - unset VCS_STATUS_REMOTE_BRANCH - unset VCS_STATUS_REMOTE_NAME - unset VCS_STATUS_REMOTE_URL - unset VCS_STATUS_ACTION - unset VCS_STATUS_INDEX_SIZE - unset VCS_STATUS_NUM_STAGED - unset VCS_STATUS_NUM_UNSTAGED - unset VCS_STATUS_NUM_CONFLICTED - unset VCS_STATUS_NUM_UNTRACKED - unset VCS_STATUS_HAS_STAGED - unset VCS_STATUS_HAS_UNSTAGED - unset VCS_STATUS_HAS_CONFLICTED - unset VCS_STATUS_HAS_UNTRACKED - unset VCS_STATUS_COMMITS_AHEAD - unset VCS_STATUS_COMMITS_BEHIND - unset VCS_STATUS_STASHES - unset VCS_STATUS_TAG - unset VCS_STATUS_NUM_UNSTAGED_DELETED - unset VCS_STATUS_NUM_STAGED_NEW - unset VCS_STATUS_NUM_STAGED_DELETED - unset VCS_STATUS_PUSH_REMOTE_NAME - unset VCS_STATUS_PUSH_REMOTE_URL - unset VCS_STATUS_PUSH_COMMITS_AHEAD - unset VCS_STATUS_PUSH_COMMITS_BEHIND - unset VCS_STATUS_NUM_SKIP_WORKTREE - unset VCS_STATUS_NUM_ASSUME_UNCHANGED - } + local s + for s in ${(ps:\x1e:)buf}; do + local -a resp=("${(@ps:\x1f:)s}") + if (( resp[2] )); then + if [[ $resp[1] == $req_id' '* ]]; then + typeset -g VCS_STATUS_RESULT=ok-sync + else + typeset -g VCS_STATUS_RESULT=ok-async + fi + for VCS_STATUS_WORKDIR \ + VCS_STATUS_COMMIT \ + VCS_STATUS_LOCAL_BRANCH \ + VCS_STATUS_REMOTE_BRANCH \ + VCS_STATUS_REMOTE_NAME \ + VCS_STATUS_REMOTE_URL \ + VCS_STATUS_ACTION \ + VCS_STATUS_INDEX_SIZE \ + VCS_STATUS_NUM_STAGED \ + VCS_STATUS_NUM_UNSTAGED \ + VCS_STATUS_NUM_CONFLICTED \ + VCS_STATUS_NUM_UNTRACKED \ + VCS_STATUS_COMMITS_AHEAD \ + VCS_STATUS_COMMITS_BEHIND \ + VCS_STATUS_STASHES \ + VCS_STATUS_TAG \ + VCS_STATUS_NUM_UNSTAGED_DELETED \ + VCS_STATUS_NUM_STAGED_NEW \ + VCS_STATUS_NUM_STAGED_DELETED \ + VCS_STATUS_PUSH_REMOTE_NAME \ + VCS_STATUS_PUSH_REMOTE_URL \ + VCS_STATUS_PUSH_COMMITS_AHEAD \ + VCS_STATUS_PUSH_COMMITS_BEHIND \ + VCS_STATUS_NUM_SKIP_WORKTREE \ + VCS_STATUS_NUM_ASSUME_UNCHANGED in "${(@)resp[3,27]}"; do + done + typeset -gi VCS_STATUS_{INDEX_SIZE,NUM_STAGED,NUM_UNSTAGED,NUM_CONFLICTED,NUM_UNTRACKED,COMMITS_AHEAD,COMMITS_BEHIND,STASHES,NUM_UNSTAGED_DELETED,NUM_STAGED_NEW,NUM_STAGED_DELETED,PUSH_COMMITS_AHEAD,PUSH_COMMITS_BEHIND,NUM_SKIP_WORKTREE,NUM_ASSUME_UNCHANGED} + typeset -gi VCS_STATUS_HAS_STAGED=$((VCS_STATUS_NUM_STAGED > 0)) + if (( dirty_max_index_size >= 0 && VCS_STATUS_INDEX_SIZE > dirty_max_index_size )); then + typeset -gi \ + VCS_STATUS_HAS_UNSTAGED=-1 \ + VCS_STATUS_HAS_CONFLICTED=-1 \ + VCS_STATUS_HAS_UNTRACKED=-1 + else + typeset -gi \ + VCS_STATUS_HAS_UNSTAGED=$((VCS_STATUS_NUM_UNSTAGED > 0)) \ + VCS_STATUS_HAS_CONFLICTED=$((VCS_STATUS_NUM_CONFLICTED > 0)) \ + VCS_STATUS_HAS_UNTRACKED=$((VCS_STATUS_NUM_UNTRACKED > 0)) + fi + else + if [[ $resp[1] == $req_id' '* ]]; then + typeset -g VCS_STATUS_RESULT=norepo-sync + else + typeset -g VCS_STATUS_RESULT=norepo-async + fi + _gitstatus_clear + fi + (( --_GITSTATUS_NUM_INFLIGHT_$name )) + [[ $VCS_STATUS_RESULT == *-async ]] && emulate zsh -c "${resp[1]#* }" + done - (( ! ours )) && (( #header )) && emulate -L zsh && "${header[@]}" || true + return 0 } # Starts gitstatusd in the background. Does nothing and succeeds if gitstatusd is already running. @@ -314,267 +369,270 @@ function _gitstatus_process_response() { # -D Unless this option is specified, report zero staged, unstaged and conflicted # changes for repositories with bash.showDirtyState = false. function gitstatus_start() { - emulate -L zsh - setopt err_return no_unset no_bg_nice + emulate -L zsh || return + setopt no_aliases no_bg_nice extended_glob typeset_silent monitor || return + + print -rnu2 || return - local opt + local opt OPTARG + local -i OPTIND local -F timeout=5 - local -i max_num_staged=1 - local -i max_num_unstaged=1 - local -i max_num_conflicted=1 - local -i max_num_untracked=1 + local -i async=0 + local -a args=() local -i dirty_max_index_size=-1 - local -i async - local -a extra_flags=() - while true; do - getopts "t:s:u:c:d:m:eaUWD" opt || break + + while getopts ":t:s:u:c:d:m:eaUWD" opt; do case $opt in - a) async=1;; - t) timeout=$OPTARG;; - s) max_num_staged=$OPTARG;; - u) max_num_unstaged=$OPTARG;; - c) max_num_conflicted=$OPTARG;; - d) max_num_untracked=$OPTARG;; - m) dirty_max_index_size=$OPTARG;; - e) extra_flags+='--recurse-untracked-dirs';; - +e) extra_flags=(${(@)extra_flags:#--recurse-untracked-dirs});; - U) extra_flags+='--ignore-status-show-untracked-files';; - +U) extra_flags=(${(@)extra_flags:#--ignore-status-show-untracked-files});; - W) extra_flags+='--ignore-bash-show-untracked-files';; - +W) extra_flags=(${(@)extra_flags:#--ignore-bash-show-untracked-files});; - D) extra_flags+='--ignore-bash-show-dirty-state';; - +D) extra_flags=(${(@)extra_flags:#--ignore-bash-show-dirty-state});; - ?) return 1;; + a) async=1;; + +a) async=0;; + t) + if [[ $OPTARG != (|+)<->(|.<->)(|[eE](|-|+)<->) ]] || (( ${timeout::=OPTARG} <= 0 )); then + print -ru2 -- "gitstatus_start: invalid -t argument: $OPTARG" + return 1 + fi + ;; + s|u|c|d|m) + if [[ $OPTARG != (|-|+)<-> ]]; then + print -ru2 -- "gitstatus_start: invalid -$opt argument: $OPTARG" + return 1 + fi + args+=(-$opt $OPTARG) + [[ $opt == m ]] && dirty_max_index_size=OPTARG + ;; + e|U|W|D) args+=$opt;; + +(e|U|W|D)) args=(${(@)args:#-$opt});; + \?) print -ru2 -- "gitstatus_start: invalid option: $OPTARG" ; return 1;; + :) print -ru2 -- "gitstatus_start: missing required argument: $OPTARG"; return 1;; + *) print -ru2 -- "gitstatus_start: invalid option: $opt" ; return 1;; esac done - (( timeout > 0 )) || { echo "invalid -t: $timeout" >&2; return 1 } - (( OPTIND == ARGC )) || { echo "usage: gitstatus_start [OPTION]... NAME" >&2; return 1 } - local name=${*[$OPTIND]} - - local lock_file req_fifo resp_fifo log_level - local log_file=/dev/null xtrace_file=/dev/null - local -i stderr_fd lock_fd req_fd resp_fd daemon_pid - local daemon_pid_var=GITSTATUS_DAEMON_PID_${name} - (( $+parameters[$daemon_pid_var] )) && { - (( ! async )) || return 0 - daemon_pid=${(P)daemon_pid_var} - (( daemon_pid == -1 )) || return 0 - local resp_fd_var=_GITSTATUS_RESP_FD_${name} - local log_file_var=GITSTATUS_DAEMON_LOG_${name} - local xtrace_file_var=GITSTATUS_XTRACE_${name} - resp_fd=${(P)resp_fd_var} - log_file=${(P)log_file_var} - xtrace_file=${(P)xtrace_file_var} - } || { - log_level=${GITSTATUS_LOG_LEVEL:-} - [[ -n $log_level || ${GITSTATUS_ENABLE_LOGGING:-0} != 1 ]] || log_level=INFO - [[ -z $log_level ]] || { - log_file=${TMPDIR:-/tmp}/gitstatus.$$.daemon-log.$EPOCHREALTIME.$RANDOM - xtrace_file=${TMPDIR:-/tmp}/gitstatus.$$.xtrace.$EPOCHREALTIME.$RANDOM - } - typeset -g GITSTATUS_DAEMON_LOG_${name}=$log_file - typeset -g GITSTATUS_XTRACE_${name}=$xtrace_file - } + if (( OPTIND != ARGC )); then + print -ru2 -- "gitstatus_start: exactly one positional argument is required" + return 1 + fi - function gitstatus_start_impl() { - [[ $xtrace_file == /dev/null ]] || { - exec {stderr_fd}>&2 2>>$xtrace_file + local name=$*[OPTIND] + if [[ $name != [[:IDENT:]]## ]]; then + print -ru2 -- "gitstatus_start: invalid positional argument: $name" + return 1 + fi + + local -i lock_fd resp_fd + local file_prefix xtrace=/dev/null daemon_log=/dev/null + if (( _GITSTATUS_STATE_$name )); then + (( async )) && return + (( _GITSTATUS_STATE_$name == 2 )) && return + lock_fd=_GITSTATUS_LOCK_FD_$name + resp_fd=_GITSTATUS_RESP_FD_$name + xtrace=${(P)${:-GITSTATUS_XTRACE_$name}} + daemon_log=${(P)${:-GITSTATUS_DAEMON_LOG_$name}} + file_prefix=${(P)${:-_GITSTATUS_FILE_PREFIX_$name}} + else + local log_level=$GITSTATUS_LOG_LEVEL + local file_prefix=${TMPDIR:-/tmp}/gitstatus.$EUID.$sysparams[pid].$EPOCHSECONDS + (( GITSTATUS_ENABLE_LOGGING )) && : ${log_level:=INFO} + if [[ -n $log_level ]]; then + xtrace=$file_prefix.xtrace.log + daemon_log=$file_prefix.daemon.log + fi + args+=(-v ${log_level:-FATAL}) + typeset -g GITSTATUS_XTRACE_$name=$xtrace + typeset -g GITSTATUS_DAEMON_LOG_$name=$daemon_log + typeset -g _GITSTATUS_FILE_PREFIX_$name=$file_prefix + typeset -gi _GITSTATUS_CLIENT_PID_$name="sysparams[pid]" + typeset -gi _GITSTATUS_DIRTY_MAX_INDEX_SIZE_$name=dirty_max_index_size + fi + + local -i stderr_fd + { + if [[ $xtrace != /dev/null && -o no_xtrace ]]; then + exec {stderr_fd}>&2 + exec 2>>$xtrace || return setopt xtrace - } - - (( daemon_pid == -1 )) || { - local os - local daemon=${GITSTATUS_DAEMON:-} - [[ -n $daemon ]] || { - os="$(uname -s)" && [[ -n $os ]] - [[ $os != Linux || "$(uname -o)" != Android ]] || os=Android - [[ ${(L)os} != (mingw|msys)* ]] || os=MSYS_NT-10.0 - local arch && arch="$(uname -m)" && [[ -n $arch ]] - daemon=$_gitstatus_plugin_dir/bin/gitstatusd-${os:l}-${arch:l} - } - [[ -x $daemon ]] - - lock_file=${TMPDIR:-/tmp}/gitstatus.$$.lock.$EPOCHREALTIME.$RANDOM - echo -n >$lock_file - zsystem flock -f lock_fd $lock_file - - req_fifo=${TMPDIR:-/tmp}/gitstatus.$$.req.$EPOCHREALTIME.$RANDOM - resp_fifo=${TMPDIR:-/tmp}/gitstatus.$$.resp.$EPOCHREALTIME.$RANDOM - mkfifo $req_fifo $resp_fifo - - local -i threads=${GITSTATUS_NUM_THREADS:-0} - (( threads > 0)) || { - threads=8 - [[ -n $os ]] || { os="$(uname -s)" && [[ -n $os ]] } - case $os in - FreeBSD) (( ! $+commands[sysctl] )) || threads=$(( 2 * $(sysctl -n hw.ncpu) ));; - *) (( ! $+commands[getconf] )) || threads=$(( 2 * $(getconf _NPROCESSORS_ONLN) ));; - esac - (( threads <= 32 )) || threads=32 - } + fi - local -a daemon_args=( - --lock-fd=3 - --parent-pid=${(q)$} - --num-threads=${(q)threads} - --max-num-staged=${(q)max_num_staged} - --max-num-unstaged=${(q)max_num_unstaged} - --max-num-conflicted=${(q)max_num_conflicted} - --max-num-untracked=${(q)max_num_untracked} - --dirty-max-index-size=${(q)dirty_max_index_size} - --log-level=${(q)log_level:-INFO} - $extra_flags) - - local cmd=" - exec >&4 - echo \$\$ - ${(q)daemon} $daemon_args - if [[ \$? != (0|10) && \$? -le 128 && -f ${(q)daemon}-static ]]; then - ${(q)daemon}-static $daemon_args - fi - echo -nE $'bye\x1f0\x1e'" - # Try to use the same executable as the current zsh. Some people like having an ancient - # `zsh` in their PATH while using a newer version. zsh 5.0.2 hangs when enabling `monitor`. - # - # zsh -mc '' &! # hangs when using zsh 5.0.2 - local zsh=${${:-/proc/self/exe}:A} - [[ -x $zsh ]] || zsh=zsh - cmd="cd /; read; unsetopt bg_nice; ${(q)zsh} -dfxc ${(q)cmd} &!; rm -f ${(q)req_fifo} ${(q)resp_fifo} ${(q)lock_file}" - # We use `zsh -c` instead of plain {} or () to work around bugs in zplug (it hangs on - # startup). Double fork is to daemonize. - $zsh -dfmxc $cmd <$req_fifo >$log_file 2>&1 3<$lock_file 4>$resp_fifo &! - - sysopen -w -o cloexec,sync -u req_fd $req_fifo - sysopen -r -o cloexec -u resp_fd $resp_fifo - echo -nE $'0\nhello\x1f\x1e' >&$req_fd - } - - (( async )) && { - daemon_pid=-1 - } || { - local reply IFS='' - read -ru $resp_fd reply - [[ $reply == <1-> ]] - daemon_pid=reply - - function _gitstatus_process_response_${name}() { + if (( ! _GITSTATUS_STATE_$name )); then + print -rn >$file_prefix.lock || return + zsystem flock -f lock_fd $file_prefix.lock || return + typeset -gi _GITSTATUS_LOCK_FD_$name=lock_fd + + { + () { + [[ $sysparams[procsubstpid] == <1-> ]] || return + typeset -gi GITSTATUS_DAEMON_PID_$name="sysparams[procsubstpid]" + sysopen -r -o cloexec -u resp_fd -- $1 || return + typeset -gi _GITSTATUS_RESP_FD_$name=resp_fd + } <( + exec 2>&3 3>&- || return + local pgid=$sysparams[pid] + [[ $pgid == <1-> ]] || return + + { + { + trap '' PIPE + + local os daemon=$GITSTATUS_DAEMON + if [[ -z $daemon ]]; then + os="$(uname -s)" || return + [[ -n $os ]] || return + case $os in + Linux) [[ "$(uname -o)" == Android ]] && os=android;; + (#i)cygwin_nt-*) os=cygwin_nt-10.0;; + (#i)(mingw|msys)) os=msys_nt-10.0;; + esac + local arch + arch="$(uname -m)" || return + [[ -n $arch ]] || return + daemon=$_gitstatus_plugin_dir/bin/gitstatusd-${os:l}-${arch:l} + fi + [[ -x $daemon ]] || return + + if [[ $GITSTATUS_NUM_THREADS == <1-> ]]; then + args+=(-t $GITSTATUS_NUM_THREADS) + else + local cpus + if [[ $+commands[sysctl] == 0 || "${os:-$(uname -s)}" == Linux ]] || + ! cpus="$(sysctl -n hw.ncpu)"; then + if (( ! $+commands[getconf] )) || ! cpus="$(getconf _NPROCESSORS_ONLN)"; then + cpus=8 + fi + fi + args+=(-t $((cpus > 16 ? 32 : cpus > 0 ? 2 * cpus : 16))) + fi + + mkfifo -- $file_prefix.fifo || return + print -n 1 || return + exec <$file_prefix.fifo || return + zf_rm -- $file_prefix.fifo || return + + $daemon "${(@)args}" + if [[ $? != (0|10) && $? -le 128 && -x ${daemon}-static ]]; then + ${daemon}-static "${(@)args}" || return + fi + } always { + kill -- -$pgid + } + } &! + + { + if zsystem flock -- $file_prefix.lock && [[ -e $file_prefix.lock ]]; then + zf_rm -f -- $file_prefix.lock $file_prefix.fifo + kill -- -$pgid + fi + } &! + ) || return + } 3>>$daemon_log /dev/null || return + + typeset -gi _GITSTATUS_STATE_$name=1 + fi + + if (( ! async )); then + (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return + + local ready + [[ -t $resp_fd ]] + sysread -s1 -t $timeout -i $resp_fd ready || return + [[ $ready == 1 ]] || return + + sysopen -w -o cloexec -u req_fd -- $file_prefix.fifo || return + typeset -gi _GITSTATUS_REQ_FD_$name=req_fd + + function _gitstatus_process_response_$name() { + emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent local name=${${(%):-%N}#_gitstatus_process_response_} - (( ARGC == 1 )) && { + if (( ARGC == 1 )); then _gitstatus_process_response $name 0 '' - true - } || { + else gitstatus_stop $name - } + fi } - zle -F $resp_fd _gitstatus_process_response_${name} - - read -r -d $'\x1e' -u $resp_fd -t $timeout reply - [[ $reply == $'hello\x1f0' ]] - - function _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid}() { - emulate -L zsh - setopt err_return no_unset - local fname=${(%):-%N} - local prefix=_gitstatus_cleanup_$$_${ZSH_SUBSHELL}_ - [[ $fname == ${prefix}* ]] || return 0 - local -i daemon_pid=${fname#$prefix} - kill -- -$daemon_pid &>/dev/null || true + if ! zle -F $resp_fd _gitstatus_process_response_$name; then + unfunction _gitstatus_process_response_$name + return 1 + fi + + function _gitstatus_cleanup_$name() { + emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent + local name=${${(%):-%N}#_gitstatus_cleanup_} + (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return + gitstatus_stop $name } - add-zsh-hook zshexit _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid} - } + if ! add-zsh-hook zshexit _gitstatus_cleanup_$name; then + unfunction _gitstatus_cleanup_$name + return 1 + fi - (( ! stderr_fd )) || { - unsetopt xtrace - exec 2>&$stderr_fd {stderr_fd}>&- - stderr_fd=0 - } - } + print -nru $req_fd -- $'hello\x1f\x1e' || return + local expected=$'hello\x1f0' actual + while (( $#actual < $#expected )); do + [[ -t $resp_fd ]] + sysread -s $(($#expected - $#actual)) -t $timeout -i $resp_fd actual || return + done + [[ $actual == $expected ]] || return - gitstatus_start_impl && { - typeset -gi GITSTATUS_DAEMON_PID_${name}=$daemon_pid - (( ! req_fd )) || { - typeset -gi _GITSTATUS_REQ_FD_${name}=$req_fd - typeset -gi _GITSTATUS_RESP_FD_${name}=$resp_fd - typeset -gi _GITSTATUS_LOCK_FD_${name}=$lock_fd - typeset -gi _GITSTATUS_CLIENT_PID_${name}=$$ - typeset -gi _GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name}=$dirty_max_index_size - } - unset -f gitstatus_start_impl - } || { - unsetopt err_return - add-zsh-hook -d zshexit _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid} - (( $+functions[_gitstatus_process_response_${name}] )) && { - zle -F $resp_fd - unfunction _gitstatus_process_response_${name} - } - (( resp_fd )) && exec {resp_fd}>&- - (( req_fd )) && exec {req_fd}>&- - (( lock_fd )) && zsystem flock -u $lock_fd - (( stderr_fd )) && { exec 2>&$stderr_fd {stderr_fd}>&- } - (( daemon_pid > 0 )) && kill -- -$daemon_pid &>/dev/null - - rm -f $lock_file $req_fifo $resp_fifo - unset -f gitstatus_start_impl - - unset GITSTATUS_DAEMON_PID_${name} - unset _GITSTATUS_REQ_FD_${name} - unset _GITSTATUS_RESP_FD_${name} - unset _GITSTATUS_LOCK_FD_${name} - unset _GITSTATUS_CLIENT_PID_${name} - unset _GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name} - - >&2 print -P '[%F{red}ERROR%f]: gitstatus failed to initialize.' - >&2 echo -E '' - >&2 echo -E ' Your git prompt may disappear or become slow.' - if [[ -s $xtrace_file ]]; then - >&2 echo -E '' - >&2 echo -E " The content of ${(q-)xtrace_file} (gitstatus_start_impl xtrace):" - >&2 print -P '%F{yellow}' - >&2 awk '{print " " $0}' <$xtrace_file - >&2 print -P '%F{red} ^ this command failed%f' + zf_rm -- $file_prefix.lock || return + zsystem flock -u $lock_fd || return + + typeset -gi _GITSTATUS_STATE_$name=2 + fi + } always { + local err=$? + if (( stderr_fd )); then + unsetopt xtrace + exec 2>&$stderr_fd fi - if [[ -s $log_file ]]; then - >&2 echo -E '' - >&2 echo -E " The content of ${(q-)log_file} (gitstatus daemon log):" - >&2 print -P '%F{yellow}' - >&2 awk '{print " " $0}' <$log_file - >&2 print -nP '%f' + (( err )) || return + gitstatus_stop $name + setopt prompt_percent no_prompt_subst no_prompt_bang + print -Pru2 -- '[%F{red}ERROR%f]: gitstatus failed to initialize.' + print -ru2 -- '' + print -ru2 -- ' Your git prompt may disappear or become slow.' + if [[ -s $xtrace ]]; then + print -ru2 -- '' + print -ru2 -- " The content of ${(q-)xtrace} (gitstatus_start xtrace):" + print -Pru2 -- '%F{yellow}' + >&2 awk '{print " " $0}' <$xtrace + print -Pnru2 -- '%f' fi - if [[ ${GITSTATUS_LOG_LEVEL:-} == DEBUG ]]; then - >&2 echo -E '' - >&2 echo -E ' Your system information:' - >&2 print -P '%F{yellow}' - >&2 echo -E " zsh: $ZSH_VERSION" - >&2 echo -E " uname -a: $(uname -a)" - >&2 print -P '%f' - >&2 echo -E ' If you need help, open an issue and attach this whole error message to it:' - >&2 echo -E '' - >&2 print -P ' %F{green}https://github.com/romkatv/gitstatus/issues/new%f' + if [[ -s $daemon_log ]]; then + print -ru2 -- '' + print -ru2 -- " The content of ${(q-)daemon_log} (gitstatus daemon log):" + print -Pru2 -- '%F{yellow}' + >&2 awk '{print " " $0}' <$daemon_log + print -Pnru2 -- '%f' + fi + if [[ $GITSTATUS_LOG_LEVEL == DEBUG ]]; then + print -ru2 -- '' + print -ru2 -- ' Your system information:' + print -Pru2 -- '%F{yellow}' + print -ru2 -- " zsh: $ZSH_VERSION" + print -ru2 -- " uname -a: $(uname -a)" + print -Pru2 -- '%f' + print -ru2 -- ' If you need help, open an issue and attach this whole error message to it:' + print -ru2 -- '' + print -Pru2 -- ' %F{green}https://github.com/romkatv/gitstatus/issues/new%f' else - >&2 echo -E '' - >&2 echo -E ' Run the following command to retry with extra diagnostics:' - >&2 print -P '%F{green}' + print -ru2 -- '' + print -ru2 -- ' Run the following command to retry with extra diagnostics:' + print -Pru2 -- '%F{green}' local env="GITSTATUS_LOG_LEVEL=DEBUG" - if [[ -n ${GITSTATUS_NUM_THREADS:-} ]]; then + if [[ -n $GITSTATUS_NUM_THREADS ]]; then env+=" GITSTATUS_NUM_THREADS=${(q)GITSTATUS_NUM_THREADS}" fi - if [[ -n ${GITSTATUS_DAEMON:-} ]]; then + if [[ -n $GITSTATUS_DAEMON ]]; then env+=" GITSTATUS_DAEMON=${(q)GITSTATUS_DAEMON}" fi - >&2 echo -nE " ${env} gitstatus_start ${(@q-)*}" - >&2 print -P '%f' - >&2 echo -E '' - local zshrc='~/.zshrc' - [[ -n ${ZDOTDIR:-} ]] && zshrc=${(D):-$ZDOTDIR/.zshrc} - >&2 echo -E " If this command produces no output, add the following parameter to $zshrc:" - >&2 echo -E '' - >&2 print -P '%F{green} GITSTATUS_LOG_LEVEL=DEBUG%f' - >&2 echo -E '' - >&2 echo -E ' With this parameter, gitstatus will print additional information on error.' + print -nru2 -- " ${env} gitstatus_start ${(@q-)*}" + print -Pru2 -- '%f' + print -ru2 -- '' + local zshrc=${(D)ZDOTDIR:-~}/.zshrc + print -ru2 -- " If this command produces no output, add the following parameter to $zshrc:" + print -ru2 -- '' + print -Pru2 -- '%F{green} GITSTATUS_LOG_LEVEL=DEBUG%f' + print -ru2 -- '' + print -ru2 -- ' With this parameter gitstatus will print additional information on error.' fi - - return 1 } } @@ -582,46 +640,57 @@ function gitstatus_start() { # # Usage: gitstatus_stop NAME. function gitstatus_stop() { - emulate -L zsh - setopt no_unset - (( ARGC == 1 )) || { echo "usage: gitstatus_stop NAME" >&2; return 1 } - - local name=$1 - - local req_fd_var=_GITSTATUS_REQ_FD_${name} - local resp_fd_var=_GITSTATUS_RESP_FD_${name} - local lock_fd_var=_GITSTATUS_LOCK_FD_${name} - local daemon_pid_var=GITSTATUS_DAEMON_PID_${name} - local client_pid_var=_GITSTATUS_CLIENT_PID_${name} - local dirty_size_var=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name} - - [[ ${(P)daemon_pid_var:-} != -1 ]] || gitstatus_start -t 0 "$name" 2>/dev/null + emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent - local req_fd=${(P)req_fd_var:-} - local resp_fd=${(P)resp_fd_var:-} - local lock_fd=${(P)lock_fd_var:-} - local daemon_pid=${(P)daemon_pid_var:-0} - - local cleanup_func=_gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid} - - (( $+functions[_gitstatus_process_response_${name}] )) && { - zle -F $resp_fd - unfunction _gitstatus_process_response_${name} - } + if (( ARGC != 1 )); then + print -ru2 -- "gitstatus_stop: exactly one positional argument is required" + return 1 + fi - (( resp_fd )) && exec {resp_fd}>&- - (( req_fd )) && exec {req_fd}>&- - (( lock_fd )) && zsystem flock -u $lock_fd - (( daemon_pid > 0 )) && kill -- -$daemon_pid &>/dev/null + local name=$1 + if [[ $name != [[:IDENT:]]## ]]; then + print -ru2 -- "gitstatus_stop: invalid positional argument: $name" + return 1 + fi - unset $req_fd_var $resp_fd_var $lock_fd_var $daemon_pid_var $client_pid_var $dirty_size_var + local state_var=_GITSTATUS_STATE_$name + local req_fd_var=_GITSTATUS_REQ_FD_$name + local resp_fd_var=_GITSTATUS_RESP_FD_$name + local lock_fd_var=_GITSTATUS_LOCK_FD_$name + local client_pid_var=GITSTATUS_CLIENT_PID_$name + local daemon_pid_var=GITSTATUS_DAEMON_PID_$name + local inflight_var=_GITSTATUS_NUM_INFLIGHT_$name + local file_prefix_var=_GITSTATUS_FILE_PREFIX_$name + local dirty_max_index_size_var=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_$name + + local req_fd=${(P)req_fd_var} + local resp_fd=${(P)resp_fd_var} + local lock_fd=${(P)lock_fd_var} + local daemon_pid=${(P)daemon_pid_var} + local file_prefix=${(P)file_prefix_var} + + local cleanup=_gitstatus_cleanup_$name + local process=_gitstatus_process_response_$name + + [[ -n $daemon_pid ]] && kill -- -$daemon_pid 2>/dev/null + + if (( $+functions[$cleanup] )); then + add-zsh-hook -d zshexit $cleanup + unfunction -- $cleanup + fi - if (( $+functions[$cleanup_func] )); then - add-zsh-hook -d zshexit $cleanup_func - unfunction $cleanup_func + if (( $+functions[$process] )); then + [[ -n $resp_fd ]] && zle -F $resp_fd + unfunction -- $process fi - return 0 + [[ -n $file_prefix ]] && zf_rm -f -- "$file_prefix.lock" "$file_prefix.fifo" + [[ -n $lock_fd ]] && system flock -u $lock_fd + [[ -n $req_fd ]] && exec {req_fd}>&- + [[ -n $resp_fd ]] && exec {resp_fd}>&- + + unset $state_var $req_fd_var $lock_fd_var $resp_fd_var $client_pid_var $daemon_pid_var + unset $inflight_var $file_prefix_var $dirty_max_index_size_var } # Usage: gitstatus_check NAME. @@ -629,10 +698,20 @@ function gitstatus_stop() { # Returns 0 if and only if `gitstatus_start NAME` has succeeded previously. # If it returns non-zero, gitstatus_query NAME is guaranteed to return non-zero. function gitstatus_check() { - emulate -L zsh - (( ARGC == 1 )) || { echo "usage: gitstatus_check NAME" >&2; return 1 } - local daemon_pid_var=GITSTATUS_DAEMON_PID_${1} - (( ${(P)daemon_pid_var:-0} > 0 )) + emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent + + if (( ARGC != 1 )); then + print -ru2 -- "gitstatus_check: exactly one positional argument is required" + return 1 + fi + + local name=$1 + if [[ $name != [[:IDENT:]]## ]]; then + print -ru2 -- "gitstatus_check: invalid positional argument: $name" + return 1 + fi + + (( _GITSTATUS_STATE_$name == 2 )) } (( ${#_gitstatus_opts} )) && setopt ${_gitstatus_opts[@]}