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
+p) no_diff=0;;
p) no_diff=1;;
d) dir=$OPTARG;; d) dir=$OPTARG;;
c) callback=$OPTARG;; c) callback=$OPTARG;;
t) timeout=$OPTARG;; t)
p) no_diff=1;; if [[ $OPTARG != (|+)<->(|.<->)(|[eE](|-|+)<->) ]]; then
?) return 1;; print -ru2 -- "gitstatus_query: invalid -t argument: $OPTARG"
done) break;; 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 -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
local req_fd_var=_GITSTATUS_REQ_FD_${name} (( ++_GITSTATUS_NUM_INFLIGHT_$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
if (( timeout == 0 )); then
typeset -g VCS_STATUS_RESULT=tout
_gitstatus_clear
else
while true; do while true; do
_gitstatus_process_response $name $timeout $req_id _gitstatus_process_response $name $timeout $req_id || return
[[ $VCS_STATUS_RESULT == *-async ]] || break [[ $VCS_STATUS_RESULT == *-async ]] || break
done done
fi
[[ $VCS_STATUS_RESULT != tout || -n $callback ]] [[ $VCS_STATUS_RESULT != tout || -n $callback ]]
} }
function _gitstatus_process_response() { function gitstatus_process_results() {
emulate -L zsh emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
setopt err_return no_unset 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
local name=$1 if (( OPTIND != ARGC )); then
local -F timeout=$2 print -ru2 -- "gitstatus_process_results: exactly one positional argument is required"
local req_id=$3 return 1
local resp_fd_var=_GITSTATUS_RESP_FD_${name} fi
local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name}
local name=$*[OPTIND]
typeset -g VCS_STATUS_RESULT if [[ $name != [[:IDENT:]]## ]]; then
(( timeout >= 0 )) && local -a t=(-t $timeout) || local -a t=() print -ru2 -- "gitstatus_process_results: invalid positional argument: $name"
local -a resp return 1
local IFS=$'\x1f' fi
read -rd $'\x1e' -u ${(P)resp_fd_var} $t -A resp || {
VCS_STATUS_RESULT=tout (( _GITSTATUS_STATE_$name == 2 )) || return
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() {
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]}}") 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 \
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)) typeset -gi VCS_STATUS_HAS_STAGED=$((VCS_STATUS_NUM_STAGED > 0))
(( dirty_max_index_size >= 0 && VCS_STATUS_INDEX_SIZE > dirty_max_index_size )) && { if (( dirty_max_index_size >= 0 && VCS_STATUS_INDEX_SIZE > dirty_max_index_size )); then
typeset -gi VCS_STATUS_HAS_UNSTAGED=-1 typeset -gi \
typeset -gi VCS_STATUS_HAS_CONFLICTED=-1 VCS_STATUS_HAS_UNSTAGED=-1 \
typeset -gi VCS_STATUS_HAS_UNTRACKED=-1 VCS_STATUS_HAS_CONFLICTED=-1 \
} || { VCS_STATUS_HAS_UNTRACKED=-1
typeset -gi VCS_STATUS_HAS_UNSTAGED=$((VCS_STATUS_NUM_UNSTAGED > 0)) else
typeset -gi VCS_STATUS_HAS_CONFLICTED=$((VCS_STATUS_NUM_CONFLICTED > 0)) typeset -gi \
typeset -gi VCS_STATUS_HAS_UNTRACKED=$((VCS_STATUS_NUM_UNTRACKED > 0)) 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))
(( ours )) && VCS_STATUS_RESULT=norepo-sync || VCS_STATUS_RESULT=norepo-async fi
unset VCS_STATUS_WORKDIR else
unset VCS_STATUS_COMMIT if [[ $resp[1] == $req_id' '* ]]; then
unset VCS_STATUS_LOCAL_BRANCH typeset -g VCS_STATUS_RESULT=norepo-sync
unset VCS_STATUS_REMOTE_BRANCH else
unset VCS_STATUS_REMOTE_NAME typeset -g VCS_STATUS_RESULT=norepo-async
unset VCS_STATUS_REMOTE_URL fi
unset VCS_STATUS_ACTION _gitstatus_clear
unset VCS_STATUS_INDEX_SIZE fi
unset VCS_STATUS_NUM_STAGED (( --_GITSTATUS_NUM_INFLIGHT_$name ))
unset VCS_STATUS_NUM_UNSTAGED [[ $VCS_STATUS_RESULT == *-async ]] && emulate zsh -c "${resp[1]#* }"
unset VCS_STATUS_NUM_CONFLICTED done
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
}
(( ! 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 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 )) || { 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 ]] [[ $sysparams[procsubstpid] == <1-> ]] || return
typeset -gi GITSTATUS_DAEMON_PID_$name="sysparams[procsubstpid]"
sysopen -r -o cloexec -u resp_fd -- $1 || return
typeset -gi _GITSTATUS_RESP_FD_$name=resp_fd
} <(
exec 2>&3 3>&- || return
local pgid=$sysparams[pid]
[[ $pgid == <1-> ]] || return
{
{
trap '' PIPE
local os daemon=$GITSTATUS_DAEMON
if [[ -z $daemon ]]; then
os="$(uname -s)" || return
[[ -n $os ]] || return
case $os in
Linux) [[ "$(uname -o)" == Android ]] && os=android;;
(#i)cygwin_nt-*) os=cygwin_nt-10.0;;
(#i)(mingw|msys)) os=msys_nt-10.0;;
esac
local arch
arch="$(uname -m)" || return
[[ -n $arch ]] || return
daemon=$_gitstatus_plugin_dir/bin/gitstatusd-${os:l}-${arch:l} daemon=$_gitstatus_plugin_dir/bin/gitstatusd-${os:l}-${arch:l}
} fi
[[ -x $daemon ]] [[ -x $daemon ]] || return
lock_file=${TMPDIR:-/tmp}/gitstatus.$$.lock.$EPOCHREALTIME.$RANDOM if [[ $GITSTATUS_NUM_THREADS == <1-> ]]; then
echo -n >$lock_file args+=(-t $GITSTATUS_NUM_THREADS)
zsystem flock -f lock_fd $lock_file else
local cpus
if [[ $+commands[sysctl] == 0 || "${os:-$(uname -s)}" == Linux ]] ||
! cpus="$(sysctl -n hw.ncpu)"; then
if (( ! $+commands[getconf] )) || ! cpus="$(getconf _NPROCESSORS_ONLN)"; then
cpus=8
fi
fi
args+=(-t $((cpus > 16 ? 32 : cpus > 0 ? 2 * cpus : 16)))
fi
req_fifo=${TMPDIR:-/tmp}/gitstatus.$$.req.$EPOCHREALTIME.$RANDOM mkfifo -- $file_prefix.fifo || return
resp_fifo=${TMPDIR:-/tmp}/gitstatus.$$.resp.$EPOCHREALTIME.$RANDOM print -n 1 || return
mkfifo $req_fifo $resp_fifo exec <$file_prefix.fifo || return
zf_rm -- $file_prefix.fifo || return
local -i threads=${GITSTATUS_NUM_THREADS:-0} $daemon "${(@)args}"
(( threads > 0)) || { if [[ $? != (0|10) && $? -le 128 && -x ${daemon}-static ]]; then
threads=8 ${daemon}-static "${(@)args}" || return
[[ -n $os ]] || { os="$(uname -s)" && [[ -n $os ]] } fi
case $os in } always {
FreeBSD) (( ! $+commands[sysctl] )) || threads=$(( 2 * $(sysctl -n hw.ncpu) ));; kill -- -$pgid
*) (( ! $+commands[getconf] )) || threads=$(( 2 * $(getconf _NPROCESSORS_ONLN) ));;
esac
(( threads <= 32 )) || threads=32
} }
} &!
local -a daemon_args=( {
--lock-fd=3 if zsystem flock -- $file_prefix.lock && [[ -e $file_prefix.lock ]]; then
--parent-pid=${(q)$} zf_rm -f -- $file_prefix.lock $file_prefix.fifo
--num-threads=${(q)threads} kill -- -$pgid
--max-num-staged=${(q)max_num_staged} fi
--max-num-unstaged=${(q)max_num_unstaged} } &!
--max-num-conflicted=${(q)max_num_conflicted} ) || return
--max-num-untracked=${(q)max_num_untracked} } 3>>$daemon_log </dev/null >/dev/null || return
--dirty-max-index-size=${(q)dirty_max_index_size}
--log-level=${(q)log_level:-INFO} typeset -gi _GITSTATUS_STATE_$name=1
$extra_flags) fi
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 )) && { if (( ! async )); then
daemon_pid=-1 (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return
} || {
local reply IFS=''
read -ru $resp_fd reply
[[ $reply == <1-> ]]
daemon_pid=reply
function _gitstatus_process_response_${name}() { 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
} }
} if ! zle -F $resp_fd _gitstatus_process_response_$name; then
zle -F $resp_fd _gitstatus_process_response_${name} unfunction _gitstatus_process_response_$name
return 1
read -r -d $'\x1e' -u $resp_fd -t $timeout reply fi
[[ $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
}
add-zsh-hook zshexit _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid}
}
(( ! stderr_fd )) || { function _gitstatus_cleanup_$name() {
unsetopt xtrace emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
exec 2>&$stderr_fd {stderr_fd}>&- local name=${${(%):-%N}#_gitstatus_cleanup_}
stderr_fd=0 (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return
} gitstatus_stop $name
} }
if ! add-zsh-hook zshexit _gitstatus_cleanup_$name; then
unfunction _gitstatus_cleanup_$name
return 1
fi
gitstatus_start_impl && { print -nru $req_fd -- $'hello\x1f\x1e' || return
typeset -gi GITSTATUS_DAEMON_PID_${name}=$daemon_pid local expected=$'hello\x1f0' actual
(( ! req_fd )) || { while (( $#actual < $#expected )); do
typeset -gi _GITSTATUS_REQ_FD_${name}=$req_fd [[ -t $resp_fd ]]
typeset -gi _GITSTATUS_RESP_FD_${name}=$resp_fd sysread -s $(($#expected - $#actual)) -t $timeout -i $resp_fd actual || return
typeset -gi _GITSTATUS_LOCK_FD_${name}=$lock_fd done
typeset -gi _GITSTATUS_CLIENT_PID_${name}=$$ [[ $actual == $expected ]] || return
typeset -gi _GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name}=$dirty_max_index_size
} zf_rm -- $file_prefix.lock || return
unset -f gitstatus_start_impl zsystem flock -u $lock_fd || return
} || {
unsetopt err_return typeset -gi _GITSTATUS_STATE_$name=2
add-zsh-hook -d zshexit _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid} fi
(( $+functions[_gitstatus_process_response_${name}] )) && { } always {
zle -F $resp_fd local err=$?
unfunction _gitstatus_process_response_${name} if (( stderr_fd )); then
} unsetopt xtrace
(( resp_fd )) && exec {resp_fd}>&- exec 2>&$stderr_fd
(( req_fd )) && exec {req_fd}>&- fi
(( lock_fd )) && zsystem flock -u $lock_fd (( err )) || return
(( stderr_fd )) && { exec 2>&$stderr_fd {stderr_fd}>&- } gitstatus_stop $name
(( daemon_pid > 0 )) && kill -- -$daemon_pid &>/dev/null setopt prompt_percent no_prompt_subst no_prompt_bang
print -Pru2 -- '[%F{red}ERROR%f]: gitstatus failed to initialize.'
rm -f $lock_file $req_fifo $resp_fifo print -ru2 -- ''
unset -f gitstatus_start_impl print -ru2 -- ' Your git prompt may disappear or become slow.'
if [[ -s $xtrace ]]; then
unset GITSTATUS_DAEMON_PID_${name} print -ru2 -- ''
unset _GITSTATUS_REQ_FD_${name} print -ru2 -- " The content of ${(q-)xtrace} (gitstatus_start xtrace):"
unset _GITSTATUS_RESP_FD_${name} print -Pru2 -- '%F{yellow}'
unset _GITSTATUS_LOCK_FD_${name} >&2 awk '{print " " $0}' <$xtrace
unset _GITSTATUS_CLIENT_PID_${name} print -Pnru2 -- '%f'
unset _GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name} fi
if [[ -s $daemon_log ]]; then
>&2 print -P '[%F{red}ERROR%f]: gitstatus failed to initialize.' print -ru2 -- ''
>&2 echo -E '' print -ru2 -- " The content of ${(q-)daemon_log} (gitstatus daemon log):"
>&2 echo -E ' Your git prompt may disappear or become slow.' print -Pru2 -- '%F{yellow}'
if [[ -s $xtrace_file ]]; then >&2 awk '{print " " $0}' <$daemon_log
>&2 echo -E '' print -Pnru2 -- '%f'
>&2 echo -E " The content of ${(q-)xtrace_file} (gitstatus_start_impl xtrace):" fi
>&2 print -P '%F{yellow}' if [[ $GITSTATUS_LOG_LEVEL == DEBUG ]]; then
>&2 awk '{print " " $0}' <$xtrace_file print -ru2 -- ''
>&2 print -P '%F{red} ^ this command failed%f' print -ru2 -- ' Your system information:'
fi print -Pru2 -- '%F{yellow}'
if [[ -s $log_file ]]; then print -ru2 -- " zsh: $ZSH_VERSION"
>&2 echo -E '' print -ru2 -- " uname -a: $(uname -a)"
>&2 echo -E " The content of ${(q-)log_file} (gitstatus daemon log):" print -Pru2 -- '%f'
>&2 print -P '%F{yellow}' print -ru2 -- ' If you need help, open an issue and attach this whole error message to it:'
>&2 awk '{print " " $0}' <$log_file print -ru2 -- ''
>&2 print -nP '%f' print -Pru2 -- ' %F{green}https://github.com/romkatv/gitstatus/issues/new%f'
fi
if [[ ${GITSTATUS_LOG_LEVEL:-} == DEBUG ]]; then
>&2 echo -E ''
>&2 echo -E ' Your system information:'
>&2 print -P '%F{yellow}'
>&2 echo -E " zsh: $ZSH_VERSION"
>&2 echo -E " uname -a: $(uname -a)"
>&2 print -P '%f'
>&2 echo -E ' If you need help, open an issue and attach this whole error message to it:'
>&2 echo -E ''
>&2 print -P ' %F{green}https://github.com/romkatv/gitstatus/issues/new%f'
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:-}
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} if (( ARGC != 1 )); then
print -ru2 -- "gitstatus_stop: exactly one positional argument is required"
(( $+functions[_gitstatus_process_response_${name}] )) && { return 1
zle -F $resp_fd fi
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