Skip to content

Commit

Permalink
git-prompt: show 'CONFLICT' indicator at command prompt
Browse files Browse the repository at this point in the history
When there are unresolved conflicts, show the word 'CONFLICT' on the
command prompt. Similar to other state indicators, this provides
information to the user about the current state of the repository.

Example prompt: (main|CONFLICT)

Signed-off-by: Justin Donnelly <justinrdonnelly@gmail.com>
  • Loading branch information
justinrdonnelly committed Jul 25, 2022
1 parent 6a475b7 commit e380826
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
11 changes: 10 additions & 1 deletion contrib/completion/git-prompt.sh
Expand Up @@ -84,6 +84,9 @@
# single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
# by setting GIT_PS1_OMITSPARSESTATE.
#
# When there is a conflict, the prompt will include "|CONFLICT". This can
# be omitted by setting GIT_PS1_OMITCONFLICTSTATE.
#
# If you would like to see more information about the identity of
# commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
# to one of these values:
Expand Down Expand Up @@ -508,6 +511,12 @@ __git_ps1 ()
r="$r $step/$total"
fi

local conflict="" # state indicator for unresolved conflicts
if [[ -z "${GIT_PS1_OMITCONFLICTSTATE-}" ]] &&
[[ $(git ls-files --unmerged 2>/dev/null) ]]; then
conflict="|CONFLICT"
fi

local w=""
local i=""
local s=""
Expand Down Expand Up @@ -572,7 +581,7 @@ __git_ps1 ()
fi

local f="$h$w$i$s$u$p"
local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}"
local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}${conflict}"

if [ $pcmode = yes ]; then
if [ "${__git_printf_supports_v-}" != yes ]; then
Expand Down
9 changes: 5 additions & 4 deletions t/t9903-bash-prompt.sh
Expand Up @@ -183,7 +183,7 @@ test_expect_success 'prompt - interactive rebase' '
'

test_expect_success 'prompt - rebase merge' '
printf " (b2|REBASE 1/3)" >expected &&
printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
git checkout b2 &&
test_when_finished "git checkout main" &&
test_must_fail git rebase --merge b1 b2 &&
Expand All @@ -193,7 +193,7 @@ test_expect_success 'prompt - rebase merge' '
'

test_expect_success 'prompt - rebase am' '
printf " (b2|REBASE 1/3)" >expected &&
printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
git checkout b2 &&
test_when_finished "git checkout main" &&
test_must_fail git rebase --apply b1 b2 &&
Expand All @@ -203,7 +203,7 @@ test_expect_success 'prompt - rebase am' '
'

test_expect_success 'prompt - merge' '
printf " (b1|MERGING)" >expected &&
printf " (b1|MERGING|CONFLICT)" >expected &&
git checkout b1 &&
test_when_finished "git checkout main" &&
test_must_fail git merge b2 &&
Expand All @@ -213,11 +213,12 @@ test_expect_success 'prompt - merge' '
'

test_expect_success 'prompt - cherry-pick' '
printf " (main|CHERRY-PICKING)" >expected &&
printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
test_must_fail git cherry-pick b1 b1^ &&
test_when_finished "git cherry-pick --abort" &&
__git_ps1 >"$actual" &&
test_cmp expected "$actual" &&
printf " (main|CHERRY-PICKING)" >expected &&
git reset --merge &&
test_must_fail git rev-parse CHERRY_PICK_HEAD &&
__git_ps1 >"$actual" &&
Expand Down

0 comments on commit e380826

Please sign in to comment.