prompt: fix missing file errors in zsh

zsh seems to have a bug while redirecting the stderr of the 'read' command: % read foo 2>/dev/null <foo zsh: no such file or directory: foo Which causes errors to be displayed when certain files are missing. Let's add a convenience function to manually check if the file is readable before calling "read". Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Felipe Contreras committed Apr 11, 2014 at 18:32 UTC 59d3924fbb300ebfe05f9079bcfe29d0a3f30661
1 file changed +14 -7
contrib/completion/git-prompt.sh
+14 -7
@@ -259,6 +259,13 @@ __git_ps1_colorize_gitstring ()
259 r="$c_clear$r"
260 }
261
262 +eread ()
263 +{
264 + f="$1"
265 + shift
266 + test -r "$f" && read "$@" <"$f"
267 +}
268 +
269 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
270 # when called from PS1 using command substitution
271 # in this mode it prints text to add to bash PS1 prompt (includes branch name)
@@ -321,9 +328,9 @@ __git_ps1 ()
328 local step=""
329 local total=""
330 if [ -d "$g/rebase-merge" ]; then
324 - read b 2>/dev/null <"$g/rebase-merge/head-name"
325 - read step 2>/dev/null <"$g/rebase-merge/msgnum"
326 - read total 2>/dev/null <"$g/rebase-merge/end"
331 + eread "$g/rebase-merge/head-name" b
332 + eread "$g/rebase-merge/msgnum" step
333 + eread "$g/rebase-merge/end" total
334 if [ -f "$g/rebase-merge/interactive" ]; then
335 r="|REBASE-i"
336 else
@@ -331,10 +338,10 @@ __git_ps1 ()
338 fi
339 else
340 if [ -d "$g/rebase-apply" ]; then
334 - read step 2>/dev/null <"$g/rebase-apply/next"
335 - read total 2>/dev/null <"$g/rebase-apply/last"
341 + eread "$g/rebase-apply/next" step
342 + eread "$g/rebase-apply/last" total
343 if [ -f "$g/rebase-apply/rebasing" ]; then
337 - read b 2>/dev/null <"$g/rebase-apply/head-name"
344 + eread "$g/rebase-apply/head-name" b
345 r="|REBASE"
346 elif [ -f "$g/rebase-apply/applying" ]; then
347 r="|AM"
@@ -358,7 +365,7 @@ __git_ps1 ()
365 b="$(git symbolic-ref HEAD 2>/dev/null)"
366 else
367 local head=""
361 - if ! read head 2>/dev/null <"$g/HEAD"; then
368 + if ! eread "$g/HEAD" head; then
369 if [ $pcmode = yes ]; then
370 PS1="$ps1pc_start$ps1pc_end"
371 fi