Skip to content

Commit 96ea404

Browse files
committed
bash prompt: return early from __git_ps1() when not in a git repository
... to gain one level of indentation for the bulk of the function. (The patch looks quite unreadable, you'd better check it with 'git diff -w'.) Signed-off-by: SZEDER Gábor <[email protected]>
1 parent e8f21ca commit 96ea404

File tree

1 file changed

+101
-100
lines changed

1 file changed

+101
-100
lines changed

contrib/completion/git-prompt.sh

Lines changed: 101 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -341,121 +341,122 @@ __git_ps1 ()
341341
#In PC mode PS1 always needs to be set
342342
PS1="$ps1pc_start$ps1pc_end"
343343
fi
344+
return
345+
fi
346+
347+
local r=""
348+
local b=""
349+
local step=""
350+
local total=""
351+
if [ -d "$g/rebase-merge" ]; then
352+
b="$(cat "$g/rebase-merge/head-name" 2>/dev/null)"
353+
step=$(cat "$g/rebase-merge/msgnum" 2>/dev/null)
354+
total=$(cat "$g/rebase-merge/end" 2>/dev/null)
355+
if [ -f "$g/rebase-merge/interactive" ]; then
356+
r="|REBASE-i"
357+
else
358+
r="|REBASE-m"
359+
fi
344360
else
345-
local r=""
346-
local b=""
347-
local step=""
348-
local total=""
349-
if [ -d "$g/rebase-merge" ]; then
350-
b="$(cat "$g/rebase-merge/head-name" 2>/dev/null)"
351-
step=$(cat "$g/rebase-merge/msgnum" 2>/dev/null)
352-
total=$(cat "$g/rebase-merge/end" 2>/dev/null)
353-
if [ -f "$g/rebase-merge/interactive" ]; then
354-
r="|REBASE-i"
361+
if [ -d "$g/rebase-apply" ]; then
362+
step=$(cat "$g/rebase-apply/next" 2>/dev/null)
363+
total=$(cat "$g/rebase-apply/last" 2>/dev/null)
364+
if [ -f "$g/rebase-apply/rebasing" ]; then
365+
b="$(cat "$g/rebase-apply/head-name" 2>/dev/null)"
366+
r="|REBASE"
367+
elif [ -f "$g/rebase-apply/applying" ]; then
368+
r="|AM"
355369
else
356-
r="|REBASE-m"
357-
fi
358-
else
359-
if [ -d "$g/rebase-apply" ]; then
360-
step=$(cat "$g/rebase-apply/next" 2>/dev/null)
361-
total=$(cat "$g/rebase-apply/last" 2>/dev/null)
362-
if [ -f "$g/rebase-apply/rebasing" ]; then
363-
b="$(cat "$g/rebase-apply/head-name" 2>/dev/null)"
364-
r="|REBASE"
365-
elif [ -f "$g/rebase-apply/applying" ]; then
366-
r="|AM"
367-
else
368-
r="|AM/REBASE"
369-
fi
370-
elif [ -f "$g/MERGE_HEAD" ]; then
371-
r="|MERGING"
372-
elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
373-
r="|CHERRY-PICKING"
374-
elif [ -f "$g/REVERT_HEAD" ]; then
375-
r="|REVERTING"
376-
elif [ -f "$g/BISECT_LOG" ]; then
377-
r="|BISECTING"
370+
r="|AM/REBASE"
378371
fi
372+
elif [ -f "$g/MERGE_HEAD" ]; then
373+
r="|MERGING"
374+
elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
375+
r="|CHERRY-PICKING"
376+
elif [ -f "$g/REVERT_HEAD" ]; then
377+
r="|REVERTING"
378+
elif [ -f "$g/BISECT_LOG" ]; then
379+
r="|BISECTING"
380+
fi
379381

380-
test -n "$b" ||
381-
b="$(git symbolic-ref HEAD 2>/dev/null)" || {
382-
detached=yes
383-
b="$(
384-
case "${GIT_PS1_DESCRIBE_STYLE-}" in
385-
(contains)
386-
git describe --contains HEAD ;;
387-
(branch)
388-
git describe --contains --all HEAD ;;
389-
(describe)
390-
git describe HEAD ;;
391-
(* | default)
392-
git describe --tags --exact-match HEAD ;;
393-
esac 2>/dev/null)" ||
382+
test -n "$b" ||
383+
b="$(git symbolic-ref HEAD 2>/dev/null)" || {
384+
detached=yes
385+
b="$(
386+
case "${GIT_PS1_DESCRIBE_STYLE-}" in
387+
(contains)
388+
git describe --contains HEAD ;;
389+
(branch)
390+
git describe --contains --all HEAD ;;
391+
(describe)
392+
git describe HEAD ;;
393+
(* | default)
394+
git describe --tags --exact-match HEAD ;;
395+
esac 2>/dev/null)" ||
394396

395-
b="$(git rev-parse --short HEAD 2>/dev/null)..." ||
396-
b="unknown"
397-
b="($b)"
398-
}
399-
fi
397+
b="$(git rev-parse --short HEAD 2>/dev/null)..." ||
398+
b="unknown"
399+
b="($b)"
400+
}
401+
fi
400402

