diff options
-rw-r--r-- | gitstatus/gitstatus.plugin.zsh | 918 | ||||
-rw-r--r-- | internal/p10k.zsh | 180 |
2 files changed, 642 insertions, 456 deletions
diff --git a/gitstatus/gitstatus.plugin.zsh b/gitstatus/gitstatus.plugin.zsh index fb32e106..13bbf2a6 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,314 +369,365 @@ 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 stderr_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}:A}/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 + + () { + if [[ $xtrace != /dev/null && -o no_xtrace ]]; then + exec {stderr_fd}>&2 || return + 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 + if [[ -r /proc/version && "$(</proc/version)" == *Microsoft* ]]; then + lock_fd=-1 + else + print -rn >$file_prefix.lock || return + zsystem flock -f lock_fd $file_prefix.lock || return + [[ $lock_fd == <1-> ]] || return + fi + typeset -gi _GITSTATUS_LOCK_FD_$name=lock_fd + + { + () { + typeset -gi GITSTATUS_DAEMON_PID_$name="${sysparams[procsubstpid]:--1}" + sysopen -r -o cloexec -u resp_fd -- $1 || return + [[ $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 + + if [[ -z $GITSTATUS_DAEMON || $GITSTATUS_NUM_THREADS != <1-> ]]; then + local kernel + kernel="${(L)$(uname -s)}" || return + [[ -n $kernel ]] || return + fi + + if [[ $GITSTATUS_DAEMON == /* ]]; then + local daemons=($GITSTATUS_DAEMON) + elif (( $+commands[$GITSTATUS_DAEMON] )); then + local daemons=($commands[$GITSTATUS_DAEMON]) + elif [[ -n $GITSTATUS_DAEMON ]]; then + local daemons=($_gitstatus_plugin_dir/{usrbin,bin}/$GITSTATUS_DAEMON) + else + local os + case $kernel in + linux) + os="${(L)$(uname -o)}" || return + [[ -n $os ]] || return + [[ $os == android ]] || os=linux + ;; + cygwin_nt-*) os=cygwin_nt-10.0;; + mingw|msys) os=msys_nt-10.0;; + *) os=$kernel;; + esac + local arch + arch="${(L)$(uname -m)}" || return + [[ -n $arch ]] || return + local daemons=($_gitstatus_plugin_dir/{usrbin,bin}/gitstatusd-$os-$arch{,-static}) + fi + + daemons=(${^daemons}(N:A)) + daemons=(${^daemons}(N*)) + (( $#daemons )) || return + + if [[ $GITSTATUS_NUM_THREADS == <1-> ]]; then + args+=(-t $GITSTATUS_NUM_THREADS) + else + local cpus + if (( ! $+commands[sysctl] )) || [[ $kernel == 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 -rn -- ${(l:20:)pgid} || return + exec <$file_prefix.fifo || return + zf_rm -- $file_prefix.fifo || return + + local daemon + for daemon in $daemons; do + $daemon "${(@)args}" + local -i ret=$? + (( ret == 0 || ret == 10 || ret > 128 )) && return ret + done + } always { + local -i ret=$? + kill -- -$pgid + } + } &! + + (( lock_fd == -1 )) && return + + { + 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 >/dev/null || return + + typeset -gi _GITSTATUS_STATE_$name=1 + fi + + if (( ! async )); then + (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return + + local pgid + while (( $#pgid < 20 )); do + [[ -t $resp_fd ]] + sysread -s $((20 - $#pgid)) -t $timeout -i $resp_fd 'pgid[$#pgid+1]' || return + done + [[ $pgid == ' '#<1-> ]] || return + typeset -gi GITSTATUS_DAEMON_PID_$name=pgid + + sysopen -w -o cloexec -u req_fd -- $file_prefix.fifo || return + [[ $req_fd == <1-> ]] || 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} - } - - (( ! stderr_fd )) || { - unsetopt xtrace - exec 2>&$stderr_fd {stderr_fd}>&- - stderr_fd=0 - } - } + if ! add-zsh-hook zshexit _gitstatus_cleanup_$name; then + unfunction _gitstatus_cleanup_$name + return 1 + fi - 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' + print -nru $req_fd -- $'hello\x1f\x1e' || return + local expected=$'hello\x1f0\x1e' actual + while (( $#actual < $#expected )); do + [[ -t $resp_fd ]] + sysread -s $(($#expected - $#actual)) -t $timeout -i $resp_fd 'actual[$#actual+1]' || return + done + [[ $actual == $expected ]] || return + + if (( lock_fd != -1 )); then + zf_rm -- $file_prefix.lock || return + zsystem flock -u $lock_fd || return + fi + unset _GITSTATUS_LOCK_FD_$name + + typeset -gi _GITSTATUS_STATE_$name=2 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' + } + + local -i err=$? + (( stderr_fd )) && exec 2>&$stderr_fd + (( err == 0 )) && 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 -Pru2 -- "%F{red} ^ this command failed ($err)%f" + fi + 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 + 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 + env+=" GITSTATUS_NUM_THREADS=${(q)GITSTATUS_NUM_THREADS}" 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' - else - >&2 echo -E '' - >&2 echo -E ' Run the following command to retry with extra diagnostics:' - >&2 print -P '%F{green}' - local env="GITSTATUS_LOG_LEVEL=DEBUG" - if [[ -n ${GITSTATUS_NUM_THREADS:-} ]]; then - env+=" GITSTATUS_NUM_THREADS=${(q)GITSTATUS_NUM_THREADS}" - fi - 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.' + if [[ -n $GITSTATUS_DAEMON ]]; then + env+=" GITSTATUS_DAEMON=${(q)GITSTATUS_DAEMON}" fi + 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 - } + return err } # Stops gitstatusd if it's running. # # 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 + emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent - 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 - - 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} + if (( ARGC != 1 )); then + print -ru2 -- "gitstatus_stop: exactly one positional argument is required" + return 1 + fi - local cleanup_func=_gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid} + local name=$1 + if [[ $name != [[:IDENT:]]## ]]; then + print -ru2 -- "gitstatus_stop: invalid positional argument: $name" + return 1 + fi - (( $+functions[_gitstatus_process_response_${name}] )) && { - zle -F $resp_fd - unfunction _gitstatus_process_response_${name} - } + 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 + + if (( $+functions[$cleanup] )); then + add-zsh-hook -d zshexit $cleanup + unfunction -- $cleanup + 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 + if (( $+functions[$process] )); then + [[ -n $resp_fd ]] && zle -F $resp_fd + unfunction -- $process + fi - unset $req_fd_var $resp_fd_var $lock_fd_var $daemon_pid_var $client_pid_var $dirty_size_var + [[ $daemon_pid == <1-> ]] && kill -- -$daemon_pid 2>/dev/null + [[ $file_prefix == /* ]] && zf_rm -f -- $file_prefix.lock $file_prefix.fifo + [[ $lock_fd == <1-> ]] && zsystem flock -u $lock_fd + [[ $req_fd == <1-> ]] && exec {req_fd}>&- + [[ $resp_fd == <1-> ]] && exec {resp_fd}>&- - if (( $+functions[$cleanup_func] )); then - add-zsh-hook -d zshexit $cleanup_func - unfunction $cleanup_func - fi + 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 - return 0 + unset VCS_STATUS_RESULT + _gitstatus_clear } # Usage: gitstatus_check NAME. @@ -629,10 +735,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[@]} diff --git a/internal/p10k.zsh b/internal/p10k.zsh index d6707fbf..51f376fc 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -3687,9 +3687,16 @@ function _p9k_vcs_resume() { fi fi - _p9k__refresh_reason=gitstatus - _p9k_set_prompt - _p9k__refresh_reason='' + if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then + local _p9k__prompt _p9k__prompt_side=$_p9k_vcs_side + local -i _p9k__has_upglob _p9k__segment_index=_p9k_vcs_index + _p9k_vcs_render + typeset -g _p9k__vcs=$_p9k__prompt + else + _p9k__refresh_reason=gitstatus + _p9k_set_prompt + _p9k__refresh_reason='' + fi _p9k_reset_prompt } @@ -3724,6 +3731,16 @@ function _p9k_vcs_gitstatus() { fi (( _p9k__prompt_idx == 1 )) && timeout=0 _p9k__git_dir=$GIT_DIR + if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then + if ! gitstatus_query -d $_p9k__cwd_a -t 0 -c '_p9k_vcs_resume 1' POWERLEVEL9K; then + unset VCS_STATUS_RESULT + return 1 + fi + _p9k__gitstatus_next_dir='' + _p9k__gitstatus_start_time=$EPOCHREALTIME + typeset -g _p9k__vcs= + return 0 + fi if ! gitstatus_query -d $_p9k__cwd_a -t $timeout -c '_p9k_vcs_resume 1' POWERLEVEL9K; then unset VCS_STATUS_RESULT return 1 @@ -3743,8 +3760,13 @@ function _p9k_vcs_gitstatus() { # Segment to show VCS information prompt_vcs() { + if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then + _p9k__prompt+='${(e)_p9k__vcs}' + return + fi + local -a backends=($_POWERLEVEL9K_VCS_BACKENDS) - if (( ${backends[(I)git]} && !_p9k__gitstatus_disabled )) && _p9k_vcs_gitstatus; then + if (( ${backends[(I)git]} && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )) && _p9k_vcs_gitstatus; then _p9k_vcs_render && return backends=(${backends:#git}) fi @@ -5079,6 +5101,8 @@ function _p9k_set_prompt() { [[ $1 == instant_ ]] || PROMPT+='${$((_p9k_on_expand()))+}' PROMPT+=$_p9k_prompt_prefix_left + local -i _p9k__has_upglob + local -i left_idx=1 right_idx=1 num_lines=$#_p9k_line_segments_left for _p9k__line_index in {1..$num_lines}; do local right= @@ -6017,6 +6041,14 @@ _p9k_precmd_impl() { _p9k_fetch_cwd + _p9k__refresh_reason=precmd + __p9k_reset_state=1 + + if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then + unset _p9k__vcs + _p9k_vcs_gitstatus + fi + local f_compute for f_compute in "${_p9k__async_segments_compute[@]}"; do _p9k_worker_invoke ${f_compute%% *} ${(e)f_compute} @@ -6024,8 +6056,8 @@ _p9k_precmd_impl() { _p9k__expanded=0 - _p9k__refresh_reason=precmd _p9k_set_prompt + _p9k__refresh_reason='' if [[ $precmd_functions[1] != _p9k_do_nothing && $precmd_functions[(I)_p9k_do_nothing] != 0 ]]; then @@ -6041,7 +6073,17 @@ _p9k_precmd_impl() { preexec_functions=(${(@)preexec_functions:#_p9k_preexec2} _p9k_preexec2) fi - __p9k_reset_state=1 + if (( _p9k_vcs_index && $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )); then + # TODO: use better timeout + gitstatus_process_results -t $_POWERLEVEL9K_VCS_MAX_SYNC_LATENCY_SECONDS POWERLEVEL9K + if (( ! $+_p9k__vcs )); then + local _p9k__prompt _p9k__prompt_side=$_p9k_vcs_side + local -i _p9k__has_upglob _p9k__segment_index=_p9k_vcs_index + _p9k_vcs_render + typeset -g _p9k__vcs=$_p9k__prompt + fi + fi + _p9k_worker_receive __p9k_reset_state=0 } @@ -6105,6 +6147,9 @@ typeset -g _p9k__param_pat typeset -g _p9k__param_sig _p9k_init_vars() { + typeset -gi _p9k_vcs_index + typeset -g _p9k_vcs_side + typeset -ga _p9k_taskwarrior_meta_files typeset -ga _p9k_taskwarrior_meta_non_files typeset -g _p9k_taskwarrior_meta_sig @@ -6223,7 +6268,6 @@ _p9k_init_vars() { typeset -gA _p9k_git_slow # git workdir => the last state we've seen for it typeset -gA _p9k__gitstatus_last - typeset -gi _p9k__gitstatus_disabled typeset -gF _p9k__gitstatus_start_time typeset -g _p9k__prompt typeset -g _p9k__rprompt @@ -7172,7 +7216,7 @@ _p9k_must_init() { [[ $sig == $_p9k__param_sig ]] && return 1 _p9k_deinit fi - _p9k__param_pat=$'v56\1'${ZSH_VERSION}$'\1'${ZSH_PATCHLEVEL}$'\1' + _p9k__param_pat=$'v57\1'${ZSH_VERSION}$'\1'${ZSH_PATCHLEVEL}$'\1' _p9k__param_pat+=$'${#parameters[(I)POWERLEVEL9K_*]}\1${(%):-%n%#}\1$GITSTATUS_LOG_LEVEL\1' _p9k__param_pat+=$'$GITSTATUS_ENABLE_LOGGING\1$GITSTATUS_DAEMON\1$GITSTATUS_NUM_THREADS\1' _p9k__param_pat+=$'$DEFAULT_USER\1${ZLE_RPROMPT_INDENT:-1}\1$P9K_SSH\1$__p9k_ksh_arrays' @@ -7362,27 +7406,72 @@ function _p9k_init_cacheable() { done fi fi + + if [[ $#_POWERLEVEL9K_VCS_BACKENDS == 1 && $_POWERLEVEL9K_VCS_BACKENDS[1] == git ]]; then + local elem line + local -i i=0 + for line in $_p9k_line_segments_left; do + for elem in ${${(0)line}%_joined}; do + (( ++i )) + if [[ $elem == vcs ]]; then + if (( _p9k_vcs_index )); then + _p9k_vcs_index=-1 + else + _p9k_vcs_index=i + _p9k_vcs_side=left + fi + fi + done + done + i=0 + for line in $_p9k_line_segments_right; do + for elem in ${${(0)line}%_joined}; do + (( ++i )) + if [[ $elem == vcs ]]; then + if (( _p9k_vcs_index )); then + _p9k_vcs_index=-1 + else + _p9k_vcs_index=i + _p9k_vcs_side=right + fi + fi + done + done + if (( _p9k_vcs_index > 0 )); then + local state + for state in ${(k)__p9k_vcs_states}; do + _p9k_param prompt_vcs_$state CONTENT_EXPANSION x + if [[ -z $_p9k__ret ]]; then + _p9k_vcs_index=-1 + break + fi + done + fi + if (( _p9k_vcs_index == -1 )); then + _p9k_vcs_index=0 + _p9k_vcs_side= + fi + fi } _p9k_init_vcs() { _p9k_segment_in_use vcs || return _p9k_vcs_info_init if (( $+functions[_p9k_preinit] )); then - (( $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )) && gitstatus_start POWERLEVEL9K || _p9k__gitstatus_disabled=1 - return 0 - fi - if (( _POWERLEVEL9K_DISABLE_GITSTATUS )); then - _p9k__gitstatus_disabled=1 + (( $+GITSTATUS_DAEMON_PID_POWERLEVEL9K )) && gitstatus_start POWERLEVEL9K return 0 fi + (( _POWERLEVEL9K_DISABLE_GITSTATUS )) && return (( $_POWERLEVEL9K_VCS_BACKENDS[(I)git] )) || return local gitstatus_dir=${_POWERLEVEL9K_GITSTATUS_DIR:-${__p9k_root_dir}/gitstatus} - if [[ -z $GITSTATUS_DAEMON && $_p9k_uname_m == (i686|arm7l) && -z $gitstatus_dir/bin/*-$_p9k_uname_m(-static|)(#qN) ]]; then - _p9k__gitstatus_disabled=1 + + local id=${(L)${${(M)_p9k_uname_o:#Android}:-$_p9k_uname}}-${(L)_p9k_uname_m} + if [[ -z $GITSTATUS_DAEMON && $id == (android-arm7l|*-i686) && + -z $gitstatus_dir/bin/(usrbin|bin)/*-$id(|-static)(#qN) ]]; then >&2 echo -E - "${(%):-[%1FERROR%f]: %BPowerlevel10k%b is unable to use %Bgitstatus%b. Git prompt will be slow.}" >&2 echo -E - "" - >&2 echo -E - "${(%):-Reason: There is no %Bgitstatusd%b binary for $_p9k_uname_m (32-bit architecture).}" + >&2 echo -E - "${(%):-Reason: There is no %Bgitstatusd%b binary for %B${id//\%/%%}%b (32-bit).}" >&2 echo -E - "" >&2 echo -E - "${(%):-You can:}" >&2 echo -E - "" @@ -7413,50 +7502,31 @@ _p9k_init_vcs() { return 0 fi - local daemon=${GITSTATUS_DAEMON} - if [[ -z $daemon ]]; then - daemon=$gitstatus_dir/bin/gitstatusd- - if [[ $_p9k_uname_o == Android ]]; then - daemon+=android - elif [[ $_p9k_uname == (#i)(mingw|msys)* ]]; then - daemon+=msys_nt-10.0 - elif [[ $_p9k_uname == (#i)cygwin_nt* ]]; then - daemon+=cygwin_nt-10.0 - else - daemon+=${_p9k_uname:l} - fi - daemon+=-${_p9k_uname_m:l} - fi - local -i threads=${GITSTATUS_NUM_THREADS:-0} - if (( threads <= 0 )); then - threads=$(( _p9k_num_cpus * 2 )) - (( threads > 0 )) || threads=8 - (( threads <= 32 )) || threads=32 - fi typeset -g _p9k_preinit="function _p9k_preinit() { [[ \$ZSH_VERSION == ${(q)ZSH_VERSION} ]] || return [[ -r ${(q)gitstatus_dir}/gitstatus.plugin.zsh ]] || return source ${(q)gitstatus_dir}/gitstatus.plugin.zsh || return - GITSTATUS_DAEMON=${(q)daemon} GITSTATUS_NUM_THREADS=$threads \ - GITSTATUS_LOG_LEVEL=${(q)GITSTATUS_LOG_LEVEL} \ - GITSTATUS_ENABLE_LOGGING=${(q)GITSTATUS_ENABLE_LOGGING} gitstatus_start \ - -s $_POWERLEVEL9K_VCS_STAGED_MAX_NUM \ - -u $_POWERLEVEL9K_VCS_UNSTAGED_MAX_NUM \ - -d $_POWERLEVEL9K_VCS_UNTRACKED_MAX_NUM \ - -c $_POWERLEVEL9K_VCS_CONFLICTED_MAX_NUM \ - -m $_POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY \ - ${${_POWERLEVEL9K_VCS_RECURSE_UNTRACKED_DIRS:#0}:+-e} \ - -a POWERLEVEL9K + GITSTATUS_DAEMON=${(q)GITSTATUS_DAEMON} \ + GITSTATUS_NUM_THREADS=${(q)GITSTATUS_NUM_THREADS} \ + GITSTATUS_LOG_LEVEL=${(q)GITSTATUS_LOG_LEVEL} \ + GITSTATUS_ENABLE_LOGGING=${(q)GITSTATUS_ENABLE_LOGGING} \ + gitstatus_start \ + -s $_POWERLEVEL9K_VCS_STAGED_MAX_NUM \ + -u $_POWERLEVEL9K_VCS_UNSTAGED_MAX_NUM \ + -d $_POWERLEVEL9K_VCS_UNTRACKED_MAX_NUM \ + -c $_POWERLEVEL9K_VCS_CONFLICTED_MAX_NUM \ + -m $_POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY \ + ${${_POWERLEVEL9K_VCS_RECURSE_UNTRACKED_DIRS:#0}:+-e} \ + -a POWERLEVEL9K }" - source ${gitstatus_dir}/gitstatus.plugin.zsh - GITSTATUS_DAEMON=$daemon GITSTATUS_NUM_THREADS=$threads gitstatus_start \ - -s $_POWERLEVEL9K_VCS_STAGED_MAX_NUM \ - -u $_POWERLEVEL9K_VCS_UNSTAGED_MAX_NUM \ - -d $_POWERLEVEL9K_VCS_UNTRACKED_MAX_NUM \ - -c $_POWERLEVEL9K_VCS_CONFLICTED_MAX_NUM \ - -m $_POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY \ - ${${_POWERLEVEL9K_VCS_RECURSE_UNTRACKED_DIRS:#0}:+-e} \ - POWERLEVEL9K || _p9k__gitstatus_disabled=1 + source $gitstatus_dir/gitstatus.plugin.zsh && gitstatus_start \ + -s $_POWERLEVEL9K_VCS_STAGED_MAX_NUM \ + -u $_POWERLEVEL9K_VCS_UNSTAGED_MAX_NUM \ + -d $_POWERLEVEL9K_VCS_UNTRACKED_MAX_NUM \ + -c $_POWERLEVEL9K_VCS_CONFLICTED_MAX_NUM \ + -m $_POWERLEVEL9K_VCS_MAX_INDEX_SIZE_DIRTY \ + ${${_POWERLEVEL9K_VCS_RECURSE_UNTRACKED_DIRS:#0}:+-e} \ + POWERLEVEL9K } _p9k_init() { |