git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Justin Donnelly via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Justin Donnelly <justinrdonnelly@gmail.com>,
	Justin Donnelly <justinrdonnelly@gmail.com>
Subject: [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt
Date: Tue, 26 Jul 2022 01:23:15 +0000	[thread overview]
Message-ID: <pull.1302.git.1658798595633.gitgitgadget@gmail.com> (raw)

From: Justin Donnelly <justinrdonnelly@gmail.com>

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>
---
    Show 'CONFLICT' indicator at command prompt
    
    This patch adds functionality for bash/zsh to show 'CONFLICT' on the
    prompt in cases where there are unresolved conflicts. It includes the
    ability for the functionality to be disabled by setting an environment
    variable (enabled by default).
    
    The conflict state is determined by running git ls-files --unmerged. In
    my testing, the performance was very good. It took around 0.01 seconds
    to run git ls-files --unmerged regardless of the number of conflicts, or
    their depth, even on vary large projects (Linux kernel). However, if
    anybody has any concerns with this, I'm open to other options.
    
    I had to make some changes to several tests in t9903-bash-prompt.sh to
    make them pass (adding '|CONFLICT' to expected output of __git_ps1). The
    changes clearly make sense, since there is a conflict in those test
    cases. But I'm curious if others think it's the best approach.
    
    Test options considered:
    
     1. Set GIT_PS1_OMITCONFLICTSTATE before the tests. This would prevent
        the new functionality from taking effect.
     2. Update the tests to include "|CONFLICT" where applicable (this is
        what I've done so far).
     3. Duplicate the tests that needed to be updated, and do #⁠1 for one of
        the tests and #⁠2 for the other.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1302%2Fjustinrdonnelly%2Fconflict-indicator-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1302/justinrdonnelly/conflict-indicator-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1302

 contrib/completion/git-prompt.sh | 11 ++++++++++-
 t/t9903-bash-prompt.sh           |  9 +++++----
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 1435548e004..2d30eb5c17e 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -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:
@@ -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=""
@@ -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
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 6a30f5719c3..9314776211c 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -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 &&
@@ -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 &&
@@ -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 &&
@@ -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" &&

base-commit: 6a475b71f8c4ce708d69fdc9317aefbde3769e25
-- 
gitgitgadget

             reply	other threads:[~2022-07-26  1:23 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-26  1:23 Justin Donnelly via GitGitGadget [this message]
2022-07-27 20:13 ` [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt Junio C Hamano
2022-07-28  2:17   ` Justin Donnelly
2022-07-28 17:44     ` Junio C Hamano
2022-07-28 23:33       ` Justin Donnelly
2022-07-29 22:08 ` [PATCH v2] git-prompt: show presence of unresolved conflicts " Justin Donnelly via GitGitGadget
2022-08-14 21:06   ` Justin Donnelly
2022-08-15  4:22     ` Junio C Hamano
2022-08-15 12:50       ` Johannes Schindelin
2022-08-16  3:36         ` Justin Donnelly
2022-08-15 13:04       ` Phillip Wood
2022-08-15 16:00         ` Junio C Hamano
2022-08-16  4:20           ` Justin Donnelly
2022-08-16 23:32             ` Justin Donnelly
2022-08-17  0:18   ` [PATCH v3] " Justin Donnelly via GitGitGadget
2022-08-19 11:29     ` Johannes Schindelin
2022-08-19 17:57       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.1302.git.1658798595633.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=justinrdonnelly@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).