Skip to content

Commit 1dcba39

Browse files
committed
bash bindings: provide better diagnostic for the common errors in gitstatus_start
1 parent 603fb2c commit 1dcba39

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

gitstatus.plugin.sh

+27-10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@
3636
# -D Unless this option is specified, report zero staged, unstaged and conflicted
3737
# changes for repositories with bash.showDirtyState = false.
3838
function gitstatus_start() {
39+
if [[ "$BASH_VERSION" < 4 ]]; then
40+
>&2 printf 'gitstatus_start: need bash version >= 4.0, found %s\n' "$BASH_VERSION"
41+
>&2 printf '\n'
42+
>&2 printf 'To see the version of the current shell, type:\n'
43+
>&2 printf '\n'
44+
>&2 printf ' \033[32mecho\033[0m \033[33m"$BASH_VERSION"\033[0m\n'
45+
>&2 printf '\n'
46+
>&2 printf 'The output of `\033[32mbash\033[0m --version` may be different and is not relevant.\n'
47+
return 1
48+
fi
49+
3950
unset OPTIND
4051
local opt timeout=5 max_dirty=-1 extra_flags
4152
local max_num_staged=1 max_num_unstaged=1 max_num_conflicted=1 max_num_untracked=1
@@ -69,7 +80,7 @@ function gitstatus_start() {
6980
local gitstatus_plugin_dir="$PWD"
7081
fi
7182

72-
local tmpdir req_fifo resp_fifo
83+
local tmpdir req_fifo resp_fifo culprit
7384

7485
function gitstatus_start_impl() {
7586
local log_level="${GITSTATUS_LOG_LEVEL:-}"
@@ -137,7 +148,7 @@ function gitstatus_start() {
137148
}
138149

139150
set -- -d "$gitstatus_plugin_dir" -s "$uname_s" -m "$uname_m" \
140-
-p "printf '.\036' >&$fd_out" -- _gitstatus_set_daemon
151+
-p "printf '.\036' >&$fd_out" -e "$fd_out" -- _gitstatus_set_daemon
141152
[[ "${GITSTATUS_AUTO_INSTALL:-1}" -ne 0 ]] || set -- -n "$@"
142153
source "$gitstatus_plugin_dir"/install || return
143154
[[ -n "$_gitstatus_bash_daemon" ]] || return
@@ -166,7 +177,7 @@ function gitstatus_start() {
166177
trap - ${sig[@]}
167178
case "$ret" in
168179
0|129|130|131|137|141|143|159)
169-
echo -nE $'bye\x1f0\x1e' >&"$fd_out"
180+
echo -nE $'}bye\x1f0\x1e' >&"$fd_out"
170181
exit "$ret"
171182
;;
172183
esac
@@ -192,7 +203,7 @@ function gitstatus_start() {
192203
trap "trap - ${sig[*]}; kill $pid &>/dev/null" ${sig[@]}
193204
wait "$pid"
194205
trap - ${sig[@]}
195-
echo -nE $'bye\x1f0\x1e' >&"$fd_out"
206+
echo -nE $'}bye\x1f0\x1e' >&"$fd_out"
196207
) & disown
197208
) & disown
198209
} 0</dev/null &>"$GITSTATUS_DAEMON_LOG"
@@ -205,12 +216,15 @@ function gitstatus_start() {
205216
[[ "$GITSTATUS_DAEMON_PID" == [1-9]* ]] || return
206217

207218
local reply
208-
echo -nE $'hello\x1f\x1e' >&$_GITSTATUS_REQ_FD || return
219+
echo -nE $'}hello\x1f\x1e' >&$_GITSTATUS_REQ_FD || return
209220
local dl=
210221
while true; do
211-
IFS='' read -rd $'\x1e' -u $_GITSTATUS_RESP_FD -t "$timeout" reply || return
212-
[[ "$reply" == $'hello\x1f0' ]] && break
213-
[[ "$reply" == . ]] || return
222+
reply=
223+
if ! IFS='' read -rd $'\x1e' -u $_GITSTATUS_RESP_FD -t "$timeout" reply; then
224+
culprit="$reply"
225+
return 1
226+
fi
227+
[[ "$reply" == $'}hello\x1f0' ]] && break
214228
if [[ -z "$dl" ]]; then
215229
dl=1
216230
if [[ -t 2 ]]; then
@@ -238,8 +252,11 @@ function gitstatus_start() {
238252
}
239253

240254
if ! gitstatus_start_impl; then
241-
echo "" >&2
242-
echo "gitstatus_start: failed to start gitstatusd" >&2
255+
>&2 printf '\n'
256+
>&2 printf '[\033[31mERROR\033[0m]: gitstatus failed to initialize.\n'
257+
if [[ -n "${culprit-}" ]]; then
258+
>&2 printf '\n%s\n' "$culprit"
259+
fi
243260
[[ -z "${req_fifo:-}" ]] || command rm -f "$req_fifo"
244261
[[ -z "${resp_fifo:-}" ]] || command rm -f "$resp_fifo"
245262
unset -f gitstatus_start_impl

0 commit comments

Comments
 (0)