2019-04-28 18:26:36 +02:00
|
|
|
# Copyright 2019 Roman Perepelitsa.
|
|
|
|
#
|
|
|
|
# This file is part of GitStatus. It provides ZSH bindings.
|
|
|
|
#
|
|
|
|
# GitStatus is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# GitStatus is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with GitStatus. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Example: Start gitstatusd, send it a request, wait for response and print it.
|
|
|
|
#
|
2019-10-02 15:30:10 +02:00
|
|
|
# source ~/gitstatus/gitstatus.plugin.zsh
|
2019-04-28 18:26:36 +02:00
|
|
|
# gitstatus_start MY
|
|
|
|
# gitstatus_query -d $PWD MY
|
2019-10-02 15:30:10 +02:00
|
|
|
# typeset -m 'VCS_STATUS_*'
|
2019-04-28 18:26:36 +02:00
|
|
|
#
|
|
|
|
# Output:
|
|
|
|
#
|
|
|
|
# VCS_STATUS_ACTION=''
|
2019-10-02 15:30:10 +02:00
|
|
|
# VCS_STATUS_COMMIT=c000eddcff0fb38df2d0137efe24d9d2d900f209
|
2019-04-28 18:26:36 +02:00
|
|
|
# VCS_STATUS_COMMITS_AHEAD=0
|
|
|
|
# VCS_STATUS_COMMITS_BEHIND=0
|
2019-10-02 15:30:10 +02:00
|
|
|
# VCS_STATUS_HAS_CONFLICTED=0
|
2019-04-28 18:26:36 +02:00
|
|
|
# VCS_STATUS_HAS_STAGED=0
|
|
|
|
# VCS_STATUS_HAS_UNSTAGED=1
|
|
|
|
# VCS_STATUS_HAS_UNTRACKED=1
|
2019-10-02 15:30:10 +02:00
|
|
|
# VCS_STATUS_INDEX_SIZE=33
|
2019-04-28 18:26:36 +02:00
|
|
|
# VCS_STATUS_LOCAL_BRANCH=master
|
2019-10-02 15:30:10 +02:00
|
|
|
# VCS_STATUS_NUM_CONFLICTED=0
|
|
|
|
# VCS_STATUS_NUM_STAGED=0
|
|
|
|
# VCS_STATUS_NUM_UNSTAGED=1
|
|
|
|
# VCS_STATUS_NUM_UNTRACKED=1
|
2019-04-28 18:26:36 +02:00
|
|
|
# VCS_STATUS_REMOTE_BRANCH=master
|
|
|
|
# VCS_STATUS_REMOTE_NAME=origin
|
|
|
|
# VCS_STATUS_REMOTE_URL=git@github.com:romkatv/powerlevel10k.git
|
|
|
|
# VCS_STATUS_RESULT=ok-sync
|
|
|
|
# VCS_STATUS_STASHES=0
|
|
|
|
# VCS_STATUS_TAG=''
|
2019-10-02 15:30:10 +02:00
|
|
|
# VCS_STATUS_WORKDIR=/home/romka/powerlevel10k
|
2019-04-28 18:26:36 +02:00
|
|
|
|
2019-07-19 23:57:37 +02:00
|
|
|
[[ -o 'interactive' ]] || 'return'
|
|
|
|
|
2019-09-20 14:20:22 +02:00
|
|
|
# Temporarily change options.
|
|
|
|
'builtin' 'local' '-a' '_gitstatus_opts'
|
|
|
|
[[ ! -o 'aliases' ]] || _gitstatus_opts+=('aliases')
|
|
|
|
[[ ! -o 'sh_glob' ]] || _gitstatus_opts+=('sh_glob')
|
|
|
|
[[ ! -o 'no_brace_expand' ]] || _gitstatus_opts+=('no_brace_expand')
|
|
|
|
'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand'
|
2019-07-19 23:57:37 +02:00
|
|
|
|
|
|
|
autoload -Uz add-zsh-hook
|
|
|
|
zmodload zsh/datetime zsh/system
|
2019-10-02 15:30:10 +02:00
|
|
|
zmodload -F zsh/files b:zf_rm
|
2019-03-07 00:04:06 +02:00
|
|
|
|
|
|
|
# Retrives status of a git repo from a directory under its working tree.
|
|
|
|
#
|
2019-04-28 18:26:36 +02:00
|
|
|
## Usage: gitstatus_query [OPTION]... NAME
|
|
|
|
#
|
2019-08-07 18:20:29 +02:00
|
|
|
# -d STR Directory to query. Must be absolute. Defaults to $GIT_DIR or the current
|
|
|
|
# directory if GIT_DIR is not set.
|
2019-03-07 00:04:06 +02:00
|
|
|
# -c STR Callback function to call once the results are available. Called only after
|
|
|
|
# gitstatus_query returns 0 with VCS_STATUS_RESULT=tout.
|
|
|
|
# -t FLOAT Timeout in seconds. Will block for at most this long. If no results are
|
|
|
|
# available by then: if -c isn't specified, will return 1; otherwise will set
|
|
|
|
# VCS_STATUS_RESULT=tout and return 0.
|
2019-07-07 19:02:35 +02:00
|
|
|
# -p Don't compute anything that requires reading Git index. If this option is used,
|
|
|
|
# the following parameters will be 0: VCS_STATUS_INDEX_SIZE,
|
2019-09-20 13:22:24 +02:00
|
|
|
# VCS_STATUS_{NUM,HAS}_{STAGED,UNSTAGED,UNTRACKED,CONFLICTED}.
|
2019-03-07 00:04:06 +02:00
|
|
|
#
|
|
|
|
# On success sets VCS_STATUS_RESULT to one of the following values:
|
|
|
|
#
|
|
|
|
# tout Timed out waiting for data; will call the user-specified callback later.
|
|
|
|
# norepo-sync The directory isn't a git repo.
|
|
|
|
# ok-sync The directory is a git repo.
|
|
|
|
#
|
2019-04-28 18:26:36 +02:00
|
|
|
# When the callback is called VCS_STATUS_RESULT is set to one of the following values:
|
2019-03-07 00:04:06 +02:00
|
|
|
#
|
|
|
|
# norepo-async The directory isn't a git repo.
|
|
|
|
# ok-async The directory is a git repo.
|
|
|
|
#
|
|
|
|
# If VCS_STATUS_RESULT is ok-sync or ok-async, additional variables are set:
|
|
|
|
#
|
2019-03-14 21:46:46 +02:00
|
|
|
# VCS_STATUS_WORKDIR Git repo working directory. Not empty.
|
2019-03-27 21:27:36 +02:00
|
|
|
# VCS_STATUS_COMMIT Commit hash that HEAD is pointing to. Either 40 hex digits or empty
|
|
|
|
# if there is no HEAD (empty repo).
|
2019-03-14 21:46:46 +02:00
|
|
|
# VCS_STATUS_LOCAL_BRANCH Local branch name or empty if not on a branch.
|
2019-03-30 20:30:52 +02:00
|
|
|
# VCS_STATUS_REMOTE_NAME The remote name, e.g. "upstream" or "origin".
|
2019-03-07 00:04:06 +02:00
|
|
|
# VCS_STATUS_REMOTE_BRANCH Upstream branch name. Can be empty.
|
|
|
|
# VCS_STATUS_REMOTE_URL Remote URL. Can be empty.
|
|
|
|
# VCS_STATUS_ACTION Repository state, A.K.A. action. Can be empty.
|
2019-06-05 18:36:55 +02:00
|
|
|
# VCS_STATUS_INDEX_SIZE The number of files in the index.
|
|
|
|
# VCS_STATUS_NUM_STAGED The number of staged changes.
|
2019-07-07 19:02:35 +02:00
|
|
|
# VCS_STATUS_NUM_CONFLICTED The number of unstaged changes.
|
2019-06-05 18:36:55 +02:00
|
|
|
# VCS_STATUS_NUM_UNSTAGED The number of unstaged changes.
|
|
|
|
# VCS_STATUS_NUM_UNTRACKED The number of untracked files.
|
2019-03-07 00:04:06 +02:00
|
|
|
# VCS_STATUS_HAS_STAGED 1 if there are staged changes, 0 otherwise.
|
2019-07-07 19:02:35 +02:00
|
|
|
# VCS_STATUS_HAS_CONFLICTED 1 if there are conflicted changes, 0 otherwise.
|
2019-03-07 00:04:06 +02:00
|
|
|
# VCS_STATUS_HAS_UNSTAGED 1 if there are unstaged changes, 0 if there aren't, -1 if unknown.
|
|
|
|
# VCS_STATUS_HAS_UNTRACKED 1 if there are untracked files, 0 if there aren't, -1 if unknown.
|
|
|
|
# VCS_STATUS_COMMITS_AHEAD Number of commits the current branch is ahead of upstream.
|
|
|
|
# Non-negative integer.
|
|
|
|
# VCS_STATUS_COMMITS_BEHIND Number of commits the current branch is behind upstream. Non-negative
|
|
|
|
# integer.
|
|
|
|
# VCS_STATUS_STASHES Number of stashes. Non-negative integer.
|
2019-04-28 18:26:36 +02:00
|
|
|
# VCS_STATUS_TAG The last tag (in lexicographical order) that points to the same
|
2019-03-14 21:46:46 +02:00
|
|
|
# commit as HEAD.
|
2019-03-07 00:04:06 +02:00
|
|
|
#
|
|
|
|
# The point of reporting -1 as unstaged and untracked is to allow the command to skip scanning
|
|
|
|
# files in large repos. See -m flag of gitstatus_start.
|
|
|
|
#
|
2019-03-17 14:32:25 +02:00
|
|
|
# gitstatus_query returns an error if gitstatus_start hasn't been called in the same shell or
|
2019-04-28 18:26:36 +02:00
|
|
|
# the call had failed.
|
2019-03-17 14:32:25 +02:00
|
|
|
#
|
2019-03-07 00:04:06 +02:00
|
|
|
# !!!!! WARNING: CONCURRENT CALLS WITH THE SAME NAME ARE NOT ALLOWED !!!!!
|
|
|
|
#
|
2019-03-17 14:32:25 +02:00
|
|
|
# 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.
|
2019-03-07 00:04:06 +02:00
|
|
|
function gitstatus_query() {
|
|
|
|
emulate -L zsh
|
|
|
|
setopt err_return no_unset
|
|
|
|
|
|
|
|
local opt
|
2019-08-07 18:20:29 +02:00
|
|
|
local dir=${GIT_DIR:-}
|
|
|
|
local callback
|
2019-03-07 00:04:06 +02:00
|
|
|
local -F timeout=-1
|
2019-07-07 19:02:35 +02:00
|
|
|
local no_diff=0
|
2019-03-07 00:04:06 +02:00
|
|
|
while true; do
|
2019-07-07 19:02:35 +02:00
|
|
|
getopts "d:c:t:p" opt || break
|
2019-03-07 00:04:06 +02:00
|
|
|
case $opt in
|
|
|
|
d) dir=$OPTARG;;
|
|
|
|
c) callback=$OPTARG;;
|
|
|
|
t) timeout=$OPTARG;;
|
2019-07-07 19:02:35 +02:00
|
|
|
p) no_diff=1;;
|
2019-03-07 00:04:06 +02:00
|
|
|
?) return 1;;
|
|
|
|
done) break;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
(( OPTIND == ARGC )) || { echo "usage: gitstatus_query [OPTION]... NAME" >&2; return 1 }
|
|
|
|
local name=${*[$OPTIND]}
|
|
|
|
|
2019-04-30 14:17:55 +02:00
|
|
|
[[ -n ${(P)${:-GITSTATUS_DAEMON_PID_${name}}:-} ]]
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-03-17 14:32:25 +02:00
|
|
|
# Verify that gitstatus_query is running in the same process that ran gitstatus_start.
|
2019-03-07 00:04:06 +02:00
|
|
|
local client_pid_var=_GITSTATUS_CLIENT_PID_${name}
|
2019-03-17 14:32:25 +02:00
|
|
|
[[ ${(P)client_pid_var} == $$ ]]
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-08-07 18:20:29 +02:00
|
|
|
[[ $dir == /* ]] || dir=${(%):-%/}/$dir
|
|
|
|
|
2019-03-17 14:32:25 +02:00
|
|
|
local req_fd_var=_GITSTATUS_REQ_FD_${name}
|
|
|
|
local -i req_fd=${(P)req_fd_var}
|
2019-03-07 00:04:06 +02:00
|
|
|
local -r req_id="$EPOCHREALTIME"
|
2019-07-07 19:02:35 +02:00
|
|
|
echo -nE $req_id' '$callback$'\x1f'$dir$'\x1f'$no_diff$'\x1e' >&$req_fd
|
2019-03-07 00:04:06 +02:00
|
|
|
|
|
|
|
while true; do
|
|
|
|
_gitstatus_process_response $name $timeout $req_id
|
|
|
|
[[ $VCS_STATUS_RESULT == *-async ]] || break
|
|
|
|
done
|
2019-03-07 11:38:08 +02:00
|
|
|
|
|
|
|
[[ $VCS_STATUS_RESULT != tout || -n $callback ]]
|
2019-03-07 00:04:06 +02:00
|
|
|
}
|
|
|
|
|
2019-03-17 14:32:25 +02:00
|
|
|
function _gitstatus_process_response() {
|
2019-03-07 00:04:06 +02:00
|
|
|
emulate -L zsh
|
|
|
|
setopt err_return no_unset
|
|
|
|
|
|
|
|
local name=$1
|
|
|
|
local -F timeout=$2
|
|
|
|
local req_id=$3
|
|
|
|
local resp_fd_var=_GITSTATUS_RESP_FD_${name}
|
2019-05-24 00:34:51 +02:00
|
|
|
local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name}
|
2019-03-07 00:04:06 +02:00
|
|
|
|
|
|
|
typeset -g VCS_STATUS_RESULT
|
|
|
|
(( timeout >= 0 )) && local -a t=(-t $timeout) || local -a t=()
|
2019-04-28 18:26:36 +02:00
|
|
|
local -a resp
|
2019-07-24 18:39:25 +02:00
|
|
|
local IFS=$'\x1f'
|
|
|
|
read -rd $'\x1e' -u ${(P)resp_fd_var} $t -A resp || {
|
2019-03-07 00:04:06 +02:00
|
|
|
VCS_STATUS_RESULT=tout
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-28 18:26:36 +02:00
|
|
|
local -a header=("${(@Q)${(z)resp[1]}}")
|
2019-03-07 00:04:06 +02:00
|
|
|
[[ ${header[1]} == $req_id ]] && local -i ours=1 || local -i ours=0
|
|
|
|
shift header
|
2019-04-28 18:26:36 +02:00
|
|
|
[[ ${resp[2]} == 1 ]] && {
|
2019-03-07 00:04:06 +02:00
|
|
|
(( ours )) && VCS_STATUS_RESULT=ok-sync || VCS_STATUS_RESULT=ok-async
|
2019-04-28 18:26:36 +02:00
|
|
|
typeset -g VCS_STATUS_WORKDIR="${resp[3]}"
|
|
|
|
typeset -g VCS_STATUS_COMMIT="${resp[4]}"
|
|
|
|
typeset -g VCS_STATUS_LOCAL_BRANCH="${resp[5]}"
|
|
|
|
typeset -g VCS_STATUS_REMOTE_BRANCH="${resp[6]}"
|
|
|
|
typeset -g VCS_STATUS_REMOTE_NAME="${resp[7]}"
|
|
|
|
typeset -g VCS_STATUS_REMOTE_URL="${resp[8]}"
|
|
|
|
typeset -g VCS_STATUS_ACTION="${resp[9]}"
|
2019-05-24 00:34:51 +02:00
|
|
|
typeset -gi VCS_STATUS_INDEX_SIZE="${resp[10]}"
|
|
|
|
typeset -gi VCS_STATUS_NUM_STAGED="${resp[11]}"
|
|
|
|
typeset -gi VCS_STATUS_NUM_UNSTAGED="${resp[12]}"
|
2019-07-07 19:02:35 +02:00
|
|
|
typeset -gi VCS_STATUS_NUM_CONFLICTED="${resp[13]}"
|
|
|
|
typeset -gi VCS_STATUS_NUM_UNTRACKED="${resp[14]}"
|
|
|
|
typeset -gi VCS_STATUS_COMMITS_AHEAD="${resp[15]}"
|
|
|
|
typeset -gi VCS_STATUS_COMMITS_BEHIND="${resp[16]}"
|
|
|
|
typeset -gi VCS_STATUS_STASHES="${resp[17]}"
|
|
|
|
typeset -g VCS_STATUS_TAG="${resp[18]}"
|
2019-05-24 00:34:51 +02:00
|
|
|
typeset -gi VCS_STATUS_HAS_STAGED=$((VCS_STATUS_NUM_STAGED > 0))
|
2019-05-27 21:32:34 +02:00
|
|
|
(( dirty_max_index_size >= 0 && VCS_STATUS_INDEX_SIZE > dirty_max_index_size )) && {
|
2019-05-24 00:34:51 +02:00
|
|
|
typeset -gi VCS_STATUS_HAS_UNSTAGED=-1
|
2019-07-07 19:02:35 +02:00
|
|
|
typeset -gi VCS_STATUS_HAS_CONFLICTED=-1
|
2019-05-24 00:34:51 +02:00
|
|
|
typeset -gi VCS_STATUS_HAS_UNTRACKED=-1
|
2019-05-27 21:32:34 +02:00
|
|
|
} || {
|
2019-05-24 00:34:51 +02:00
|
|
|
typeset -gi VCS_STATUS_HAS_UNSTAGED=$((VCS_STATUS_NUM_UNSTAGED > 0))
|
2019-07-07 19:02:35 +02:00
|
|
|
typeset -gi VCS_STATUS_HAS_CONFLICTED=$((VCS_STATUS_NUM_CONFLICTED > 0))
|
2019-05-24 00:34:51 +02:00
|
|
|
typeset -gi VCS_STATUS_HAS_UNTRACKED=$((VCS_STATUS_NUM_UNTRACKED > 0))
|
2019-05-27 21:32:34 +02:00
|
|
|
}
|
2019-03-07 00:04:06 +02:00
|
|
|
} || {
|
|
|
|
(( ours )) && VCS_STATUS_RESULT=norepo-sync || VCS_STATUS_RESULT=norepo-async
|
2019-03-14 21:46:46 +02:00
|
|
|
unset VCS_STATUS_WORKDIR
|
|
|
|
unset VCS_STATUS_COMMIT
|
2019-03-07 00:04:06 +02:00
|
|
|
unset VCS_STATUS_LOCAL_BRANCH
|
|
|
|
unset VCS_STATUS_REMOTE_BRANCH
|
2019-03-30 20:30:52 +02:00
|
|
|
unset VCS_STATUS_REMOTE_NAME
|
2019-03-07 00:04:06 +02:00
|
|
|
unset VCS_STATUS_REMOTE_URL
|
|
|
|
unset VCS_STATUS_ACTION
|
2019-05-24 00:34:51 +02:00
|
|
|
unset VCS_STATUS_INDEX_SIZE
|
|
|
|
unset VCS_STATUS_NUM_STAGED
|
|
|
|
unset VCS_STATUS_NUM_UNSTAGED
|
2019-07-07 19:02:35 +02:00
|
|
|
unset VCS_STATUS_NUM_CONFLICTED
|
2019-05-24 00:34:51 +02:00
|
|
|
unset VCS_STATUS_NUM_UNTRACKED
|
2019-03-07 00:04:06 +02:00
|
|
|
unset VCS_STATUS_HAS_STAGED
|
|
|
|
unset VCS_STATUS_HAS_UNSTAGED
|
2019-07-07 19:02:35 +02:00
|
|
|
unset VCS_STATUS_HAS_CONFLICTED
|
2019-03-07 00:04:06 +02:00
|
|
|
unset VCS_STATUS_HAS_UNTRACKED
|
|
|
|
unset VCS_STATUS_COMMITS_AHEAD
|
|
|
|
unset VCS_STATUS_COMMITS_BEHIND
|
|
|
|
unset VCS_STATUS_STASHES
|
2019-03-14 21:46:46 +02:00
|
|
|
unset VCS_STATUS_TAG
|
2019-03-07 00:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(( ! ours )) && (( #header )) && emulate -L zsh && "${header[@]}" || true
|
|
|
|
}
|
|
|
|
|
2019-04-28 18:26:36 +02:00
|
|
|
# Starts gitstatusd in the background. Does nothing and succeeds if gitstatusd is already running.
|
|
|
|
#
|
2019-03-07 00:04:06 +02:00
|
|
|
# Usage: gitstatus_start [OPTION]... NAME
|
|
|
|
#
|
|
|
|
# -t FLOAT Fail the self-check on initialization if not getting a response from gitstatusd for
|
|
|
|
# this this many seconds. Defaults to 5.
|
2019-05-24 00:34:51 +02:00
|
|
|
#
|
|
|
|
# -s INT Report at most this many staged changes; negative value means infinity.
|
|
|
|
# Defaults to 1.
|
|
|
|
#
|
|
|
|
# -u INT Report at most this many unstaged changes; negative value means infinity.
|
|
|
|
# Defaults to 1.
|
|
|
|
#
|
2019-07-07 19:02:35 +02:00
|
|
|
# -c INT Report at most this many conflicted changes; negative value means infinity.
|
|
|
|
# Defaults to 1.
|
|
|
|
#
|
2019-05-24 00:34:51 +02:00
|
|
|
# -d INT Report at most this many untracked files; negative value means infinity.
|
|
|
|
# Defaults to 1.
|
|
|
|
#
|
|
|
|
# -m INT If a repo has more files in its index than this, override -u and -d (but not -s)
|
|
|
|
# with zeros. Negative value means infinity. Defaults to -1.
|
2019-08-20 20:28:27 +02:00
|
|
|
#
|
|
|
|
# -e Count files within untracked directories like `git status --untracked-files`.
|
2019-03-07 00:04:06 +02:00
|
|
|
function gitstatus_start() {
|
|
|
|
emulate -L zsh
|
2019-03-17 14:32:25 +02:00
|
|
|
setopt err_return no_unset no_bg_nice
|
2019-03-07 00:04:06 +02:00
|
|
|
|
|
|
|
local opt
|
|
|
|
local -F timeout=5
|
2019-05-24 00:34:51 +02:00
|
|
|
local -i max_num_staged=1
|
|
|
|
local -i max_num_unstaged=1
|
2019-07-07 19:02:35 +02:00
|
|
|
local -i max_num_conflicted=1
|
2019-05-24 00:34:51 +02:00
|
|
|
local -i max_num_untracked=1
|
|
|
|
local -i dirty_max_index_size=-1
|
2019-08-20 20:28:27 +02:00
|
|
|
local recurse_untracked_dirs
|
2019-03-07 00:04:06 +02:00
|
|
|
while true; do
|
2019-08-20 20:28:27 +02:00
|
|
|
getopts "t:s:u:c:d:m:e" opt || break
|
2019-03-07 00:04:06 +02:00
|
|
|
case $opt in
|
|
|
|
t) timeout=$OPTARG;;
|
2019-05-24 00:34:51 +02:00
|
|
|
s) max_num_staged=$OPTARG;;
|
|
|
|
u) max_num_unstaged=$OPTARG;;
|
2019-07-07 19:02:35 +02:00
|
|
|
c) max_num_conflicted=$OPTARG;;
|
2019-05-24 00:34:51 +02:00
|
|
|
d) max_num_untracked=$OPTARG;;
|
|
|
|
m) dirty_max_index_size=$OPTARG;;
|
2019-08-20 20:28:27 +02:00
|
|
|
e) recurse_untracked_dirs='--recurse-untracked-dirs';;
|
|
|
|
+e) recurse_untracked_dirs=;;
|
2019-03-07 00:04:06 +02:00
|
|
|
?) return 1;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
(( timeout > 0 )) || { echo "invalid -t: $timeout" >&2; return 1 }
|
|
|
|
(( OPTIND == ARGC )) || { echo "usage: gitstatus_start [OPTION]... NAME" >&2; return 1 }
|
|
|
|
local name=${*[$OPTIND]}
|
|
|
|
|
2019-04-30 14:17:55 +02:00
|
|
|
[[ -z ${(P)${:-GITSTATUS_DAEMON_PID_${name}}:-} ]] || return 0
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-05-03 13:29:45 +02:00
|
|
|
local dir && dir=${${(%):-%x}:A:h}
|
2019-05-03 13:23:34 +02:00
|
|
|
local xtrace_file lock_file req_fifo resp_fifo log_file
|
|
|
|
local -i stderr_fd=-1 lock_fd=-1 req_fd=-1 resp_fd=-1 daemon_pid=-1
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-05-03 13:23:34 +02:00
|
|
|
function gitstatus_start_impl() {
|
2019-07-04 14:07:42 +02:00
|
|
|
local log_level=${GITSTATUS_LOG_LEVEL:-}
|
|
|
|
[[ -n $log_level || ${GITSTATUS_ENABLE_LOGGING:-0} != 1 ]] || log_level=INFO
|
|
|
|
|
|
|
|
[[ -z $log_level ]] || {
|
2019-10-02 15:30:10 +02:00
|
|
|
xtrace_file=${TMPDIR:-/tmp}/gitstatus.$$.xtrace.$EPOCHREALTIME.$RANDOM
|
2019-05-03 13:23:34 +02:00
|
|
|
typeset -g GITSTATUS_XTRACE_${name}=$xtrace_file
|
|
|
|
exec {stderr_fd}>&2 2>$xtrace_file
|
|
|
|
setopt xtrace
|
|
|
|
}
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-10-02 19:51:42 +02:00
|
|
|
local daemon=${GITSTATUS_DAEMON:-} os
|
2019-10-02 15:30:10 +02:00
|
|
|
[[ -n $daemon ]] || {
|
2019-10-02 19:51:42 +02:00
|
|
|
local arch
|
2019-10-02 15:30:10 +02:00
|
|
|
os="$(uname -s)"
|
|
|
|
[[ -n $os ]]
|
|
|
|
[[ $os != Linux || "$(uname -o)" != Android ]] || os=Android
|
|
|
|
arch="$(uname -m)"
|
|
|
|
[[ -n $arch ]]
|
|
|
|
daemon=$dir/bin/gitstatusd-${os:l}-${arch:l}
|
|
|
|
}
|
2019-09-05 22:38:36 +02:00
|
|
|
[[ -x $daemon ]]
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-10-02 15:30:10 +02:00
|
|
|
lock_file=${TMPDIR:-/tmp}/gitstatus.$$.lock.$EPOCHREALTIME.$RANDOM
|
|
|
|
echo -n >$lock_file
|
2019-03-17 14:32:25 +02:00
|
|
|
zsystem flock -f lock_fd $lock_file
|
|
|
|
|
2019-10-02 15:30:10 +02:00
|
|
|
req_fifo=${TMPDIR:-/tmp}/gitstatus.$$.req.$EPOCHREALTIME.$RANDOM
|
|
|
|
resp_fifo=${TMPDIR:-/tmp}/gitstatus.$$.resp.$EPOCHREALTIME.$RANDOM
|
|
|
|
mkfifo $req_fifo $resp_fifo
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-07-04 14:07:42 +02:00
|
|
|
[[ -n $log_level ]] &&
|
2019-10-02 15:30:10 +02:00
|
|
|
log_file=${TMPDIR:-/tmp}/gitstatus.$$.daemon-log.$EPOCHREALTIME.$RANDOM ||
|
2019-05-01 22:42:02 +02:00
|
|
|
log_file=/dev/null
|
|
|
|
typeset -g GITSTATUS_DAEMON_LOG_${name}=$log_file
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-03-11 14:19:29 +02:00
|
|
|
local -i threads=${GITSTATUS_NUM_THREADS:-0}
|
|
|
|
(( threads > 0)) || {
|
2019-06-06 10:53:41 +02:00
|
|
|
threads=8
|
2019-10-02 19:51:42 +02:00
|
|
|
[[ -n $os ]] || { os="$(uname -s)" && [[ -n $os ]] }
|
2019-03-11 14:19:29 +02:00
|
|
|
case $os in
|
2019-07-19 23:57:37 +02:00
|
|
|
FreeBSD) (( ! $+commands[sysctl] )) || threads=$(( 2 * $(sysctl -n hw.ncpu) ));;
|
|
|
|
*) (( ! $+commands[getconf] )) || threads=$(( 2 * $(getconf _NPROCESSORS_ONLN) ));;
|
2019-03-11 14:19:29 +02:00
|
|
|
esac
|
2019-04-11 09:00:58 +02:00
|
|
|
(( threads <= 32 )) || threads=32
|
2019-03-11 14:19:29 +02:00
|
|
|
}
|
|
|
|
|
2019-06-10 13:16:05 +02:00
|
|
|
local -a daemon_args=(
|
|
|
|
--lock-fd=3
|
|
|
|
--parent-pid=${(q)$}
|
|
|
|
--num-threads=${(q)threads}
|
|
|
|
--max-num-staged=${(q)max_num_staged}
|
|
|
|
--max-num-unstaged=${(q)max_num_unstaged}
|
2019-07-07 19:02:35 +02:00
|
|
|
--max-num-conflicted=${(q)max_num_conflicted}
|
2019-06-10 13:16:05 +02:00
|
|
|
--max-num-untracked=${(q)max_num_untracked}
|
2019-07-04 14:07:42 +02:00
|
|
|
--dirty-max-index-size=${(q)dirty_max_index_size}
|
2019-08-20 21:18:57 +02:00
|
|
|
--log-level=${(q)log_level:-INFO}
|
2019-08-20 20:28:27 +02:00
|
|
|
$recurse_untracked_dirs)
|
2019-06-10 13:16:05 +02:00
|
|
|
|
2019-07-24 10:31:16 +02:00
|
|
|
local cmd="
|
|
|
|
echo \$\$
|
2019-06-10 13:16:05 +02:00
|
|
|
${(q)daemon} $daemon_args
|
2019-10-02 15:30:10 +02:00
|
|
|
if [[ \$? != (0|10) && \$? -le 128 && -f ${(q)daemon}-static ]]; then
|
2019-06-10 13:16:05 +02:00
|
|
|
${(q)daemon}-static $daemon_args
|
|
|
|
fi
|
2019-07-24 10:31:16 +02:00
|
|
|
echo -nE $'bye\x1f0\x1e'"
|
2019-07-24 12:26:04 +02:00
|
|
|
local setsid=${commands[setsid]:-/usr/local/opt/util-linux/bin/setsid}
|
2019-09-05 22:38:36 +02:00
|
|
|
[[ -x $setsid ]] && setsid=${(q)setsid} || setsid=
|
2019-08-06 13:05:17 +02:00
|
|
|
cmd="cd /; $setsid zsh -dfxc ${(q)cmd} &!"
|
2019-09-20 13:22:24 +02:00
|
|
|
# We use `zsh -c` instead of plain {} or () to work around bugs in zplug (it hangs on startup).
|
|
|
|
# Double fork is to daemonize, and so is `setsid`. Note that on macOS `setsid` has to
|
|
|
|
# be installed manually by running `brew install util-linux`.
|
2019-07-24 12:11:11 +02:00
|
|
|
zsh -dfmxc $cmd <$req_fifo >$resp_fifo 2>$log_file 3<$lock_file &!
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-06-18 23:09:10 +02:00
|
|
|
sysopen -w -o cloexec,sync -u req_fd $req_fifo
|
|
|
|
sysopen -r -o cloexec -u resp_fd $resp_fifo
|
|
|
|
|
2019-07-24 10:31:16 +02:00
|
|
|
read -u $resp_fd daemon_pid
|
|
|
|
|
2019-10-02 15:30:10 +02:00
|
|
|
zf_rm -f $req_fifo $resp_fifo $lock_file
|
2019-06-18 23:09:10 +02:00
|
|
|
|
|
|
|
function _gitstatus_process_response_${name}() {
|
2019-08-07 08:44:18 +02:00
|
|
|
local name=${${(%):-%N}#_gitstatus_process_response_}
|
|
|
|
(( ARGC == 1 )) && {
|
|
|
|
_gitstatus_process_response $name 0 ''
|
|
|
|
true
|
|
|
|
} || {
|
|
|
|
gitstatus_stop $name
|
|
|
|
}
|
2019-06-18 23:09:10 +02:00
|
|
|
}
|
|
|
|
zle -F $resp_fd _gitstatus_process_response_${name}
|
2019-03-07 00:04:06 +02:00
|
|
|
|
2019-07-24 18:39:25 +02:00
|
|
|
local reply IFS=''
|
2019-03-07 00:04:06 +02:00
|
|
|
echo -nE $'hello\x1f\x1e' >&$req_fd
|
2019-07-24 18:39:25 +02:00
|
|
|
read -r -d $'\x1e' -u $resp_fd -t $timeout reply
|
2019-03-07 00:04:06 +02:00
|
|
|
[[ $reply == $'hello\x1f0' ]]
|
2019-03-15 20:14:39 +02:00
|
|
|
|
2019-08-06 09:57:33 +02:00
|
|
|
function _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid}() {
|
2019-03-15 20:14:39 +02:00
|
|
|
emulate -L zsh
|
2019-03-17 14:32:25 +02:00
|
|
|
setopt err_return no_unset
|
2019-05-06 16:35:35 +02:00
|
|
|
local fname=${(%):-%N}
|
2019-08-06 09:57:33 +02:00
|
|
|
local prefix=_gitstatus_cleanup_$$_${ZSH_SUBSHELL}_
|
2019-05-06 16:35:35 +02:00
|
|
|
[[ $fname == ${prefix}* ]] || return 0
|
|
|
|
local -i daemon_pid=${fname#$prefix}
|
|
|
|
kill -- -$daemon_pid &>/dev/null || true
|
2019-03-15 20:14:39 +02:00
|
|
|
}
|
2019-08-06 09:57:33 +02:00
|
|
|
add-zsh-hook zshexit _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid}
|
2019-05-03 13:23:34 +02:00
|
|
|
|
|
|
|
[[ $stderr_fd == -1 ]] || {
|
|
|
|
unsetopt xtrace
|
|
|
|
exec 2>&$stderr_fd {stderr_fd}>&-
|
|
|
|
stderr_fd=-1
|
|
|
|
}
|
2019-03-07 00:04:06 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 18:26:36 +02:00
|
|
|
gitstatus_start_impl && {
|
2019-05-04 11:41:45 +02:00
|
|
|
typeset -gi GITSTATUS_DAEMON_PID_${name}=$daemon_pid
|
|
|
|
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}=$$
|
2019-05-24 00:34:51 +02:00
|
|
|
typeset -gi _GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name}=$dirty_max_index_size
|
2019-04-28 18:26:36 +02:00
|
|
|
unset -f gitstatus_start_impl
|
2019-03-07 00:04:06 +02:00
|
|
|
} || {
|
2019-05-03 13:23:34 +02:00
|
|
|
unsetopt err_return
|
2019-08-06 09:57:33 +02:00
|
|
|
add-zsh-hook -d zshexit _gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid}
|
2019-05-03 13:23:34 +02:00
|
|
|
[[ $resp_fd -ge 0 ]] && { zle -F $resp_fd; exec {resp_fd}>&- }
|
2019-08-07 08:44:18 +02:00
|
|
|
[[ $req_fd -ge 0 ]] && exec {req_fd}>&-
|
|
|
|
[[ $lock_fd -ge 0 ]] && zsystem flock -u $lock_fd
|
|
|
|
[[ $stderr_fd -ge 0 ]] && { exec 2>&$stderr_fd {stderr_fd}>&- }
|
|
|
|
[[ $daemon_pid -gt 0 ]] && kill -- -$daemon_pid &>/dev/null
|
|
|
|
|
2019-07-19 23:57:37 +02:00
|
|
|
rm -f $lock_file $req_fifo $resp_fifo
|
2019-04-28 18:26:36 +02:00
|
|
|
unset -f gitstatus_start_impl
|
2019-05-03 13:23:34 +02:00
|
|
|
|
|
|
|
>&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
|
|
|
|
if [[ -s $log_file ]]; then
|
|
|
|
>&2 echo -E ''
|
|
|
|
>&2 echo -E " The content of ${(q-)log_file} (gitstatus daemon log):"
|
|
|
|
>&2 print -P '%F{yellow}'
|
|
|
|
>&2 awk '{print " " $0}' <$log_file
|
|
|
|
>&2 print -nP '%f'
|
|
|
|
fi
|
2019-07-04 14:07:42 +02:00
|
|
|
if [[ -n ${GITSTATUS_LOG_LEVEL:-} || ${GITSTATUS_ENABLE_LOGGING:-0} == 1 ]]; then
|
2019-05-03 13:23:34 +02:00
|
|
|
>&2 echo -E ''
|
|
|
|
>&2 echo -E ' Your system information:'
|
|
|
|
>&2 print -P '%F{yellow}'
|
|
|
|
>&2 echo -E " zsh: $ZSH_VERSION"
|
|
|
|
>&2 echo -E " uname -a: $(uname -a)"
|
|
|
|
>&2 print -P '%f'
|
|
|
|
>&2 echo -E ' If you need help, open an issue and attach this whole error message to it:'
|
|
|
|
>&2 echo -E ''
|
|
|
|
>&2 print -P ' %F{green}https://github.com/romkatv/gitstatus/issues/new%f'
|
|
|
|
else
|
|
|
|
>&2 echo -E ''
|
|
|
|
>&2 echo -E ' Run the following command to retry with extra diagnostics:'
|
|
|
|
>&2 print -P '%F{green}'
|
2019-07-07 19:02:35 +02:00
|
|
|
>&2 echo -E " GITSTATUS_LOG_LEVEL=DEBUG gitstatus_start ${(@q-)*}"
|
2019-05-03 13:23:34 +02:00
|
|
|
>&2 print -nP '%f'
|
|
|
|
fi
|
|
|
|
|
2019-03-07 00:04:06 +02:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 11:41:45 +02:00
|
|
|
# Stops gitstatusd if it's running.
|
|
|
|
#
|
|
|
|
# Usage: gitstatus_stop NAME.
|
|
|
|
function gitstatus_stop() {
|
|
|
|
emulate -L zsh
|
|
|
|
setopt no_unset
|
|
|
|
(( ARGC == 1 )) || { echo "usage: gitstatus_stop NAME" >&2; return 1 }
|
|
|
|
|
|
|
|
local name=$1
|
|
|
|
|
|
|
|
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}
|
2019-08-07 08:44:18 +02:00
|
|
|
local dirty_size_var=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_${name}
|
2019-05-04 11:41:45 +02:00
|
|
|
|
|
|
|
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:-}
|
|
|
|
|
2019-08-06 09:57:33 +02:00
|
|
|
local cleanup_func=_gitstatus_cleanup_$$_${ZSH_SUBSHELL}_${daemon_pid}
|
2019-05-04 11:41:45 +02:00
|
|
|
|
|
|
|
[[ -n $resp_fd ]] && { zle -F $resp_fd; exec {resp_fd}>&- }
|
2019-08-07 08:44:18 +02:00
|
|
|
[[ -n $req_fd ]] && exec {req_fd}>&-
|
2019-05-04 11:41:45 +02:00
|
|
|
[[ -n $lock_fd ]] && zsystem flock -u $lock_fd
|
2019-08-07 08:44:18 +02:00
|
|
|
[[ -n $daemon_pid ]] && kill -- -$daemon_pid &>/dev/null
|
2019-05-04 11:41:45 +02:00
|
|
|
|
2019-08-07 08:44:18 +02:00
|
|
|
unset $req_fd_var $resp_fd_var $lock_fd_var $daemon_pid_var $client_pid_var $dirty_size_var
|
2019-05-04 11:41:45 +02:00
|
|
|
|
|
|
|
if (( $+functions[$cleanup_func] )); then
|
|
|
|
add-zsh-hook -d zshexit $cleanup_func
|
|
|
|
unfunction $cleanup_func
|
|
|
|
fi
|
2019-05-06 16:35:35 +02:00
|
|
|
|
|
|
|
return 0
|
2019-05-04 11:41:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 00:04:06 +02:00
|
|
|
# Usage: gitstatus_check NAME.
|
|
|
|
#
|
|
|
|
# Returns 0 if and only if `gitstatus_start NAME` has succeeded previously.
|
|
|
|
# If it returns non-zero, gitstatus_query NAME is guaranteed to return non-zero.
|
|
|
|
function gitstatus_check() {
|
2019-05-04 11:41:45 +02:00
|
|
|
emulate -L zsh
|
|
|
|
(( ARGC == 1 )) || { echo "usage: gitstatus_check NAME" >&2; return 1 }
|
|
|
|
[[ -n ${(P)${:-GITSTATUS_DAEMON_PID_${1}}} ]]
|
2019-03-07 00:04:06 +02:00
|
|
|
}
|
2019-07-19 23:57:37 +02:00
|
|
|
|
2019-10-03 14:51:01 +02:00
|
|
|
(( ${#_gitstatus_opts} )) && setopt ${_gitstatus_opts[@]}
|
2019-09-20 14:20:22 +02:00
|
|
|
'builtin' 'unset' '_gitstatus_opts'
|