401-
if [ -n "$step" ] && [ -n "$total" ]; then
402-
r="$r $step/$total"
403-
fi
403+
if [ -n "$step" ] && [ -n "$total" ]; then
404+
r="$r $step/$total"
405+
fi
404406

405-
local w=""
406-
local i=""
407-
local s=""
408-
local u=""
409-
local c=""
410-
local p=""
407+
local w=""
408+
local i=""
409+
local s=""
410+
local u=""
411+
local c=""
412+
local p=""
411413

412-
if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
413-
if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
414-
c="BARE:"
414+
if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
415+
if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
416+
c="BARE:"
417+
else
418+
b="GIT_DIR!"
419+
fi
420+
elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
421+
if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
422+
[ "$(git config --bool bash.showDirtyState)" != "false" ]
423+
then
424+
git diff --no-ext-diff --quiet --exit-code || w="*"
425+
if git rev-parse --quiet --verify HEAD >/dev/null; then
426+
git diff-index --cached --quiet HEAD -- || i="+"
415427
else
416-
b="GIT_DIR!"
417-
fi
418-
elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
419-
if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
420-
[ "$(git config --bool bash.showDirtyState)" != "false" ]
421-
then
422-
git diff --no-ext-diff --quiet --exit-code || w="*"
423-
if git rev-parse --quiet --verify HEAD >/dev/null; then
424-
git diff-index --cached --quiet HEAD -- || i="+"
425-
else
426-
i="#"
427-
fi
428-
fi
429-
if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
430-
git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
428+
i="#"
431429
fi
430+
fi
431+
if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
432+
git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
433+
fi
432434

433-
if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
434-
[ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
435-
[ -n "$(git ls-files --others --exclude-standard)" ]
436-
then
437-
u="%${ZSH_VERSION+%}"
438-
fi
435+
if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
436+
[ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
437+
[ -n "$(git ls-files --others --exclude-standard)" ]
438+
then
439+
u="%${ZSH_VERSION+%}"
440+
fi
439441

440-
if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
441-
__git_ps1_show_upstream
442-
fi
442+
if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
443+
__git_ps1_show_upstream
443444
fi
445+
fi
444446

445-
local z="${GIT_PS1_STATESEPARATOR-" "}"
446-
local f="$w$i$s$u"
447-
if [ $pcmode = yes ]; then
448-
local gitstring=
449-
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
450-
__git_ps1_colorize_gitstring
451-
else
452-
gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
453-
fi
454-
gitstring=$(printf -- "$printf_format" "$gitstring")
455-
PS1="$ps1pc_start$gitstring$ps1pc_end"
447+
local z="${GIT_PS1_STATESEPARATOR-" "}"
448+
local f="$w$i$s$u"
449+
if [ $pcmode = yes ]; then
450+
local gitstring=
451+
if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
452+
__git_ps1_colorize_gitstring
456453
else
457-
# NO color option unless in PROMPT_COMMAND mode
458-
printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"
454+
gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
459455
fi
456+
gitstring=$(printf -- "$printf_format" "$gitstring")
457+
PS1="$ps1pc_start$gitstring$ps1pc_end"
458+
else
459+
# NO color option unless in PROMPT_COMMAND mode
460+
printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"
460461
fi
461462
}

0 commit comments

Comments
 (0)