my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

903 lines
33 KiB

3 years ago
  1. # Zsh bindings for gitstatus.
  2. #
  3. # ------------------------------------------------------------------
  4. #
  5. # Example: Start gitstatusd, send it a request, wait for response and print it.
  6. #
  7. # source ~/gitstatus/gitstatus.plugin.zsh
  8. # gitstatus_start MY
  9. # gitstatus_query -d $PWD MY
  10. # typeset -m 'VCS_STATUS_*'
  11. #
  12. # Output:
  13. #
  14. # VCS_STATUS_ACTION=''
  15. # VCS_STATUS_COMMIT=c000eddcff0fb38df2d0137efe24d9d2d900f209
  16. # VCS_STATUS_COMMITS_AHEAD=0
  17. # VCS_STATUS_COMMITS_BEHIND=0
  18. # VCS_STATUS_COMMIT_ENCODING=''
  19. # VCS_STATUS_COMMIT_SUMMARY='pull upstream changes from gitstatus'
  20. # VCS_STATUS_HAS_CONFLICTED=0
  21. # VCS_STATUS_HAS_STAGED=0
  22. # VCS_STATUS_HAS_UNSTAGED=1
  23. # VCS_STATUS_HAS_UNTRACKED=1
  24. # VCS_STATUS_INDEX_SIZE=33
  25. # VCS_STATUS_LOCAL_BRANCH=master
  26. # VCS_STATUS_NUM_ASSUME_UNCHANGED=0
  27. # VCS_STATUS_NUM_CONFLICTED=0
  28. # VCS_STATUS_NUM_STAGED=0
  29. # VCS_STATUS_NUM_UNSTAGED=1
  30. # VCS_STATUS_NUM_SKIP_WORKTREE=0
  31. # VCS_STATUS_NUM_STAGED_NEW=0
  32. # VCS_STATUS_NUM_STAGED_DELETED=0
  33. # VCS_STATUS_NUM_UNSTAGED_DELETED=0
  34. # VCS_STATUS_NUM_UNTRACKED=1
  35. # VCS_STATUS_PUSH_COMMITS_AHEAD=0
  36. # VCS_STATUS_PUSH_COMMITS_BEHIND=0
  37. # VCS_STATUS_PUSH_REMOTE_NAME=''
  38. # VCS_STATUS_PUSH_REMOTE_URL=''
  39. # VCS_STATUS_REMOTE_BRANCH=master
  40. # VCS_STATUS_REMOTE_NAME=origin
  41. # [email protected]:romkatv/powerlevel10k.git
  42. # VCS_STATUS_RESULT=ok-sync
  43. # VCS_STATUS_STASHES=0
  44. # VCS_STATUS_TAG=''
  45. # VCS_STATUS_WORKDIR=/home/romka/powerlevel10k
  46. [[ -o 'interactive' ]] || 'return'
  47. # Temporarily change options.
  48. 'builtin' 'local' '-a' '_gitstatus_opts'
  49. [[ ! -o 'aliases' ]] || _gitstatus_opts+=('aliases')
  50. [[ ! -o 'sh_glob' ]] || _gitstatus_opts+=('sh_glob')
  51. [[ ! -o 'no_brace_expand' ]] || _gitstatus_opts+=('no_brace_expand')
  52. 'builtin' 'setopt' 'no_aliases' 'no_sh_glob' 'brace_expand'
  53. autoload -Uz add-zsh-hook || return
  54. zmodload zsh/datetime zsh/system || return
  55. zmodload -F zsh/files b:zf_rm || return
  56. typeset -g _gitstatus_plugin_dir"${1:-}"="${${(%):-%x}:A:h}"
  57. # Retrives status of a git repo from a directory under its working tree.
  58. #
  59. ## Usage: gitstatus_query [OPTION]... NAME
  60. #
  61. # -d STR Directory to query. Defaults to the current directory. Has no effect if GIT_DIR
  62. # is set.
  63. # -c STR Callback function to call once the results are available. Called only after
  64. # gitstatus_query returns 0 with VCS_STATUS_RESULT=tout.
  65. # -t FLOAT Timeout in seconds. Negative value means infinity. Will block for at most this long.
  66. # If no results are available by then: if -c isn't specified, will return 1; otherwise
  67. # will set VCS_STATUS_RESULT=tout and return 0.
  68. # -p Don't compute anything that requires reading Git index. If this option is used,
  69. # the following parameters will be 0: VCS_STATUS_INDEX_SIZE,
  70. # VCS_STATUS_{NUM,HAS}_{STAGED,UNSTAGED,UNTRACKED,CONFLICTED}.
  71. #
  72. # On success sets VCS_STATUS_RESULT to one of the following values:
  73. #
  74. # tout Timed out waiting for data; will call the user-specified callback later.
  75. # norepo-sync The directory isn't a git repo.
  76. # ok-sync The directory is a git repo.
  77. #
  78. # When the callback is called, VCS_STATUS_RESULT is set to one of the following values:
  79. #
  80. # norepo-async The directory isn't a git repo.
  81. # ok-async The directory is a git repo.
  82. #
  83. # If VCS_STATUS_RESULT is ok-sync or ok-async, additional variables are set:
  84. #
  85. # VCS_STATUS_WORKDIR Git repo working directory. Not empty.
  86. # VCS_STATUS_COMMIT Commit hash that HEAD is pointing to. Either 40 hex digits or
  87. # empty if there is no HEAD (empty repo).
  88. # VCS_STATUS_COMMIT_ENCODING Encoding of the HEAD's commit message. Empty value means UTF-8.
  89. # VCS_STATUS_COMMIT_SUMMARY The first paragraph of the HEAD's commit message as one line.
  90. # VCS_STATUS_LOCAL_BRANCH Local branch name or empty if not on a branch.
  91. # VCS_STATUS_REMOTE_NAME The remote name, e.g. "upstream" or "origin".
  92. # VCS_STATUS_REMOTE_BRANCH Upstream branch name. Can be empty.
  93. # VCS_STATUS_REMOTE_URL Remote URL. Can be empty.
  94. # VCS_STATUS_ACTION Repository state, A.K.A. action. Can be empty.
  95. # VCS_STATUS_INDEX_SIZE The number of files in the index.
  96. # VCS_STATUS_NUM_STAGED The number of staged changes.
  97. # VCS_STATUS_NUM_CONFLICTED The number of conflicted changes.
  98. # VCS_STATUS_NUM_UNSTAGED The number of unstaged changes.
  99. # VCS_STATUS_NUM_UNTRACKED The number of untracked files.
  100. # VCS_STATUS_HAS_STAGED 1 if there are staged changes, 0 otherwise.
  101. # VCS_STATUS_HAS_CONFLICTED 1 if there are conflicted changes, 0 otherwise.
  102. # VCS_STATUS_HAS_UNSTAGED 1 if there are unstaged changes, 0 if there aren't, -1 if
  103. # unknown.
  104. # VCS_STATUS_NUM_STAGED_NEW The number of staged new files. Note that renamed files
  105. # are reported as deleted plus new.
  106. # VCS_STATUS_NUM_STAGED_DELETED The number of staged deleted files. Note that renamed files
  107. # are reported as deleted plus new.
  108. # VCS_STATUS_NUM_UNSTAGED_DELETED The number of unstaged deleted files. Note that renamed files
  109. # are reported as deleted plus new.
  110. # VCS_STATUS_HAS_UNTRACKED 1 if there are untracked files, 0 if there aren't, -1 if
  111. # unknown.
  112. # VCS_STATUS_COMMITS_AHEAD Number of commits the current branch is ahead of upstream.
  113. # Non-negative integer.
  114. # VCS_STATUS_COMMITS_BEHIND Number of commits the current branch is behind upstream.
  115. # Non-negative integer.
  116. # VCS_STATUS_STASHES Number of stashes. Non-negative integer.
  117. # VCS_STATUS_TAG The last tag (in lexicographical order) that points to the same
  118. # commit as HEAD.
  119. # VCS_STATUS_PUSH_REMOTE_NAME The push remote name, e.g. "upstream" or "origin".
  120. # VCS_STATUS_PUSH_REMOTE_URL Push remote URL. Can be empty.
  121. # VCS_STATUS_PUSH_COMMITS_AHEAD Number of commits the current branch is ahead of push remote.
  122. # Non-negative integer.
  123. # VCS_STATUS_PUSH_COMMITS_BEHIND Number of commits the current branch is behind push remote.
  124. # Non-negative integer.
  125. # VCS_STATUS_NUM_SKIP_WORKTREE The number of files in the index with skip-worktree bit set.
  126. # Non-negative integer.
  127. # VCS_STATUS_NUM_ASSUME_UNCHANGED The number of files in the index with assume-unchanged bit set.
  128. # Non-negative integer.
  129. #
  130. # The point of reporting -1 via VCS_STATUS_HAS_* is to allow the command to skip scanning files in
  131. # large repos. See -m flag of gitstatus_start.
  132. #
  133. # gitstatus_query returns an error if gitstatus_start hasn't been called in the same shell or
  134. # the call had failed.
  135. #
  136. # !!!!! WARNING: CONCURRENT CALLS WITH THE SAME NAME ARE NOT ALLOWED !!!!!
  137. #
  138. # It's illegal to call gitstatus_query if the last asynchronous call with the same NAME hasn't
  139. # completed yet. If you need to issue concurrent requests, use different NAME arguments.
  140. function gitstatus_query"${1:-}"() {
  141. emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
  142. local fsuf=${${(%):-%N}#gitstatus_query}
  143. unset VCS_STATUS_RESULT
  144. local opt dir callback OPTARG
  145. local -i no_diff OPTIND
  146. local -F timeout=-1
  147. while getopts ":d:c:t:p" opt; do
  148. case $opt in
  149. +p) no_diff=0;;
  150. p) no_diff=1;;
  151. d) dir=$OPTARG;;
  152. c) callback=$OPTARG;;
  153. t)
  154. if [[ $OPTARG != (|+|-)<->(|.<->)(|[eE](|-|+)<->) ]]; then
  155. print -ru2 -- "gitstatus_query: invalid -t argument: $OPTARG"
  156. return 1
  157. fi
  158. timeout=OPTARG
  159. ;;
  160. \?) print -ru2 -- "gitstatus_query: invalid option: $OPTARG" ; return 1;;
  161. :) print -ru2 -- "gitstatus_query: missing required argument: $OPTARG"; return 1;;
  162. *) print -ru2 -- "gitstatus_query: invalid option: $opt" ; return 1;;
  163. esac
  164. done
  165. if (( OPTIND != ARGC )); then
  166. print -ru2 -- "gitstatus_query: exactly one positional argument is required"
  167. return 1
  168. fi
  169. local name=$*[OPTIND]
  170. if [[ $name != [[:IDENT:]]## ]]; then
  171. print -ru2 -- "gitstatus_query: invalid positional argument: $name"
  172. return 1
  173. fi
  174. (( _GITSTATUS_STATE_$name == 2 )) || return
  175. if [[ -z $GIT_DIR ]]; then
  176. if [[ $dir != /* ]]; then
  177. if [[ $PWD == /* && $PWD -ef . ]]; then
  178. dir=$PWD/$dir
  179. else
  180. dir=${dir:a}
  181. fi
  182. fi
  183. else
  184. if [[ $GIT_DIR == /* ]]; then
  185. dir=:$GIT_DIR
  186. elif [[ $PWD == /* && $PWD -ef . ]]; then
  187. dir=:$PWD/$GIT_DIR
  188. else
  189. dir=:${GIT_DIR:a}
  190. fi
  191. fi
  192. if [[ $dir != (|:)/* ]]; then
  193. typeset -g VCS_STATUS_RESULT=norepo-sync
  194. _gitstatus_clear$fsuf
  195. return 0
  196. fi
  197. local -i req_fd=${(P)${:-_GITSTATUS_REQ_FD_$name}}
  198. local req_id=$EPOCHREALTIME
  199. print -rnu $req_fd -- $req_id' '$callback$'\x1f'$dir$'\x1f'$no_diff$'\x1e' || return
  200. (( ++_GITSTATUS_NUM_INFLIGHT_$name ))
  201. if (( timeout == 0 )); then
  202. typeset -g VCS_STATUS_RESULT=tout
  203. _gitstatus_clear$fsuf
  204. else
  205. while true; do
  206. _gitstatus_process_response$fsuf $name $timeout $req_id || return
  207. [[ $VCS_STATUS_RESULT == *-async ]] || break
  208. done
  209. fi
  210. [[ $VCS_STATUS_RESULT != tout || -n $callback ]]
  211. }
  212. # If the last call to gitstatus_query timed out (VCS_STATUS_RESULT=tout), wait for the callback
  213. # to be called. Otherwise do nothing.
  214. #
  215. # Usage: gitstatus_process_results [OPTION]... NAME
  216. #
  217. # -t FLOAT Timeout in seconds. Negative value means infinity. Will block for at most this long.
  218. #
  219. # Returns an error only when invoked with incorrect arguments and when gitstatusd isn't running or
  220. # broken.
  221. #
  222. # If a callback gets called, VCS_STATUS_* parameters are set as in gitstatus_query.
  223. # VCS_STATUS_RESULT is either norepo-async or ok-async.
  224. function gitstatus_process_results"${1:-}"() {
  225. emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
  226. local fsuf=${${(%):-%N}#gitstatus_process_results}
  227. local opt OPTARG
  228. local -i OPTIND
  229. local -F timeout=-1
  230. while getopts ":t:" opt; do
  231. case $opt in
  232. t)
  233. if [[ $OPTARG != (|+|-)<->(|.<->)(|[eE](|-|+)<->) ]]; then
  234. print -ru2 -- "gitstatus_process_results: invalid -t argument: $OPTARG"
  235. return 1
  236. fi
  237. timeout=OPTARG
  238. ;;
  239. \?) print -ru2 -- "gitstatus_process_results: invalid option: $OPTARG" ; return 1;;
  240. :) print -ru2 -- "gitstatus_process_results: missing required argument: $OPTARG"; return 1;;
  241. *) print -ru2 -- "gitstatus_process_results: invalid option: $opt" ; return 1;;
  242. esac
  243. done
  244. if (( OPTIND != ARGC )); then
  245. print -ru2 -- "gitstatus_process_results: exactly one positional argument is required"
  246. return 1
  247. fi
  248. local name=$*[OPTIND]
  249. if [[ $name != [[:IDENT:]]## ]]; then
  250. print -ru2 -- "gitstatus_process_results: invalid positional argument: $name"
  251. return 1
  252. fi
  253. (( _GITSTATUS_STATE_$name == 2 )) || return
  254. while (( _GITSTATUS_NUM_INFLIGHT_$name )); do
  255. _gitstatus_process_response$fsuf $name $timeout '' || return
  256. [[ $VCS_STATUS_RESULT == *-async ]] || break
  257. done
  258. return 0
  259. }
  260. function _gitstatus_clear"${1:-}"() {
  261. 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}
  262. }
  263. function _gitstatus_process_response"${1:-}"() {
  264. local name=$1 timeout req_id=$3 buf
  265. local -i resp_fd=_GITSTATUS_RESP_FD_$name
  266. local -i dirty_max_index_size=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_$name
  267. (( $2 >= 0 )) && timeout=-t$2 && [[ -t $resp_fd ]]
  268. sysread $timeout -i $resp_fd 'buf[$#buf+1]' || {
  269. if (( $? == 4 )); then
  270. if [[ -n $req_id ]]; then
  271. typeset -g VCS_STATUS_RESULT=tout
  272. _gitstatus_clear$fsuf
  273. fi
  274. return 0
  275. else
  276. gitstatus_stop$fsuf $name
  277. return 1
  278. fi
  279. }
  280. while [[ $buf != *$'\x1e' ]]; do
  281. if ! sysread -i $resp_fd 'buf[$#buf+1]'; then
  282. gitstatus_stop$fsuf $name
  283. return 1
  284. fi
  285. done
  286. local s
  287. for s in ${(ps:\x1e:)buf}; do
  288. local -a resp=("${(@ps:\x1f:)s}")
  289. if (( resp[2] )); then
  290. if [[ $resp[1] == $req_id' '* ]]; then
  291. typeset -g VCS_STATUS_RESULT=ok-sync
  292. else
  293. typeset -g VCS_STATUS_RESULT=ok-async
  294. fi
  295. for VCS_STATUS_WORKDIR \
  296. VCS_STATUS_COMMIT \
  297. VCS_STATUS_LOCAL_BRANCH \
  298. VCS_STATUS_REMOTE_BRANCH \
  299. VCS_STATUS_REMOTE_NAME \
  300. VCS_STATUS_REMOTE_URL \
  301. VCS_STATUS_ACTION \
  302. VCS_STATUS_INDEX_SIZE \
  303. VCS_STATUS_NUM_STAGED \
  304. VCS_STATUS_NUM_UNSTAGED \
  305. VCS_STATUS_NUM_CONFLICTED \
  306. VCS_STATUS_NUM_UNTRACKED \
  307. VCS_STATUS_COMMITS_AHEAD \
  308. VCS_STATUS_COMMITS_BEHIND \
  309. VCS_STATUS_STASHES \
  310. VCS_STATUS_TAG \
  311. VCS_STATUS_NUM_UNSTAGED_DELETED \
  312. VCS_STATUS_NUM_STAGED_NEW \
  313. VCS_STATUS_NUM_STAGED_DELETED \
  314. VCS_STATUS_PUSH_REMOTE_NAME \
  315. VCS_STATUS_PUSH_REMOTE_URL \
  316. VCS_STATUS_PUSH_COMMITS_AHEAD \
  317. VCS_STATUS_PUSH_COMMITS_BEHIND \
  318. VCS_STATUS_NUM_SKIP_WORKTREE \
  319. VCS_STATUS_NUM_ASSUME_UNCHANGED \
  320. VCS_STATUS_COMMIT_ENCODING \
  321. VCS_STATUS_COMMIT_SUMMARY in "${(@)resp[3,29]}"; do
  322. done
  323. 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}
  324. typeset -gi VCS_STATUS_HAS_STAGED=$((VCS_STATUS_NUM_STAGED > 0))
  325. if (( dirty_max_index_size >= 0 && VCS_STATUS_INDEX_SIZE > dirty_max_index_size )); then
  326. typeset -gi \
  327. VCS_STATUS_HAS_UNSTAGED=-1 \
  328. VCS_STATUS_HAS_CONFLICTED=-1 \
  329. VCS_STATUS_HAS_UNTRACKED=-1
  330. else
  331. typeset -gi \
  332. VCS_STATUS_HAS_UNSTAGED=$((VCS_STATUS_NUM_UNSTAGED > 0)) \
  333. VCS_STATUS_HAS_CONFLICTED=$((VCS_STATUS_NUM_CONFLICTED > 0)) \
  334. VCS_STATUS_HAS_UNTRACKED=$((VCS_STATUS_NUM_UNTRACKED > 0))
  335. fi
  336. else
  337. if [[ $resp[1] == $req_id' '* ]]; then
  338. typeset -g VCS_STATUS_RESULT=norepo-sync
  339. else
  340. typeset -g VCS_STATUS_RESULT=norepo-async
  341. fi
  342. _gitstatus_clear$fsuf
  343. fi
  344. (( --_GITSTATUS_NUM_INFLIGHT_$name ))
  345. [[ $VCS_STATUS_RESULT == *-async ]] && emulate zsh -c "${resp[1]#* }"
  346. done
  347. return 0
  348. }
  349. function _gitstatus_daemon"${1:-}"() {
  350. local -i pipe_fd
  351. exec 0<&- {pipe_fd}>&1 1>>$daemon_log 2>&1 || return
  352. local pgid=$sysparams[pid]
  353. [[ $pgid == <1-> ]] || return
  354. builtin cd -q / || return
  355. {
  356. {
  357. trap '' PIPE
  358. local uname_sm
  359. uname_sm="${${(L)$(command uname -sm)}//ı/i}" || return
  360. [[ $uname_sm == [^' ']##' '[^' ']## ]] || return
  361. local uname_s=${uname_sm% *}
  362. local uname_m=${uname_sm#* }
  363. if [[ $GITSTATUS_NUM_THREADS == <1-> ]]; then
  364. args+=(-t $GITSTATUS_NUM_THREADS)
  365. else
  366. local cpus
  367. if (( ! $+commands[sysctl] )) || [[ $uname_s == linux ]] ||
  368. ! cpus="$(command sysctl -n hw.ncpu)"; then
  369. if (( ! $+commands[getconf] )) || ! cpus="$(command getconf _NPROCESSORS_ONLN)"; then
  370. cpus=8
  371. fi
  372. fi
  373. args+=(-t $((cpus > 16 ? 32 : cpus > 0 ? 2 * cpus : 16)))
  374. fi
  375. command mkfifo -- $file_prefix.fifo || return
  376. print -rnu $pipe_fd -- ${(l:20:)pgid} || return
  377. exec <$file_prefix.fifo || return
  378. zf_rm -- $file_prefix.fifo || return
  379. local _gitstatus_zsh_daemon _gitstatus_zsh_version _gitstatus_zsh_downloaded
  380. function _gitstatus_set_daemon$fsuf() {
  381. _gitstatus_zsh_daemon="$1"
  382. _gitstatus_zsh_version="$2"
  383. _gitstatus_zsh_downloaded="$3"
  384. }
  385. local gitstatus_plugin_dir_var=_gitstatus_plugin_dir$fsuf
  386. local gitstatus_plugin_dir=${(P)gitstatus_plugin_dir_var}
  387. builtin set -- -d $gitstatus_plugin_dir -s $uname_s -m $uname_m \
  388. -p "printf '\\001' >&$pipe_fd" -e $pipe_fd -- _gitstatus_set_daemon$fsuf
  389. [[ ${GITSTATUS_AUTO_INSTALL:-1} == (|-|+)<1-> ]] || builtin set -- -n "$@"
  390. builtin source $gitstatus_plugin_dir/install || return
  391. [[ -n $_gitstatus_zsh_daemon ]] || return
  392. [[ -n $_gitstatus_zsh_version ]] || return
  393. [[ $_gitstatus_zsh_downloaded == [01] ]] || return
  394. if (( UID == EUID )); then
  395. local home=~
  396. else
  397. local user
  398. user="$(command id -un)" || return
  399. local home=${userdirs[$user]}
  400. [[ -n $home ]] || return
  401. fi
  402. if [[ -x $_gitstatus_zsh_daemon ]]; then
  403. HOME=$home $_gitstatus_zsh_daemon -G $_gitstatus_zsh_version "${(@)args}" >&$pipe_fd
  404. local -i ret=$?
  405. [[ $ret == (0|129|130|131|137|141|143|159) ]] && return ret
  406. fi
  407. (( ! _gitstatus_zsh_downloaded )) || return
  408. [[ ${GITSTATUS_AUTO_INSTALL:-1} == (|-|+)<1-> ]] || return
  409. [[ $_gitstatus_zsh_daemon == \
  410. ${GITSTATUS_CACHE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/gitstatus}/* ]] || return
  411. builtin set -- -f "$@"
  412. _gitstatus_zsh_daemon=
  413. _gitstatus_zsh_version=
  414. _gitstatus_zsh_downloaded=
  415. builtin source $gitstatus_plugin_dir/install || return
  416. [[ -n $_gitstatus_zsh_daemon ]] || return
  417. [[ -n $_gitstatus_zsh_version ]] || return
  418. [[ $_gitstatus_zsh_downloaded == 1 ]] || return
  419. HOME=$home $_gitstatus_zsh_daemon -G $_gitstatus_zsh_version "${(@)args}" >&$pipe_fd
  420. } always {
  421. local -i ret=$?
  422. zf_rm -f -- $file_prefix.lock $file_prefix.fifo
  423. kill -- -$pgid
  424. }
  425. } &!
  426. (( lock_fd == -1 )) && return
  427. {
  428. if zsystem flock -- $file_prefix.lock && command sleep 5 && [[ -e $file_prefix.lock ]]; then
  429. zf_rm -f -- $file_prefix.lock $file_prefix.fifo
  430. kill -- -$pgid
  431. fi
  432. } &!
  433. }
  434. # Starts gitstatusd in the background. Does nothing and succeeds if gitstatusd is already running.
  435. #
  436. # Usage: gitstatus_start [OPTION]... NAME
  437. #
  438. # -t FLOAT Fail the self-check on initialization if not getting a response from gitstatusd for
  439. # this this many seconds. Defaults to 5.
  440. #
  441. # -s INT Report at most this many staged changes; negative value means infinity.
  442. # Defaults to 1.
  443. #
  444. # -u INT Report at most this many unstaged changes; negative value means infinity.
  445. # Defaults to 1.
  446. #
  447. # -c INT Report at most this many conflicted changes; negative value means infinity.
  448. # Defaults to 1.
  449. #
  450. # -d INT Report at most this many untracked files; negative value means infinity.
  451. # Defaults to 1.
  452. #
  453. # -m INT Report -1 unstaged, untracked and conflicted if there are more than this many
  454. # files in the index. Negative value means infinity. Defaults to -1.
  455. #
  456. # -e Count files within untracked directories like `git status --untracked-files`.
  457. #
  458. # -U Unless this option is specified, report zero untracked files for repositories
  459. # with status.showUntrackedFiles = false.
  460. #
  461. # -W Unless this option is specified, report zero untracked files for repositories
  462. # with bash.showUntrackedFiles = false.
  463. #
  464. # -D Unless this option is specified, report zero staged, unstaged and conflicted
  465. # changes for repositories with bash.showDirtyState = false.
  466. function gitstatus_start"${1:-}"() {
  467. emulate -L zsh -o no_aliases -o no_bg_nice -o extended_glob -o typeset_silent || return
  468. print -rnu2 || return
  469. local fsuf=${${(%):-%N}#gitstatus_start}
  470. local opt OPTARG
  471. local -i OPTIND
  472. local -F timeout=5
  473. local -i async=0
  474. local -a args=()
  475. local -i dirty_max_index_size=-1
  476. while getopts ":t:s:u:c:d:m:eaUWD" opt; do
  477. case $opt in
  478. a) async=1;;
  479. +a) async=0;;
  480. t)
  481. if [[ $OPTARG != (|+)<->(|.<->)(|[eE](|-|+)<->) ]] || (( ${timeout::=OPTARG} <= 0 )); then
  482. print -ru2 -- "gitstatus_start: invalid -t argument: $OPTARG"
  483. return 1
  484. fi
  485. ;;
  486. s|u|c|d|m)
  487. if [[ $OPTARG != (|-|+)<-> ]]; then
  488. print -ru2 -- "gitstatus_start: invalid -$opt argument: $OPTARG"
  489. return 1
  490. fi
  491. args+=(-$opt $OPTARG)
  492. [[ $opt == m ]] && dirty_max_index_size=OPTARG
  493. ;;
  494. e|U|W|D) args+=-$opt;;
  495. +(e|U|W|D)) args=(${(@)args:#-$opt});;
  496. \?) print -ru2 -- "gitstatus_start: invalid option: $OPTARG" ; return 1;;
  497. :) print -ru2 -- "gitstatus_start: missing required argument: $OPTARG"; return 1;;
  498. *) print -ru2 -- "gitstatus_start: invalid option: $opt" ; return 1;;
  499. esac
  500. done
  501. if (( OPTIND != ARGC )); then
  502. print -ru2 -- "gitstatus_start: exactly one positional argument is required"
  503. return 1
  504. fi
  505. local name=$*[OPTIND]
  506. if [[ $name != [[:IDENT:]]## ]]; then
  507. print -ru2 -- "gitstatus_start: invalid positional argument: $name"
  508. return 1
  509. fi
  510. local -i lock_fd resp_fd stderr_fd
  511. local file_prefix xtrace=/dev/null daemon_log=/dev/null culprit
  512. {
  513. if (( _GITSTATUS_STATE_$name )); then
  514. (( async )) && return
  515. (( _GITSTATUS_STATE_$name == 2 )) && return
  516. lock_fd=_GITSTATUS_LOCK_FD_$name
  517. resp_fd=_GITSTATUS_RESP_FD_$name
  518. xtrace=${(P)${:-GITSTATUS_XTRACE_$name}}
  519. daemon_log=${(P)${:-GITSTATUS_DAEMON_LOG_$name}}
  520. file_prefix=${(P)${:-_GITSTATUS_FILE_PREFIX_$name}}
  521. else
  522. typeset -gi _GITSTATUS_START_COUNTER
  523. local log_level=$GITSTATUS_LOG_LEVEL
  524. local file_prefix=${${TMPDIR:-/tmp}:A}/gitstatus.$name.$EUID
  525. file_prefix+=.$sysparams[pid].$EPOCHSECONDS.$((++_GITSTATUS_START_COUNTER))
  526. (( GITSTATUS_ENABLE_LOGGING )) && : ${log_level:=INFO}
  527. if [[ -n $log_level ]]; then
  528. xtrace=$file_prefix.xtrace.log
  529. daemon_log=$file_prefix.daemon.log
  530. fi
  531. args+=(-v ${log_level:-FATAL})
  532. typeset -g GITSTATUS_XTRACE_$name=$xtrace
  533. typeset -g GITSTATUS_DAEMON_LOG_$name=$daemon_log
  534. typeset -g _GITSTATUS_FILE_PREFIX_$name=$file_prefix
  535. typeset -gi _GITSTATUS_CLIENT_PID_$name="sysparams[pid]"
  536. typeset -gi _GITSTATUS_DIRTY_MAX_INDEX_SIZE_$name=dirty_max_index_size
  537. fi
  538. () {
  539. if [[ $xtrace != /dev/null && -o no_xtrace ]]; then
  540. exec {stderr_fd}>&2 || return
  541. exec 2>>$xtrace || return
  542. setopt xtrace
  543. fi
  544. setopt monitor || return
  545. if (( ! _GITSTATUS_STATE_$name )); then
  546. if [[ -r /proc/version && "$(</proc/version)" == *Microsoft* ]]; then
  547. lock_fd=-1
  548. else
  549. print -rn >$file_prefix.lock || return
  550. zsystem flock -f lock_fd $file_prefix.lock || return
  551. [[ $lock_fd == <1-> ]] || return
  552. fi
  553. typeset -gi _GITSTATUS_LOCK_FD_$name=lock_fd
  554. if [[ $OSTYPE == cygwin* && -d /proc/self/fd ]]; then
  555. # Work around bugs in Cygwin 32-bit.
  556. #
  557. # This hangs:
  558. #
  559. # emulate -L zsh
  560. # () { exec {fd}< $1 } <(:)
  561. # =true # hangs here
  562. #
  563. # This hangs:
  564. #
  565. # sysopen -r -u fd <(:)
  566. local -i fd
  567. exec {fd}< <(_gitstatus_daemon$fsuf) || return
  568. {
  569. [[ -r /proc/self/fd/$fd ]] || return
  570. sysopen -r -o cloexec -u resp_fd /proc/self/fd/$fd || return
  571. } always {
  572. exec {fd} >&- || return
  573. }
  574. else
  575. sysopen -r -o cloexec -u resp_fd <(_gitstatus_daemon$fsuf) || return
  576. fi
  577. typeset -gi GITSTATUS_DAEMON_PID_$name="${sysparams[procsubstpid]:--1}"
  578. [[ $resp_fd == <1-> ]] || return
  579. typeset -gi _GITSTATUS_RESP_FD_$name=resp_fd
  580. typeset -gi _GITSTATUS_STATE_$name=1
  581. fi
  582. if (( ! async )); then
  583. (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return
  584. local pgid
  585. while (( $#pgid < 20 )); do
  586. [[ -t $resp_fd ]]
  587. sysread -s $((20 - $#pgid)) -t $timeout -i $resp_fd 'pgid[$#pgid+1]' || return
  588. done
  589. [[ $pgid == ' '#<1-> ]] || return
  590. typeset -gi GITSTATUS_DAEMON_PID_$name=pgid
  591. sysopen -w -o cloexec -u req_fd -- $file_prefix.fifo || return
  592. [[ $req_fd == <1-> ]] || return
  593. typeset -gi _GITSTATUS_REQ_FD_$name=req_fd
  594. print -nru $req_fd -- $'}hello\x1f\x1e' || return
  595. local expected=$'}hello\x1f0\x1e' actual
  596. if (( $+functions[p10k] )) && [[ ! -t 1 && ! -t 0 ]]; then
  597. local -F deadline='EPOCHREALTIME + 4'
  598. else
  599. local -F deadline='1'
  600. fi
  601. while true; do
  602. [[ -t $resp_fd ]]
  603. sysread -s 1 -t $timeout -i $resp_fd actual || return
  604. [[ $expected == $actual* ]] && break
  605. if [[ $actual != $'\1' ]]; then
  606. [[ -t $resp_fd ]]
  607. while sysread -t $timeout -i $resp_fd 'actual[$#actual+1]'; do
  608. [[ -t $resp_fd ]]
  609. done
  610. culprit=$actual
  611. return 1
  612. fi
  613. (( EPOCHREALTIME < deadline )) && continue
  614. if (( deadline > 0 )); then
  615. deadline=0
  616. if (( stderr_fd )); then
  617. unsetopt xtrace
  618. exec 2>&$stderr_fd {stderr_fd}>&-
  619. stderr_fd=0
  620. fi
  621. if (( $+functions[p10k] )); then
  622. p10k clear-instant-prompt || return
  623. fi
  624. if [[ $name == POWERLEVEL9K ]]; then
  625. local label=powerlevel10k
  626. else
  627. local label=gitstatus
  628. fi
  629. if [[ -t 2 ]]; then
  630. local spinner=($'\b%3F-%f' $'\b%3F\\%f' $'\b%3F|%f' $'\b%3F/%f')
  631. print -Prnu2 -- "[%3F$label%f] fetching %2Fgitstatusd%f .. "
  632. else
  633. local spinner=('.')
  634. print -rnu2 -- "[$label] fetching gitstatusd .."
  635. fi
  636. fi
  637. print -Prnu2 -- $spinner[1]
  638. spinner=($spinner[2,-1] $spinner[1])
  639. done
  640. if (( deadline == 0 )); then
  641. if [[ -t 2 ]]; then
  642. print -Pru2 -- $'\b[%2Fok%f]'
  643. else
  644. print -ru2 -- ' [ok]'
  645. fi
  646. if [[ $xtrace != /dev/null && -o no_xtrace ]]; then
  647. exec {stderr_fd}>&2 || return
  648. exec 2>>$xtrace || return
  649. setopt xtrace
  650. fi
  651. fi
  652. while (( $#actual < $#expected )); do
  653. [[ -t $resp_fd ]]
  654. sysread -s $(($#expected - $#actual)) -t $timeout -i $resp_fd 'actual[$#actual+1]' || return
  655. done
  656. [[ $actual == $expected ]] || return
  657. function _gitstatus_process_response_$name-$fsuf() {
  658. emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
  659. local pair=${${(%):-%N}#_gitstatus_process_response_}
  660. local name=${pair%%-*}
  661. local fsuf=${pair#*-}
  662. [[ $name == POWERLEVEL9K && $fsuf == _p9k_ ]] && eval $__p9k_intro_base
  663. if (( ARGC == 1 )); then
  664. _gitstatus_process_response$fsuf $name 0 ''
  665. else
  666. gitstatus_stop$fsuf $name
  667. fi
  668. }
  669. if ! zle -F $resp_fd _gitstatus_process_response_$name-$fsuf; then
  670. unfunction _gitstatus_process_response_$name-$fsuf
  671. return 1
  672. fi
  673. function _gitstatus_cleanup_$name-$fsuf() {
  674. emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
  675. local pair=${${(%):-%N}#_gitstatus_cleanup_}
  676. local name=${pair%%-*}
  677. local fsuf=${pair#*-}
  678. (( _GITSTATUS_CLIENT_PID_$name == sysparams[pid] )) || return
  679. gitstatus_stop$fsuf $name
  680. }
  681. if ! add-zsh-hook zshexit _gitstatus_cleanup_$name-$fsuf; then
  682. unfunction _gitstatus_cleanup_$name-$fsuf
  683. return 1
  684. fi
  685. if (( lock_fd != -1 )); then
  686. zf_rm -- $file_prefix.lock || return
  687. zsystem flock -u $lock_fd || return
  688. fi
  689. unset _GITSTATUS_LOCK_FD_$name
  690. typeset -gi _GITSTATUS_STATE_$name=2
  691. fi
  692. }
  693. } always {
  694. local -i err=$?
  695. (( stderr_fd )) && exec 2>&$stderr_fd {stderr_fd}>&-
  696. (( err == 0 )) && return
  697. gitstatus_stop$fsuf $name
  698. setopt prompt_percent no_prompt_subst no_prompt_bang
  699. (( $+functions[p10k] )) && p10k clear-instant-prompt
  700. print -ru2 -- ''
  701. print -Pru2 -- '[%F{red}ERROR%f]: gitstatus failed to initialize.'
  702. print -ru2 -- ''
  703. if [[ -n $culprit ]]; then
  704. print -ru2 -- $culprit
  705. return err
  706. fi
  707. if [[ -s $xtrace ]]; then
  708. print -ru2 -- ''
  709. print -Pru2 -- " Zsh log (%U${xtrace//\%/%%}%u):"
  710. print -Pru2 -- '%F{yellow}'
  711. print -lru2 -- "${(@)${(@f)$(<$xtrace)}/#/ }"
  712. print -Pnru2 -- '%f'
  713. fi
  714. if [[ -s $daemon_log ]]; then
  715. print -ru2 -- ''
  716. print -Pru2 -- " Daemon log (%U${daemon_log//\%/%%}%u):"
  717. print -Pru2 -- '%F{yellow}'
  718. print -lru2 -- "${(@)${(@f)$(<$daemon_log)}/#/ }"
  719. print -Pnru2 -- '%f'
  720. fi
  721. if [[ $GITSTATUS_LOG_LEVEL == DEBUG ]]; then
  722. print -ru2 -- ''
  723. print -ru2 -- ' System information:'
  724. print -Pru2 -- '%F{yellow}'
  725. print -ru2 -- " zsh: $ZSH_VERSION"
  726. print -ru2 -- " uname -a: $(command uname -a)"
  727. print -Pru2 -- '%f'
  728. print -ru2 -- ' If you need help, open an issue and attach this whole error message to it:'
  729. print -ru2 -- ''
  730. print -Pru2 -- ' %Uhttps://github.com/romkatv/gitstatus/issues/new%u'
  731. else
  732. print -ru2 -- ''
  733. local home=~
  734. local zshrc=${${${(q)${ZDOTDIR:-~}}/#${(q)home}/'~'}//\%/%%}/.zshrc
  735. print -Pru2 -- " Add the following parameter to %U$zshrc%u for extra diagnostics on error:"
  736. print -ru2 -- ''
  737. print -Pru2 -- ' %BGITSTATUS_LOG_LEVEL=DEBUG%b'
  738. print -ru2 -- ''
  739. print -ru2 -- ' Restart Zsh to retry gitstatus initialization:'
  740. print -ru2 -- ''
  741. print -Pru2 -- ' %F{green}%Uexec%u zsh%f'
  742. fi
  743. }
  744. }
  745. # Stops gitstatusd if it's running.
  746. #
  747. # Usage: gitstatus_stop NAME.
  748. function gitstatus_stop"${1:-}"() {
  749. emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
  750. local fsuf=${${(%):-%N}#gitstatus_stop}
  751. if (( ARGC != 1 )); then
  752. print -ru2 -- "gitstatus_stop: exactly one positional argument is required"
  753. return 1
  754. fi
  755. local name=$1
  756. if [[ $name != [[:IDENT:]]## ]]; then
  757. print -ru2 -- "gitstatus_stop: invalid positional argument: $name"
  758. return 1
  759. fi
  760. local state_var=_GITSTATUS_STATE_$name
  761. local req_fd_var=_GITSTATUS_REQ_FD_$name
  762. local resp_fd_var=_GITSTATUS_RESP_FD_$name
  763. local lock_fd_var=_GITSTATUS_LOCK_FD_$name
  764. local client_pid_var=_GITSTATUS_CLIENT_PID_$name
  765. local daemon_pid_var=GITSTATUS_DAEMON_PID_$name
  766. local inflight_var=_GITSTATUS_NUM_INFLIGHT_$name
  767. local file_prefix_var=_GITSTATUS_FILE_PREFIX_$name
  768. local dirty_max_index_size_var=_GITSTATUS_DIRTY_MAX_INDEX_SIZE_$name
  769. local req_fd=${(P)req_fd_var}
  770. local resp_fd=${(P)resp_fd_var}
  771. local lock_fd=${(P)lock_fd_var}
  772. local daemon_pid=${(P)daemon_pid_var}
  773. local file_prefix=${(P)file_prefix_var}
  774. local cleanup=_gitstatus_cleanup_$name-$fsuf
  775. local process=_gitstatus_process_response_$name-$fsuf
  776. if (( $+functions[$cleanup] )); then
  777. add-zsh-hook -d zshexit $cleanup
  778. unfunction -- $cleanup
  779. fi
  780. if (( $+functions[$process] )); then
  781. [[ -n $resp_fd ]] && zle -F $resp_fd
  782. unfunction -- $process
  783. fi
  784. [[ $daemon_pid == <1-> ]] && kill -- -$daemon_pid 2>/dev/null
  785. [[ $file_prefix == /* ]] && zf_rm -f -- $file_prefix.lock $file_prefix.fifo
  786. [[ $lock_fd == <1-> ]] && zsystem flock -u $lock_fd
  787. [[ $req_fd == <1-> ]] && exec {req_fd}>&-
  788. [[ $resp_fd == <1-> ]] && exec {resp_fd}>&-
  789. unset $state_var $req_fd_var $lock_fd_var $resp_fd_var $client_pid_var $daemon_pid_var
  790. unset $inflight_var $file_prefix_var $dirty_max_index_size_var
  791. unset VCS_STATUS_RESULT
  792. _gitstatus_clear$fsuf
  793. }
  794. # Usage: gitstatus_check NAME.
  795. #
  796. # Returns 0 if and only if `gitstatus_start NAME` has succeeded previously.
  797. # If it returns non-zero, gitstatus_query NAME is guaranteed to return non-zero.
  798. function gitstatus_check"${1:-}"() {
  799. emulate -L zsh -o no_aliases -o extended_glob -o typeset_silent
  800. local fsuf=${${(%):-%N}#gitstatus_check}
  801. if (( ARGC != 1 )); then
  802. print -ru2 -- "gitstatus_check: exactly one positional argument is required"
  803. return 1
  804. fi
  805. local name=$1
  806. if [[ $name != [[:IDENT:]]## ]]; then
  807. print -ru2 -- "gitstatus_check: invalid positional argument: $name"
  808. return 1
  809. fi
  810. (( _GITSTATUS_STATE_$name == 2 ))
  811. }
  812. (( ${#_gitstatus_opts} )) && setopt ${_gitstatus_opts[@]}
  813. 'builtin' 'unset' '_gitstatus_opts'