pull upstream changes from gitstatus

pull/527/head
romkatv 5 years ago
parent 7aab4d7ac5
commit b4deda53a3

@ -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
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. (( _GITSTATUS_STATE_$name == 2 )) || return
local client_pid_var=_GITSTATUS_CLIENT_PID_${name}
[[ ${(P)client_pid_var} == $$ ]]
[[ -z ${GIT_DIR:-} ]] && { 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 -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} (( _GITSTATUS_STATE_$name == 2 )) || return
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
while true; do while (( _GITSTATUS_NUM_INFLIGHT_$name )); do
_gitstatus_process_response $name $timeout $req_id _gitstatus_process_response $name $timeout '' || return
[[ $VCS_STATUS_RESULT == *-async ]] || break [[ $VCS_STATUS_RESULT == *-async ]] || break
done done
[[ $VCS_STATUS_RESULT != tout || -n $callback ]] return 0
} }
function _gitstatus_process_response() { function _gitstatus_clear() {
emulate -L zsh 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}
setopt err_return no_unset }
local name=$1 function _gitstatus_process_response() {
local -F timeout=$2 local name=$1 timeout req_id=$3 buf
local req_id=$3 local -i resp_fd=_GITSTATUS_RESP_FD_$name
local resp_fd_var=_GITSTATUS_RESP_FD_${name} local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_$name
local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name}
(( $2 >= 0 )) && timeout=-t$2 && [[ -t $resp_fd ]]
typeset -g VCS_STATUS_RESULT sysread $timeout -i $resp_fd 'buf[$#buf+1]' || {
(( timeout >= 0 )) && local -a t=(-t $timeout) || local -a t=() if (( $? == 4 )); then
local -a resp if [[ -n $req_id ]]; then
local IFS=$'\x1f' typeset -g VCS_STATUS_RESULT=tout
read -rd $'\x1e' -u ${(P)resp_fd_var} $t -A resp || { _gitstatus_clear
VCS_STATUS_RESULT=tout fi
return 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]}}") 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
print -rnu2 || return
local opt 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 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
}
function gitstatus_start_impl() { local name=$*[OPTIND]
[[ $xtrace_file == /dev/null ]] || { if [[ $name != [[:IDENT:]]## ]]; then
exec {stderr_fd}>&2 2>>$xtrace_file 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 setopt xtrace
} fi
(( 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
}
local -a daemon_args=( if (( ! _GITSTATUS_STATE_$name )); then
--lock-fd=3 print -rn >$file_prefix.lock || return
--parent-pid=${(q)$} zsystem flock -f lock_fd $file_prefix.lock || return
--num-threads=${(q)threads} typeset -gi _GITSTATUS_LOCK_FD_$name=lock_fd
--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} [[ $sysparams[procsubstpid] == <1-> ]] || return
--dirty-max-index-size=${(q)dirty_max_index_size} typeset -gi GITSTATUS_DAEMON_PID_$name="sysparams[procsubstpid]"
--log-level=${(q)log_level:-INFO} sysopen -r -o cloexec -u resp_fd -- $1 || return
$extra_flags) typeset -gi _GITSTATUS_RESP_FD_$name=resp_fd
} <(
local cmd=" exec 2>&3 3>&- || return
exec >&4 local pgid=$sysparams[pid]
echo \$\$ [[ $pgid == <1-> ]] || return
${(q)daemon} $daemon_args
if [[ \$? != (0|10) && \$? -le 128 && -f ${(q)daemon}-static ]]; then {
${(q)daemon}-static $daemon_args {
fi trap '' PIPE
echo -nE $'bye\x1f0\x1e'"
# Try to use the same executable as the current zsh. Some people like having an ancient local os daemon=$GITSTATUS_DAEMON
# `zsh` in their PATH while using a newer version. zsh 5.0.2 hangs when enabling `monitor`. if [[ -z $daemon ]]; then
# os="$(uname -s)" || return
# zsh -mc '' &! # hangs when using zsh 5.0.2 [[ -n $os ]] || return
local zsh=${${:-/proc/self/exe}:A} case $os in
[[ -x $zsh ]] || zsh=zsh Linux) [[ "$(uname -o)" == Android ]] && os=android;;
cmd="cd /; read; unsetopt bg_nice; ${(q)zsh} -dfxc ${(q)cmd} &!; rm -f ${(q)req_fifo} ${(q)resp_fifo} ${(q)lock_file}" (#i)cygwin_nt-*) os=cygwin_nt-10.0;;
# We use `zsh -c` instead of plain {} or () to work around bugs in zplug (it hangs on (#i)(mingw|msys)) os=msys_nt-10.0;;
# startup). Double fork is to daemonize. esac
$zsh -dfmxc $cmd <$req_fifo >$log_file 2>&1 3<$lock_file 4>$resp_fifo &! local arch
arch="$(uname -m)" || return
sysopen -w -o cloexec,sync -u req_fd $req_fifo [[ -n $arch ]] || return
sysopen -r -o cloexec -u resp_fd $resp_fifo daemon=$_gitstatus_plugin_dir/bin/gitstatusd-${os:l}-${arch:l}
echo -nE $'0\nhello\x1f\x1e' >&$req_fd fi
} [[ -x $daemon ]] || return
(( async )) && { if [[ $GITSTATUS_NUM_THREADS == <1-> ]]; then
daemon_pid=-1 args+=(-t $GITSTATUS_NUM_THREADS)
} || { else
local reply IFS='' local cpus
read -ru $resp_fd reply if [[ $+commands[sysctl] == 0 || "${os:-$(uname -s)}" == Linux ]] ||
[[ $reply == <1-> ]] ! cpus="$(sysctl -n hw.ncpu)"; then
daemon_pid=reply if (( ! $+commands[getconf] )) || ! cpus="$(getconf _NPROCESSORS_ONLN)"; then
cpus=8
function _gitstatus_process_response_${name}() { 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 >/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_} 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
read -r -d $'\x1e' -u $resp_fd -t $timeout reply return 1
[[ $reply == $'hello\x1f0' ]] fi
function _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid}() { function _gitstatus_cleanup_$name() {
emulate -L zsh emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
setopt err_return no_unset local name=${${(%):-%N}#_gitstatus_cleanup_}
local fname=${(%):-%N} (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return
local prefix=_gitstatus_cleanup_$$_${ZSH_SUBSHELL}_ gitstatus_stop $name
[[ $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
unsetopt xtrace local expected=$'hello\x1f0' actual
exec 2>&$stderr_fd {stderr_fd}>&- while (( $#actual < $#expected )); do
stderr_fd=0 [[ -t $resp_fd ]]
} sysread -s $(($#expected - $#actual)) -t $timeout -i $resp_fd actual || return
} done
[[ $actual == $expected ]] || return
gitstatus_start_impl && { zf_rm -- $file_prefix.lock || return
typeset -gi GITSTATUS_DAEMON_PID_${name}=$daemon_pid zsystem flock -u $lock_fd || return
(( ! req_fd )) || {
typeset -gi _GITSTATUS_REQ_FD_${name}=$req_fd typeset -gi _GITSTATUS_STATE_$name=2
typeset -gi _GITSTATUS_RESP_FD_${name}=$resp_fd fi
typeset -gi _GITSTATUS_LOCK_FD_${name}=$lock_fd } always {
typeset -gi _GITSTATUS_CLIENT_PID_${name}=$$ local err=$?
typeset -gi _GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name}=$dirty_max_index_size if (( stderr_fd )); then
} unsetopt xtrace
unset -f gitstatus_start_impl exec 2>&$stderr_fd
} || {
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
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:-} if (( ARGC != 1 )); then
local resp_fd=${(P)resp_fd_var:-} print -ru2 -- "gitstatus_stop: exactly one positional argument is required"
local lock_fd=${(P)lock_fd_var:-} return 1
local daemon_pid=${(P)daemon_pid_var:-0} fi
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}>&- local name=$1
(( req_fd )) && exec {req_fd}>&- if [[ $name != [[:IDENT:]]## ]]; then
(( lock_fd )) && zsystem flock -u $lock_fd print -ru2 -- "gitstatus_stop: invalid positional argument: $name"
(( daemon_pid > 0 )) && kill -- -$daemon_pid &>/dev/null 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 if (( $+functions[$process] )); then
add-zsh-hook -d zshexit $cleanup_func [[ -n $resp_fd ]] && zle -F $resp_fd
unfunction $cleanup_func unfunction -- $process
fi 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. # 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[@]}

Loading…
Cancel
Save