1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-25 21:40:08 +00:00

pull upstream changes from gitstatus

This commit is contained in:
romkatv 2020-02-22 18:29:21 +01:00
parent 7aab4d7ac5
commit b4deda53a3

View file

@ -51,8 +51,9 @@
[[ ! -o 'no_brace_expand' ]] || _gitstatus_opts+=('no_brace_expand') [[ ! -o 'no_brace_expand' ]] || _gitstatus_opts+=('no_brace_expand')
'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand' 'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand'
autoload -Uz add-zsh-hook autoload -Uz add-zsh-hook || return
zmodload zsh/datetime zsh/system zmodload zsh/datetime zsh/system || return
zmodload -F zsh/files b:zf_rm || return
typeset -g _gitstatus_plugin_dir=${${(%):-%x}:A:h} 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 # 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. # completed yet. If you need to issue concurrent requests, use different NAME arguments.
function gitstatus_query() { function gitstatus_query() {
emulate -L zsh emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
setopt err_return no_unset
local opt unset VCS_STATUS_RESULT
local dir
local callback local opt dir callback OPTARG
local -i no_diff OPTIND
local -F timeout=-1 local -F timeout=-1
local no_diff=0 while getopts ":d:c:t:p" opt; do
while true; do
getopts "d:c:t:p" opt || break
case $opt in case $opt in
d) dir=$OPTARG;; +p) no_diff=0;;
c) callback=$OPTARG;; p) no_diff=1;;
t) timeout=$OPTARG;; d) dir=$OPTARG;;
p) no_diff=1;; c) callback=$OPTARG;;
?) return 1;; t)
done) break;; 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 esac
done done
(( OPTIND == ARGC )) || { echo "usage: gitstatus_query [OPTION]... NAME" >&2; return 1 }
local name=${*[$OPTIND]}
local daemon_pid_var=GITSTATUS_DAEMON_PID_${name} if (( OPTIND != ARGC )); then
(( ${(P)daemon_pid_var:-0} > 0 )) print -ru2 -- "gitstatus_start: exactly one positional argument is required"
return 1
fi
# Verify that gitstatus_query is running in the same process that ran gitstatus_start. local name=$*[OPTIND]
local client_pid_var=_GITSTATUS_CLIENT_PID_${name} if [[ $name != [[:IDENT:]]## ]]; then
[[ ${(P)client_pid_var} == $$ ]] print -ru2 -- "gitstatus_start: invalid positional argument: $name"
return 1
fi
[[ -z ${GIT_DIR:-} ]] && { (( _GITSTATUS_STATE_$name == 2 )) || return
if [[ -z $GIT_DIR ]]; then
[[ $dir == /* ]] || dir=${(%):-%/}/$dir [[ $dir == /* ]] || dir=${(%):-%/}/$dir
} || { else
[[ $GIT_DIR == /* ]] && dir=:$GIT_DIR || dir=:${(%):-%/}/$GIT_DIR [[ $GIT_DIR == /* ]] && dir=:$GIT_DIR || dir=:${(%):-%/}/$GIT_DIR
} fi
local req_fd_var=_GITSTATUS_REQ_FD_${name} local -i req_fd=${(P)${:-_GITSTATUS_REQ_FD_$name}}
local -i req_fd=${(P)req_fd_var} local req_id=$EPOCHREALTIME
local -r req_id="$EPOCHREALTIME" print -rnu $req_fd -- $req_id' '$callback$'\x1f'$dir$'\x1f'$no_diff$'\x1e' || return
echo -nE $req_id' '$callback$'\x1f'$dir$'\x1f'$no_diff$'\x1e' >&$req_fd
while true; do (( ++_GITSTATUS_NUM_INFLIGHT_$name ))
_gitstatus_process_response $name $timeout $req_id
[[ $VCS_STATUS_RESULT == *-async ]] || break if (( timeout == 0 )); then
done 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 ]] [[ $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
(( _GITSTATUS_STATE_$name == 2 )) || return
while (( _GITSTATUS_NUM_INFLIGHT_$name )); do
_gitstatus_process_response $name $timeout '' || return
[[ $VCS_STATUS_RESULT == *-async ]] || break
done
return 0
}
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}
}
function _gitstatus_process_response() { function _gitstatus_process_response() {
emulate -L zsh local name=$1 timeout req_id=$3 buf
setopt err_return no_unset local -i resp_fd=_GITSTATUS_RESP_FD_$name
local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_$name
local name=$1 (( $2 >= 0 )) && timeout=-t$2 && [[ -t $resp_fd ]]
local -F timeout=$2 sysread $timeout -i $resp_fd 'buf[$#buf+1]' || {
local req_id=$3 if (( $? == 4 )); then
local resp_fd_var=_GITSTATUS_RESP_FD_${name} if [[ -n $req_id ]]; then
local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name} typeset -g VCS_STATUS_RESULT=tout
_gitstatus_clear
typeset -g VCS_STATUS_RESULT fi
(( timeout >= 0 )) && local -a t=(-t $timeout) || local -a t=() return 0
local -a resp else
local IFS=$'\x1f' gitstatus_stop $name
read -rd $'\x1e' -u ${(P)resp_fd_var} $t -A resp || { return 1
VCS_STATUS_RESULT=tout fi
return
} }
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]}}") local s
[[ ${header[1]} == $req_id ]] && local -i ours=1 || local -i ours=0 for s in ${(ps:\x1e:)buf}; do
shift header local -a resp=("${(@ps:\x1f:)s}")
[[ ${resp[2]} == 1 ]] && { if (( resp[2] )); then
(( ours )) && VCS_STATUS_RESULT=ok-sync || VCS_STATUS_RESULT=ok-async if [[ $resp[1] == $req_id' '* ]]; then
typeset -g VCS_STATUS_WORKDIR="${resp[3]}" typeset -g VCS_STATUS_RESULT=ok-sync
typeset -g VCS_STATUS_COMMIT="${resp[4]}" else
typeset -g VCS_STATUS_LOCAL_BRANCH="${resp[5]}" typeset -g VCS_STATUS_RESULT=ok-async
typeset -g VCS_STATUS_REMOTE_BRANCH="${resp[6]}" fi
typeset -g VCS_STATUS_REMOTE_NAME="${resp[7]}" for VCS_STATUS_WORKDIR \
typeset -g VCS_STATUS_REMOTE_URL="${resp[8]}" VCS_STATUS_COMMIT \
typeset -g VCS_STATUS_ACTION="${resp[9]}" VCS_STATUS_LOCAL_BRANCH \
typeset -gi VCS_STATUS_INDEX_SIZE="${resp[10]}" VCS_STATUS_REMOTE_BRANCH \
typeset -gi VCS_STATUS_NUM_STAGED="${resp[11]}" VCS_STATUS_REMOTE_NAME \
typeset -gi VCS_STATUS_NUM_UNSTAGED="${resp[12]}" VCS_STATUS_REMOTE_URL \
typeset -gi VCS_STATUS_NUM_CONFLICTED="${resp[13]}" VCS_STATUS_ACTION \
typeset -gi VCS_STATUS_NUM_UNTRACKED="${resp[14]}" VCS_STATUS_INDEX_SIZE \
typeset -gi VCS_STATUS_COMMITS_AHEAD="${resp[15]}" VCS_STATUS_NUM_STAGED \
typeset -gi VCS_STATUS_COMMITS_BEHIND="${resp[16]}" VCS_STATUS_NUM_UNSTAGED \
typeset -gi VCS_STATUS_STASHES="${resp[17]}" VCS_STATUS_NUM_CONFLICTED \
typeset -g VCS_STATUS_TAG="${resp[18]}" VCS_STATUS_NUM_UNTRACKED \
typeset -gi VCS_STATUS_NUM_UNSTAGED_DELETED="${resp[19]}" VCS_STATUS_COMMITS_AHEAD \
typeset -gi VCS_STATUS_NUM_STAGED_NEW="${resp[20]:-0}" VCS_STATUS_COMMITS_BEHIND \
typeset -gi VCS_STATUS_NUM_STAGED_DELETED="${resp[21]:-0}" VCS_STATUS_STASHES \
typeset -g VCS_STATUS_PUSH_REMOTE_NAME="${resp[22]:-}" VCS_STATUS_TAG \
typeset -g VCS_STATUS_PUSH_REMOTE_URL="${resp[23]:-}" VCS_STATUS_NUM_UNSTAGED_DELETED \
typeset -gi VCS_STATUS_PUSH_COMMITS_AHEAD="${resp[24]:-0}" VCS_STATUS_NUM_STAGED_NEW \
typeset -gi VCS_STATUS_PUSH_COMMITS_BEHIND="${resp[25]:-0}" VCS_STATUS_NUM_STAGED_DELETED \
typeset -gi VCS_STATUS_NUM_SKIP_WORKTREE="${resp[26]:-0}" VCS_STATUS_PUSH_REMOTE_NAME \
typeset -gi VCS_STATUS_NUM_ASSUME_UNCHANGED="${resp[27]:-0}" VCS_STATUS_PUSH_REMOTE_URL \
typeset -gi VCS_STATUS_HAS_STAGED=$((VCS_STATUS_NUM_STAGED > 0)) VCS_STATUS_PUSH_COMMITS_AHEAD \
(( dirty_max_index_size >= 0 && VCS_STATUS_INDEX_SIZE > dirty_max_index_size )) && { VCS_STATUS_PUSH_COMMITS_BEHIND \
typeset -gi VCS_STATUS_HAS_UNSTAGED=-1 VCS_STATUS_NUM_SKIP_WORKTREE \
typeset -gi VCS_STATUS_HAS_CONFLICTED=-1 VCS_STATUS_NUM_ASSUME_UNCHANGED in "${(@)resp[3,27]}"; do
typeset -gi VCS_STATUS_HAS_UNTRACKED=-1 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_UNSTAGED=$((VCS_STATUS_NUM_UNSTAGED > 0)) typeset -gi VCS_STATUS_HAS_STAGED=$((VCS_STATUS_NUM_STAGED > 0))
typeset -gi VCS_STATUS_HAS_CONFLICTED=$((VCS_STATUS_NUM_CONFLICTED > 0)) if (( dirty_max_index_size >= 0 && VCS_STATUS_INDEX_SIZE > dirty_max_index_size )); then
typeset -gi VCS_STATUS_HAS_UNTRACKED=$((VCS_STATUS_NUM_UNTRACKED > 0)) typeset -gi \
} VCS_STATUS_HAS_UNSTAGED=-1 \
} || { VCS_STATUS_HAS_CONFLICTED=-1 \
(( ours )) && VCS_STATUS_RESULT=norepo-sync || VCS_STATUS_RESULT=norepo-async VCS_STATUS_HAS_UNTRACKED=-1
unset VCS_STATUS_WORKDIR else
unset VCS_STATUS_COMMIT typeset -gi \
unset VCS_STATUS_LOCAL_BRANCH VCS_STATUS_HAS_UNSTAGED=$((VCS_STATUS_NUM_UNSTAGED > 0)) \
unset VCS_STATUS_REMOTE_BRANCH VCS_STATUS_HAS_CONFLICTED=$((VCS_STATUS_NUM_CONFLICTED > 0)) \
unset VCS_STATUS_REMOTE_NAME VCS_STATUS_HAS_UNTRACKED=$((VCS_STATUS_NUM_UNTRACKED > 0))
unset VCS_STATUS_REMOTE_URL fi
unset VCS_STATUS_ACTION else
unset VCS_STATUS_INDEX_SIZE if [[ $resp[1] == $req_id' '* ]]; then
unset VCS_STATUS_NUM_STAGED typeset -g VCS_STATUS_RESULT=norepo-sync
unset VCS_STATUS_NUM_UNSTAGED else
unset VCS_STATUS_NUM_CONFLICTED typeset -g VCS_STATUS_RESULT=norepo-async
unset VCS_STATUS_NUM_UNTRACKED fi
unset VCS_STATUS_HAS_STAGED _gitstatus_clear
unset VCS_STATUS_HAS_UNSTAGED fi
unset VCS_STATUS_HAS_CONFLICTED (( --_GITSTATUS_NUM_INFLIGHT_$name ))
unset VCS_STATUS_HAS_UNTRACKED [[ $VCS_STATUS_RESULT == *-async ]] && emulate zsh -c "${resp[1]#* }"
unset VCS_STATUS_COMMITS_AHEAD done
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
}
(( ! ours )) && (( #header )) && emulate -L zsh && "${header[@]}" || true return 0
} }
# Starts gitstatusd in the background. Does nothing and succeeds if gitstatusd is already running. # 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 # -D Unless this option is specified, report zero staged, unstaged and conflicted
# changes for repositories with bash.showDirtyState = false. # changes for repositories with bash.showDirtyState = false.
function gitstatus_start() { function gitstatus_start() {
emulate -L zsh emulate -L zsh || return
setopt err_return no_unset no_bg_nice setopt no_aliases no_bg_nice extended_glob typeset_silent monitor || return
local opt print -rnu2 || return
local opt OPTARG
local -i OPTIND
local -F timeout=5 local -F timeout=5
local -i max_num_staged=1 local -i async=0
local -i max_num_unstaged=1 local -a args=()
local -i max_num_conflicted=1
local -i max_num_untracked=1
local -i dirty_max_index_size=-1 local -i dirty_max_index_size=-1
local -i async
local -a extra_flags=() while getopts ":t:s:u:c:d:m:eaUWD" opt; do
while true; do
getopts "t:s:u:c:d:m:eaUWD" opt || break
case $opt in case $opt in
a) async=1;; a) async=1;;
t) timeout=$OPTARG;; +a) async=0;;
s) max_num_staged=$OPTARG;; t)
u) max_num_unstaged=$OPTARG;; if [[ $OPTARG != (|+)<->(|.<->)(|[eE](|-|+)<->) ]] || (( ${timeout::=OPTARG} <= 0 )); then
c) max_num_conflicted=$OPTARG;; print -ru2 -- "gitstatus_start: invalid -t argument: $OPTARG"
d) max_num_untracked=$OPTARG;; return 1
m) dirty_max_index_size=$OPTARG;; fi
e) extra_flags+='--recurse-untracked-dirs';; ;;
+e) extra_flags=(${(@)extra_flags:#--recurse-untracked-dirs});; s|u|c|d|m)
U) extra_flags+='--ignore-status-show-untracked-files';; if [[ $OPTARG != (|-|+)<-> ]]; then
+U) extra_flags=(${(@)extra_flags:#--ignore-status-show-untracked-files});; print -ru2 -- "gitstatus_start: invalid -$opt argument: $OPTARG"
W) extra_flags+='--ignore-bash-show-untracked-files';; return 1
+W) extra_flags=(${(@)extra_flags:#--ignore-bash-show-untracked-files});; fi
D) extra_flags+='--ignore-bash-show-dirty-state';; args+=(-$opt $OPTARG)
+D) extra_flags=(${(@)extra_flags:#--ignore-bash-show-dirty-state});; [[ $opt == m ]] && dirty_max_index_size=OPTARG
?) return 1;; ;;
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 esac
done done
(( timeout > 0 )) || { echo "invalid -t: $timeout" >&2; return 1 } if (( OPTIND != ARGC )); then
(( OPTIND == ARGC )) || { echo "usage: gitstatus_start [OPTION]... NAME" >&2; return 1 } print -ru2 -- "gitstatus_start: exactly one positional argument is required"
local name=${*[$OPTIND]} return 1
fi
local lock_file req_fifo resp_fifo log_level local name=$*[OPTIND]
local log_file=/dev/null xtrace_file=/dev/null if [[ $name != [[:IDENT:]]## ]]; then
local -i stderr_fd lock_fd req_fd resp_fd daemon_pid print -ru2 -- "gitstatus_start: invalid positional argument: $name"
local daemon_pid_var=GITSTATUS_DAEMON_PID_${name} return 1
(( $+parameters[$daemon_pid_var] )) && { fi
(( ! 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
}
function gitstatus_start_impl() { local -i lock_fd resp_fd
[[ $xtrace_file == /dev/null ]] || { local file_prefix xtrace=/dev/null daemon_log=/dev/null
exec {stderr_fd}>&2 2>>$xtrace_file 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 setopt xtrace
} fi
(( daemon_pid == -1 )) || { if (( ! _GITSTATUS_STATE_$name )); then
local os print -rn >$file_prefix.lock || return
local daemon=${GITSTATUS_DAEMON:-} zsystem flock -f lock_fd $file_prefix.lock || return
[[ -n $daemon ]] || { typeset -gi _GITSTATUS_LOCK_FD_$name=lock_fd
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 [[ $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
req_fifo=${TMPDIR:-/tmp}/gitstatus.$$.req.$EPOCHREALTIME.$RANDOM {
resp_fifo=${TMPDIR:-/tmp}/gitstatus.$$.resp.$EPOCHREALTIME.$RANDOM {
mkfifo $req_fifo $resp_fifo trap '' PIPE
local -i threads=${GITSTATUS_NUM_THREADS:-0} local os daemon=$GITSTATUS_DAEMON
(( threads > 0)) || { if [[ -z $daemon ]]; then
threads=8 os="$(uname -s)" || return
[[ -n $os ]] || { os="$(uname -s)" && [[ -n $os ]] } [[ -n $os ]] || return
case $os in case $os in
FreeBSD) (( ! $+commands[sysctl] )) || threads=$(( 2 * $(sysctl -n hw.ncpu) ));; Linux) [[ "$(uname -o)" == Android ]] && os=android;;
*) (( ! $+commands[getconf] )) || threads=$(( 2 * $(getconf _NPROCESSORS_ONLN) ));; (#i)cygwin_nt-*) os=cygwin_nt-10.0;;
esac (#i)(mingw|msys)) os=msys_nt-10.0;;
(( threads <= 32 )) || threads=32 esac
} local arch
arch="$(uname -m)" || return
[[ -n $arch ]] || return
daemon=$_gitstatus_plugin_dir/bin/gitstatusd-${os:l}-${arch:l}
fi
[[ -x $daemon ]] || return
local -a daemon_args=( if [[ $GITSTATUS_NUM_THREADS == <1-> ]]; then
--lock-fd=3 args+=(-t $GITSTATUS_NUM_THREADS)
--parent-pid=${(q)$} else
--num-threads=${(q)threads} local cpus
--max-num-staged=${(q)max_num_staged} if [[ $+commands[sysctl] == 0 || "${os:-$(uname -s)}" == Linux ]] ||
--max-num-unstaged=${(q)max_num_unstaged} ! cpus="$(sysctl -n hw.ncpu)"; then
--max-num-conflicted=${(q)max_num_conflicted} if (( ! $+commands[getconf] )) || ! cpus="$(getconf _NPROCESSORS_ONLN)"; then
--max-num-untracked=${(q)max_num_untracked} cpus=8
--dirty-max-index-size=${(q)dirty_max_index_size} fi
--log-level=${(q)log_level:-INFO} fi
$extra_flags) args+=(-t $((cpus > 16 ? 32 : cpus > 0 ? 2 * cpus : 16)))
fi
local cmd=" mkfifo -- $file_prefix.fifo || return
exec >&4 print -n 1 || return
echo \$\$ exec <$file_prefix.fifo || return
${(q)daemon} $daemon_args zf_rm -- $file_prefix.fifo || return
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 $daemon "${(@)args}"
sysopen -r -o cloexec -u resp_fd $resp_fifo if [[ $? != (0|10) && $? -le 128 && -x ${daemon}-static ]]; then
echo -nE $'0\nhello\x1f\x1e' >&$req_fd ${daemon}-static "${(@)args}" || return
} fi
} always {
kill -- -$pgid
}
} &!
(( async )) && { {
daemon_pid=-1 if zsystem flock -- $file_prefix.lock && [[ -e $file_prefix.lock ]]; then
} || { zf_rm -f -- $file_prefix.lock $file_prefix.fifo
local reply IFS='' kill -- -$pgid
read -ru $resp_fd reply fi
[[ $reply == <1-> ]] } &!
daemon_pid=reply ) || return
} 3>>$daemon_log </dev/null >/dev/null || return
function _gitstatus_process_response_${name}() { 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_} local name=${${(%):-%N}#_gitstatus_process_response_}
(( ARGC == 1 )) && { if (( ARGC == 1 )); then
_gitstatus_process_response $name 0 '' _gitstatus_process_response $name 0 ''
true else
} || {
gitstatus_stop $name gitstatus_stop $name
} fi
} }
zle -F $resp_fd _gitstatus_process_response_${name} if ! zle -F $resp_fd _gitstatus_process_response_$name; then
unfunction _gitstatus_process_response_$name
return 1
fi
read -r -d $'\x1e' -u $resp_fd -t $timeout reply function _gitstatus_cleanup_$name() {
[[ $reply == $'hello\x1f0' ]] emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
local name=${${(%):-%N}#_gitstatus_cleanup_}
function _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid}() { (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return
emulate -L zsh gitstatus_stop $name
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
} }
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 )) || { 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
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 unsetopt xtrace
exec 2>&$stderr_fd {stderr_fd}>&- exec 2>&$stderr_fd
stderr_fd=0
}
}
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'
fi fi
if [[ -s $log_file ]]; then (( err )) || return
>&2 echo -E '' gitstatus_stop $name
>&2 echo -E " The content of ${(q-)log_file} (gitstatus daemon log):" setopt prompt_percent no_prompt_subst no_prompt_bang
>&2 print -P '%F{yellow}' print -Pru2 -- '[%F{red}ERROR%f]: gitstatus failed to initialize.'
>&2 awk '{print " " $0}' <$log_file print -ru2 -- ''
>&2 print -nP '%f' 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 fi
if [[ ${GITSTATUS_LOG_LEVEL:-} == DEBUG ]]; then if [[ -s $daemon_log ]]; then
>&2 echo -E '' print -ru2 -- ''
>&2 echo -E ' Your system information:' print -ru2 -- " The content of ${(q-)daemon_log} (gitstatus daemon log):"
>&2 print -P '%F{yellow}' print -Pru2 -- '%F{yellow}'
>&2 echo -E " zsh: $ZSH_VERSION" >&2 awk '{print " " $0}' <$daemon_log
>&2 echo -E " uname -a: $(uname -a)" print -Pnru2 -- '%f'
>&2 print -P '%f' fi
>&2 echo -E ' If you need help, open an issue and attach this whole error message to it:' if [[ $GITSTATUS_LOG_LEVEL == DEBUG ]]; then
>&2 echo -E '' print -ru2 -- ''
>&2 print -P ' %F{green}https://github.com/romkatv/gitstatus/issues/new%f' 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 else
>&2 echo -E '' print -ru2 -- ''
>&2 echo -E ' Run the following command to retry with extra diagnostics:' print -ru2 -- ' Run the following command to retry with extra diagnostics:'
>&2 print -P '%F{green}' print -Pru2 -- '%F{green}'
local env="GITSTATUS_LOG_LEVEL=DEBUG" 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}" env+=" GITSTATUS_NUM_THREADS=${(q)GITSTATUS_NUM_THREADS}"
fi fi
if [[ -n ${GITSTATUS_DAEMON:-} ]]; then if [[ -n $GITSTATUS_DAEMON ]]; then
env+=" GITSTATUS_DAEMON=${(q)GITSTATUS_DAEMON}" env+=" GITSTATUS_DAEMON=${(q)GITSTATUS_DAEMON}"
fi fi
>&2 echo -nE " ${env} gitstatus_start ${(@q-)*}" print -nru2 -- " ${env} gitstatus_start ${(@q-)*}"
>&2 print -P '%f' print -Pru2 -- '%f'
>&2 echo -E '' print -ru2 -- ''
local zshrc='~/.zshrc' local zshrc=${(D)ZDOTDIR:-~}/.zshrc
[[ -n ${ZDOTDIR:-} ]] && zshrc=${(D):-$ZDOTDIR/.zshrc} print -ru2 -- " If this command produces no output, add the following parameter to $zshrc:"
>&2 echo -E " If this command produces no output, add the following parameter to $zshrc:" print -ru2 -- ''
>&2 echo -E '' print -Pru2 -- '%F{green} GITSTATUS_LOG_LEVEL=DEBUG%f'
>&2 print -P '%F{green} GITSTATUS_LOG_LEVEL=DEBUG%f' print -ru2 -- ''
>&2 echo -E '' print -ru2 -- ' With this parameter gitstatus will print additional information on error.'
>&2 echo -E ' With this parameter, gitstatus will print additional information on error.'
fi fi
return 1
} }
} }
@ -582,46 +640,57 @@ function gitstatus_start() {
# #
# Usage: gitstatus_stop NAME. # Usage: gitstatus_stop NAME.
function gitstatus_stop() { function gitstatus_stop() {
emulate -L zsh emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
setopt no_unset
(( ARGC == 1 )) || { echo "usage: gitstatus_stop NAME" >&2; return 1 }
local name=$1 if (( ARGC != 1 )); then
print -ru2 -- "gitstatus_stop: exactly one positional argument is required"
local req_fd_var=_GITSTATUS_REQ_FD_${name} return 1
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}
local cleanup_func=_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
(( daemon_pid > 0 )) && kill -- -$daemon_pid &>/dev/null
unset $req_fd_var $resp_fd_var $lock_fd_var $daemon_pid_var $client_pid_var $dirty_size_var
if (( $+functions[$cleanup_func] )); then
add-zsh-hook -d zshexit $cleanup_func
unfunction $cleanup_func
fi fi
return 0 local name=$1
if [[ $name != [[:IDENT:]]## ]]; then
print -ru2 -- "gitstatus_stop: invalid positional argument: $name"
return 1
fi
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[$process] )); then
[[ -n $resp_fd ]] && zle -F $resp_fd
unfunction -- $process
fi
[[ -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. # Usage: gitstatus_check NAME.
@ -629,10 +698,20 @@ function gitstatus_stop() {
# Returns 0 if and only if `gitstatus_start NAME` has succeeded previously. # 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. # If it returns non-zero, gitstatus_query NAME is guaranteed to return non-zero.
function gitstatus_check() { function gitstatus_check() {
emulate -L zsh emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
(( ARGC == 1 )) || { echo "usage: gitstatus_check NAME" >&2; return 1 }
local daemon_pid_var=GITSTATUS_DAEMON_PID_${1} if (( ARGC != 1 )); then
(( ${(P)daemon_pid_var:-0} > 0 )) 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[@]} (( ${#_gitstatus_opts} )) && setopt ${_gitstatus_opts[@]}