git-prompt: show presence of unresolved conflicts at command prompt

If GIT_PS1_SHOWCONFLICTSTATE is set to "yes", show the word "CONFLICT" on the command prompt when there are unresolved conflicts. Example prompt: (main|CONFLICT) Signed-off-by: Justin Donnelly <justinrdonnelly@gmail.com> Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Justin Donnelly committed Aug 17, 2022 at 00:18 UTC e03acd0d4ad75115f454041fac3300ae796f108f
2 files changed +27 -1
contrib/completion/git-prompt.sh
+11 -1
@@ -84,6 +84,10 @@
84 # single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
85 # by setting GIT_PS1_OMITSPARSESTATE.
86 #
87 +# If you would like to see a notification on the prompt when there are
88 +# unresolved conflicts, set GIT_PS1_SHOWCONFLICTSTATE to "yes". The
89 +# prompt will include "|CONFLICT".
90 +#
91 # If you would like to see more information about the identity of
92 # commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
93 # to one of these values:
@@ -508,6 +512,12 @@ __git_ps1 ()
512 r="$r $step/$total"
513 fi
514
515 + local conflict="" # state indicator for unresolved conflicts
516 + if [[ "${GIT_PS1_SHOWCONFLICTSTATE}" == "yes" ]] &&
517 + [[ $(git ls-files --unmerged 2>/dev/null) ]]; then
518 + conflict="|CONFLICT"
519 + fi
520 +
521 local w=""
522 local i=""
523 local s=""
@@ -572,7 +582,7 @@ __git_ps1 ()
582 fi
583
584 local f="$h$w$i$s$u$p"
575 - local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}"
585 + local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}${conflict}"
586
587 if [ $pcmode = yes ]; then
588 if [ "${__git_printf_supports_v-}" != yes ]; then
t/t9903-bash-prompt.sh
+16
@@ -759,4 +759,20 @@ test_expect_success 'prompt - hide if pwd ignored - inside gitdir' '
759 test_cmp expected "$actual"
760 '
761
762 +test_expect_success 'prompt - conflict indicator' '
763 + printf " (main|CONFLICT)" >expected &&
764 + echo "stash" >file &&
765 + git stash &&
766 + test_when_finished "git stash drop" &&
767 + echo "commit" >file &&
768 + git commit -m "commit" file &&
769 + test_when_finished "git reset --hard HEAD~" &&
770 + test_must_fail git stash apply &&
771 + (
772 + GIT_PS1_SHOWCONFLICTSTATE="yes" &&
773 + __git_ps1 >"$actual"
774 + ) &&
775 + test_cmp expected "$actual"
776 +'
777 +
778 test_done