git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt
@ 2022-07-26  1:23 Justin Donnelly via GitGitGadget
  2022-07-27 20:13 ` Junio C Hamano
  2022-07-29 22:08 ` [PATCH v2] git-prompt: show presence of unresolved conflicts " Justin Donnelly via GitGitGadget
  0 siblings, 2 replies; 17+ messages in thread
From: Justin Donnelly via GitGitGadget @ 2022-07-26  1:23 UTC (permalink / raw)
  To: git; +Cc: Justin Donnelly, Justin Donnelly

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

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt
  2022-07-26  1:23 [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt Justin Donnelly via GitGitGadget
@ 2022-07-27 20:13 ` Junio C Hamano
  2022-07-28  2:17   ` Justin Donnelly
  2022-07-29 22:08 ` [PATCH v2] git-prompt: show presence of unresolved conflicts " Justin Donnelly via GitGitGadget
  1 sibling, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2022-07-27 20:13 UTC (permalink / raw)
  To: Justin Donnelly via GitGitGadget; +Cc: git, Justin Donnelly

"Justin Donnelly via GitGitGadget" <gitgitgadget@gmail.com> writes:

> 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.
> +#

It is unusual to subject our unsuspecting users to new features in a
way that is done by this patch.  A more usual practice, I think, is
to tell the users that they can set GIT_PS1_INCLUDECONFLICTSTATE to
"yes" if they want to opt in, and trigger the new feature only to
them.  Later, we may decide that the feature is useful and widely
apprlicable enough, at which time it may be turned on by default and
tell the users to set GIT_PS1_INCLUDECONFLICTSTATE to "no" if they
do not want to see it.  But one step at a time.

> @@ -508,6 +511,12 @@ __git_ps1 ()
>  		r="$r $step/$total"
>  	fi
>  
> +	local conflict="" # state indicator for unresolved conflicts
> +	if [[ -z "${GIT_PS1_OMITCONFLICTSTATE-}" ]] &&

And flipping the polarity and disabling it by default would be a
simple change, I would imagine, that can be made here.

> +	   [[ $(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' '

Of course, this needs to be tweaked if we did so.  Running the test
in the default state will not need this change, but then ...

> -	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 &&

... a new test that runs the same thing but with the new feature
asked by setting the environment variable would show the new |CONFLICT
marker.

Thanks.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt
  2022-07-27 20:13 ` Junio C Hamano
@ 2022-07-28  2:17   ` Justin Donnelly
  2022-07-28 17:44     ` Junio C Hamano
  0 siblings, 1 reply; 17+ messages in thread
From: Justin Donnelly @ 2022-07-28  2:17 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Justin Donnelly via GitGitGadget, git

Thanks for the feedback.


On Wed, Jul 27, 2022 at 4:13 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Justin Donnelly via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > 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.
> > +#
>
> It is unusual to subject our unsuspecting users to new features in a
> way that is done by this patch.  A more usual practice, I think, is
> to tell the users that they can set GIT_PS1_INCLUDECONFLICTSTATE to
> "yes" if they want to opt in, and trigger the new feature only to
> them.  Later, we may decide that the feature is useful and widely
> apprlicable enough, at which time it may be turned on by default and
> tell the users to set GIT_PS1_INCLUDECONFLICTSTATE to "no" if they
> do not want to see it.  But one step at a time.
>

I see that most of the state indicators are disabled by default, so it
makes sense to be consistent. Should I make a variable with 'yes'/'no'
values, or set/unset? 'yes'/'no' has the benefit that if the default
is later changed, existing setups will continue to work. Set/unset
makes it harder to change the default later (I guess you could just
use a new variable with a different name), but is more consistent with
the other state indicators. I'm leaning towards set/unset, but it's
not a strong preference. Let me know if you disagree.

> > @@ -508,6 +511,12 @@ __git_ps1 ()
> >               r="$r $step/$total"
> >       fi
> >
> > +     local conflict="" # state indicator for unresolved conflicts
> > +     if [[ -z "${GIT_PS1_OMITCONFLICTSTATE-}" ]] &&
>
> And flipping the polarity and disabling it by default would be a
> simple change, I would imagine, that can be made here.
>
> > +        [[ $(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' '
>
> Of course, this needs to be tweaked if we did so.  Running the test
> in the default state will not need this change, but then ...
>
> > -     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 &&
>
> ... a new test that runs the same thing but with the new feature
> asked by setting the environment variable would show the new |CONFLICT
> marker.

I'll add these.

>
> Thanks.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt
  2022-07-28  2:17   ` Justin Donnelly
@ 2022-07-28 17:44     ` Junio C Hamano
  2022-07-28 23:33       ` Justin Donnelly
  0 siblings, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2022-07-28 17:44 UTC (permalink / raw)
  To: Justin Donnelly; +Cc: Justin Donnelly via GitGitGadget, git

Justin Donnelly <justinrdonnelly@gmail.com> writes:

>> It is unusual to subject our unsuspecting users to new features in a
>> way that is done by this patch.  A more usual practice, I think, is
>> to tell the users that they can set GIT_PS1_INCLUDECONFLICTSTATE to
>> "yes" if they want to opt in, and trigger the new feature only to
>> them.  Later, we may decide that the feature is useful and widely
>> apprlicable enough, at which time it may be turned on by default and
>> tell the users to set GIT_PS1_INCLUDECONFLICTSTATE to "no" if they
>> do not want to see it.  But one step at a time.
>>
>
> I see that most of the state indicators are disabled by default, so it
> makes sense to be consistent. Should I make a variable with 'yes'/'no'
> values, or set/unset? 'yes'/'no' has the benefit that if the default
> is later changed, existing setups will continue to work. Set/unset
> makes it harder to change the default later ...

As you said, "an environment variable that is set triggers the
feature" is harder to transition.  Starting from a clear Boolean is
probably easy to see what is going on, and that is why I suggested
doing that way.  

Ones that are "if set, enabled" can be corrected later when needed,
e.g. GIT_PS1_FROTZ may enable the feature FROTZ when the environment
is set, but when we introduce two or more ways to do FROTZ thing
(e.g. in addition to always do FROTZ, which may be the original
design of "an environment that is set triggers the feature", we may
add "automatically enable FROTZ only under this and that
condition"), we may say "when set to 'auto', do the auto-FROTZ, and
when set to 'yes', do FROTZ as we have always done.  When set to any
other value, warn and then do FROTZ").  But let's leave them outside
the scope of the topic.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt
  2022-07-28 17:44     ` Junio C Hamano
@ 2022-07-28 23:33       ` Justin Donnelly
  0 siblings, 0 replies; 17+ messages in thread
From: Justin Donnelly @ 2022-07-28 23:33 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Justin Donnelly via GitGitGadget, git

On Thu, Jul 28, 2022 at 1:44 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Justin Donnelly <justinrdonnelly@gmail.com> writes:
>
> >> It is unusual to subject our unsuspecting users to new features in a
> >> way that is done by this patch.  A more usual practice, I think, is
> >> to tell the users that they can set GIT_PS1_INCLUDECONFLICTSTATE to
> >> "yes" if they want to opt in, and trigger the new feature only to
> >> them.  Later, we may decide that the feature is useful and widely
> >> apprlicable enough, at which time it may be turned on by default and
> >> tell the users to set GIT_PS1_INCLUDECONFLICTSTATE to "no" if they
> >> do not want to see it.  But one step at a time.
> >>
> >
> > I see that most of the state indicators are disabled by default, so it
> > makes sense to be consistent. Should I make a variable with 'yes'/'no'
> > values, or set/unset? 'yes'/'no' has the benefit that if the default
> > is later changed, existing setups will continue to work. Set/unset
> > makes it harder to change the default later ...
>
> As you said, "an environment variable that is set triggers the
> feature" is harder to transition.  Starting from a clear Boolean is
> probably easy to see what is going on, and that is why I suggested
> doing that way.
>
> Ones that are "if set, enabled" can be corrected later when needed,
> e.g. GIT_PS1_FROTZ may enable the feature FROTZ when the environment
> is set, but when we introduce two or more ways to do FROTZ thing
> (e.g. in addition to always do FROTZ, which may be the original
> design of "an environment that is set triggers the feature", we may
> add "automatically enable FROTZ only under this and that
> condition"), we may say "when set to 'auto', do the auto-FROTZ, and
> when set to 'yes', do FROTZ as we have always done.  When set to any
> other value, warn and then do FROTZ").  But let's leave them outside
> the scope of the topic.
>

Thanks. I'll incorporate these suggestions into a re-roll.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  2022-07-26  1:23 [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt Justin Donnelly via GitGitGadget
  2022-07-27 20:13 ` Junio C Hamano
@ 2022-07-29 22:08 ` Justin Donnelly via GitGitGadget
  2022-08-14 21:06   ` Justin Donnelly
  2022-08-17  0:18   ` [PATCH v3] " Justin Donnelly via GitGitGadget
  1 sibling, 2 replies; 17+ messages in thread
From: Justin Donnelly via GitGitGadget @ 2022-07-29 22:08 UTC (permalink / raw)
  To: git; +Cc: Justin Donnelly, Justin Donnelly

From: Justin Donnelly <justinrdonnelly@gmail.com>

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>
---
    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. The feature is
    only enabled after setting an environment variable.
    
    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 very large projects (Linux kernel). However, if
    anybody has any concerns with this, I'm open to other options.
    
    Any tests that were impacted (with the conflict prompt feature enabled)
    were duplicated. The original test was left as-is (no changes, and
    conflict prompt feature disabled). The new version of each test enables
    the conflict prompt feature and confirms the prompt includes
    "|CONFLICT".
    
    ------------------------------------------------------------------------
    
    Changes since v1:
    
     * This feature is now disabled by default.
     * Created new tests for conflict state (instead of modifying existing
       tests).

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

Range-diff vs v1:

 1:  e380826dcaf ! 1:  7154d695426 git-prompt: show 'CONFLICT' indicator at command prompt
     @@ Metadata
      Author: Justin Donnelly <justinrdonnelly@gmail.com>
      
       ## Commit message ##
     -    git-prompt: show 'CONFLICT' indicator at command prompt
     +    git-prompt: show presence of unresolved conflicts at command prompt
      
     -    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.
     +    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)
      
     @@ contrib/completion/git-prompt.sh
       # 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 a notification on the prompt when there are
     ++# unresolved conflicts, set GIT_PS1_SHOWCONFLICTSTATE to "yes". The
     ++# prompt will include "|CONFLICT".
      +#
       # If you would like to see more information about the identity of
       # commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
     @@ contrib/completion/git-prompt.sh: __git_ps1 ()
       	fi
       
      +	local conflict="" # state indicator for unresolved conflicts
     -+	if [[ -z "${GIT_PS1_OMITCONFLICTSTATE-}" ]] &&
     ++	if [[ "${GIT_PS1_SHOWCONFLICTSTATE}" == "yes" ]] &&
      +	   [[ $(git ls-files --unmerged 2>/dev/null) ]]; then
      +		conflict="|CONFLICT"
      +	fi
     @@ contrib/completion/git-prompt.sh: __git_ps1 ()
       		if [ "${__git_printf_supports_v-}" != yes ]; then
      
       ## t/t9903-bash-prompt.sh ##
     -@@ t/t9903-bash-prompt.sh: 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 &&
     +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase merge' '
       	test_when_finished "git checkout main" &&
       	test_must_fail git rebase --merge b1 b2 &&
     -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase merge' '
     + 	test_when_finished "git rebase --abort" &&
     +-	__git_ps1 >"$actual" &&
     ++	(
     ++		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
     ++		__git_ps1 >"$actual"
     ++	) &&
     ++	test_cmp expected "$actual"
     ++'
     ++
     ++test_expect_success 'prompt - rebase merge conflict' '
     ++	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
     ++	git checkout b2 &&
     ++	test_when_finished "git checkout main" &&
     ++	test_must_fail git rebase --merge b1 b2 &&
     ++	test_when_finished "git rebase --abort" &&
     ++	(
     ++		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
     ++		__git_ps1 >"$actual"
     ++	) &&
     + 	test_cmp expected "$actual"
       '
       
     - test_expect_success 'prompt - rebase am' '
     --	printf " (b2|REBASE 1/3)" >expected &&
     -+	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
     - 	git checkout b2 &&
     +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase am' '
       	test_when_finished "git checkout main" &&
       	test_must_fail git rebase --apply b1 b2 &&
     -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase am' '
     + 	test_when_finished "git rebase --abort" &&
     +-	__git_ps1 >"$actual" &&
     ++	(
     ++		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
     ++		__git_ps1 >"$actual"
     ++	) &&
     ++	test_cmp expected "$actual"
     ++'
     ++
     ++test_expect_success 'prompt - rebase am conflict' '
     ++	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
     ++	git checkout b2 &&
     ++	test_when_finished "git checkout main" &&
     ++	test_must_fail git rebase --apply b1 b2 &&
     ++	test_when_finished "git rebase --abort" &&
     ++	(
     ++		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
     ++		__git_ps1 >"$actual"
     ++	) &&
     + 	test_cmp expected "$actual"
       '
       
     - test_expect_success 'prompt - merge' '
     --	printf " (b1|MERGING)" >expected &&
     -+	printf " (b1|MERGING|CONFLICT)" >expected &&
     - 	git checkout b1 &&
     +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - merge' '
       	test_when_finished "git checkout main" &&
       	test_must_fail git merge b2 &&
     -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - merge' '
     + 	test_when_finished "git reset --hard" &&
     +-	__git_ps1 >"$actual" &&
     ++	(
     ++		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
     ++		__git_ps1 >"$actual"
     ++	) &&
     ++	test_cmp expected "$actual"
     ++'
     ++
     ++test_expect_success 'prompt - merge conflict' '
     ++	printf " (b1|MERGING|CONFLICT)" >expected &&
     ++	git checkout b1 &&
     ++	test_when_finished "git checkout main" &&
     ++	test_must_fail git merge b2 &&
     ++	test_when_finished "git reset --hard" &&
     ++	(
     ++		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
     ++		__git_ps1 >"$actual"
     ++	) &&
     + 	test_cmp expected "$actual"
       '
       
     - test_expect_success 'prompt - cherry-pick' '
     --	printf " (main|CHERRY-PICKING)" >expected &&
     -+	printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
     +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
     + 	printf " (main|CHERRY-PICKING)" >expected &&
       	test_must_fail git cherry-pick b1 b1^ &&
       	test_when_finished "git cherry-pick --abort" &&
     - 	__git_ps1 >"$actual" &&
     +-	__git_ps1 >"$actual" &&
     ++	(
     ++		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
     ++		__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" &&
     +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
     + 	test_cmp expected "$actual"
     + '
     + 
     ++test_expect_success 'prompt - cherry-pick conflict' '
     ++	printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
     ++	test_must_fail git cherry-pick b1 b1^ &&
     ++	test_when_finished "git cherry-pick --abort" &&
     ++	(
     ++		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
     ++		__git_ps1 >"$actual"
     ++	) &&
     ++	test_cmp expected "$actual"
     ++'
     ++
     + test_expect_success 'prompt - revert' '
     + 	printf " (main|REVERTING)" >expected &&
     + 	test_must_fail git revert b1^ b1 &&


 contrib/completion/git-prompt.sh | 12 +++++-
 t/t9903-bash-prompt.sh           | 70 ++++++++++++++++++++++++++++++--
 2 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 1435548e004..57972c2845c 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -84,6 +84,10 @@
 # single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
 # by setting GIT_PS1_OMITSPARSESTATE.
 #
+# If you would like to see a notification on the prompt when there are
+# unresolved conflicts, set GIT_PS1_SHOWCONFLICTSTATE to "yes". The
+# prompt will include "|CONFLICT".
+#
 # 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 +512,12 @@ __git_ps1 ()
 		r="$r $step/$total"
 	fi
 
+	local conflict="" # state indicator for unresolved conflicts
+	if [[ "${GIT_PS1_SHOWCONFLICTSTATE}" == "yes" ]] &&
+	   [[ $(git ls-files --unmerged 2>/dev/null) ]]; then
+		conflict="|CONFLICT"
+	fi
+
 	local w=""
 	local i=""
 	local s=""
@@ -572,7 +582,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..47eb98893ef 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -188,7 +188,23 @@ test_expect_success 'prompt - rebase merge' '
 	test_when_finished "git checkout main" &&
 	test_must_fail git rebase --merge b1 b2 &&
 	test_when_finished "git rebase --abort" &&
-	__git_ps1 >"$actual" &&
+	(
+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - rebase merge conflict' '
+	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
+	git checkout b2 &&
+	test_when_finished "git checkout main" &&
+	test_must_fail git rebase --merge b1 b2 &&
+	test_when_finished "git rebase --abort" &&
+	(
+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
+		__git_ps1 >"$actual"
+	) &&
 	test_cmp expected "$actual"
 '
 
@@ -198,7 +214,23 @@ test_expect_success 'prompt - rebase am' '
 	test_when_finished "git checkout main" &&
 	test_must_fail git rebase --apply b1 b2 &&
 	test_when_finished "git rebase --abort" &&
-	__git_ps1 >"$actual" &&
+	(
+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - rebase am conflict' '
+	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
+	git checkout b2 &&
+	test_when_finished "git checkout main" &&
+	test_must_fail git rebase --apply b1 b2 &&
+	test_when_finished "git rebase --abort" &&
+	(
+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
+		__git_ps1 >"$actual"
+	) &&
 	test_cmp expected "$actual"
 '
 
@@ -208,7 +240,23 @@ test_expect_success 'prompt - merge' '
 	test_when_finished "git checkout main" &&
 	test_must_fail git merge b2 &&
 	test_when_finished "git reset --hard" &&
-	__git_ps1 >"$actual" &&
+	(
+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - merge conflict' '
+	printf " (b1|MERGING|CONFLICT)" >expected &&
+	git checkout b1 &&
+	test_when_finished "git checkout main" &&
+	test_must_fail git merge b2 &&
+	test_when_finished "git reset --hard" &&
+	(
+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
+		__git_ps1 >"$actual"
+	) &&
 	test_cmp expected "$actual"
 '
 
@@ -216,7 +264,10 @@ test_expect_success 'prompt - cherry-pick' '
 	printf " (main|CHERRY-PICKING)" >expected &&
 	test_must_fail git cherry-pick b1 b1^ &&
 	test_when_finished "git cherry-pick --abort" &&
-	__git_ps1 >"$actual" &&
+	(
+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
+		__git_ps1 >"$actual"
+	) &&
 	test_cmp expected "$actual" &&
 	git reset --merge &&
 	test_must_fail git rev-parse CHERRY_PICK_HEAD &&
@@ -224,6 +275,17 @@ test_expect_success 'prompt - cherry-pick' '
 	test_cmp expected "$actual"
 '
 
+test_expect_success 'prompt - cherry-pick conflict' '
+	printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
+	test_must_fail git cherry-pick b1 b1^ &&
+	test_when_finished "git cherry-pick --abort" &&
+	(
+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
 test_expect_success 'prompt - revert' '
 	printf " (main|REVERTING)" >expected &&
 	test_must_fail git revert b1^ b1 &&

base-commit: 6a475b71f8c4ce708d69fdc9317aefbde3769e25
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  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-17  0:18   ` [PATCH v3] " Justin Donnelly via GitGitGadget
  1 sibling, 1 reply; 17+ messages in thread
From: Justin Donnelly @ 2022-08-14 21:06 UTC (permalink / raw)
  To: Justin Donnelly via GitGitGadget
  Cc: git, Ævar Arnfjörð Bjarmason, Junio C Hamano,
	newren, phillip.wood, Johannes.Schindelin

On Fri, Jul 29, 2022 at 6:08 PM Justin Donnelly via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Justin Donnelly <justinrdonnelly@gmail.com>
>
> 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>
> ---
>     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. The feature is
>     only enabled after setting an environment variable.
>
>     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 very large projects (Linux kernel). However, if
>     anybody has any concerns with this, I'm open to other options.
>
>     Any tests that were impacted (with the conflict prompt feature enabled)
>     were duplicated. The original test was left as-is (no changes, and
>     conflict prompt feature disabled). The new version of each test enables
>     the conflict prompt feature and confirms the prompt includes
>     "|CONFLICT".
>
>     ------------------------------------------------------------------------
>
>     Changes since v1:
>
>      * This feature is now disabled by default.
>      * Created new tests for conflict state (instead of modifying existing
>        tests).
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1302%2Fjustinrdonnelly%2Fconflict-indicator-v2
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1302/justinrdonnelly/conflict-indicator-v2
> Pull-Request: https://github.com/gitgitgadget/git/pull/1302
>
> Range-diff vs v1:
>
>  1:  e380826dcaf ! 1:  7154d695426 git-prompt: show 'CONFLICT' indicator at command prompt
>      @@ Metadata
>       Author: Justin Donnelly <justinrdonnelly@gmail.com>
>
>        ## Commit message ##
>      -    git-prompt: show 'CONFLICT' indicator at command prompt
>      +    git-prompt: show presence of unresolved conflicts at command prompt
>
>      -    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.
>      +    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)
>
>      @@ contrib/completion/git-prompt.sh
>        # 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 a notification on the prompt when there are
>      ++# unresolved conflicts, set GIT_PS1_SHOWCONFLICTSTATE to "yes". The
>      ++# prompt will include "|CONFLICT".
>       +#
>        # If you would like to see more information about the identity of
>        # commits checked out as a detached HEAD, set GIT_PS1_DESCRIBE_STYLE
>      @@ contrib/completion/git-prompt.sh: __git_ps1 ()
>         fi
>
>       + local conflict="" # state indicator for unresolved conflicts
>      -+ if [[ -z "${GIT_PS1_OMITCONFLICTSTATE-}" ]] &&
>      ++ if [[ "${GIT_PS1_SHOWCONFLICTSTATE}" == "yes" ]] &&
>       +    [[ $(git ls-files --unmerged 2>/dev/null) ]]; then
>       +         conflict="|CONFLICT"
>       + fi
>      @@ contrib/completion/git-prompt.sh: __git_ps1 ()
>                 if [ "${__git_printf_supports_v-}" != yes ]; then
>
>        ## t/t9903-bash-prompt.sh ##
>      -@@ t/t9903-bash-prompt.sh: 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 &&
>      +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase merge' '
>         test_when_finished "git checkout main" &&
>         test_must_fail git rebase --merge b1 b2 &&
>      -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase merge' '
>      +  test_when_finished "git rebase --abort" &&
>      +- __git_ps1 >"$actual" &&
>      ++ (
>      ++         sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
>      ++         __git_ps1 >"$actual"
>      ++ ) &&
>      ++ test_cmp expected "$actual"
>      ++'
>      ++
>      ++test_expect_success 'prompt - rebase merge conflict' '
>      ++ printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
>      ++ git checkout b2 &&
>      ++ test_when_finished "git checkout main" &&
>      ++ test_must_fail git rebase --merge b1 b2 &&
>      ++ test_when_finished "git rebase --abort" &&
>      ++ (
>      ++         GIT_PS1_SHOWCONFLICTSTATE="yes" &&
>      ++         __git_ps1 >"$actual"
>      ++ ) &&
>      +  test_cmp expected "$actual"
>        '
>
>      - test_expect_success 'prompt - rebase am' '
>      -- printf " (b2|REBASE 1/3)" >expected &&
>      -+ printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
>      -  git checkout b2 &&
>      +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase am' '
>         test_when_finished "git checkout main" &&
>         test_must_fail git rebase --apply b1 b2 &&
>      -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase am' '
>      +  test_when_finished "git rebase --abort" &&
>      +- __git_ps1 >"$actual" &&
>      ++ (
>      ++         sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
>      ++         __git_ps1 >"$actual"
>      ++ ) &&
>      ++ test_cmp expected "$actual"
>      ++'
>      ++
>      ++test_expect_success 'prompt - rebase am conflict' '
>      ++ printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
>      ++ git checkout b2 &&
>      ++ test_when_finished "git checkout main" &&
>      ++ test_must_fail git rebase --apply b1 b2 &&
>      ++ test_when_finished "git rebase --abort" &&
>      ++ (
>      ++         GIT_PS1_SHOWCONFLICTSTATE="yes" &&
>      ++         __git_ps1 >"$actual"
>      ++ ) &&
>      +  test_cmp expected "$actual"
>        '
>
>      - test_expect_success 'prompt - merge' '
>      -- printf " (b1|MERGING)" >expected &&
>      -+ printf " (b1|MERGING|CONFLICT)" >expected &&
>      -  git checkout b1 &&
>      +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - merge' '
>         test_when_finished "git checkout main" &&
>         test_must_fail git merge b2 &&
>      -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - merge' '
>      +  test_when_finished "git reset --hard" &&
>      +- __git_ps1 >"$actual" &&
>      ++ (
>      ++         sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
>      ++         __git_ps1 >"$actual"
>      ++ ) &&
>      ++ test_cmp expected "$actual"
>      ++'
>      ++
>      ++test_expect_success 'prompt - merge conflict' '
>      ++ printf " (b1|MERGING|CONFLICT)" >expected &&
>      ++ git checkout b1 &&
>      ++ test_when_finished "git checkout main" &&
>      ++ test_must_fail git merge b2 &&
>      ++ test_when_finished "git reset --hard" &&
>      ++ (
>      ++         GIT_PS1_SHOWCONFLICTSTATE="yes" &&
>      ++         __git_ps1 >"$actual"
>      ++ ) &&
>      +  test_cmp expected "$actual"
>        '
>
>      - test_expect_success 'prompt - cherry-pick' '
>      -- printf " (main|CHERRY-PICKING)" >expected &&
>      -+ printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
>      +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
>      +  printf " (main|CHERRY-PICKING)" >expected &&
>         test_must_fail git cherry-pick b1 b1^ &&
>         test_when_finished "git cherry-pick --abort" &&
>      -  __git_ps1 >"$actual" &&
>      +- __git_ps1 >"$actual" &&
>      ++ (
>      ++         sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
>      ++         __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" &&
>      +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
>      +  test_cmp expected "$actual"
>      + '
>      +
>      ++test_expect_success 'prompt - cherry-pick conflict' '
>      ++ printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
>      ++ test_must_fail git cherry-pick b1 b1^ &&
>      ++ test_when_finished "git cherry-pick --abort" &&
>      ++ (
>      ++         GIT_PS1_SHOWCONFLICTSTATE="yes" &&
>      ++         __git_ps1 >"$actual"
>      ++ ) &&
>      ++ test_cmp expected "$actual"
>      ++'
>      ++
>      + test_expect_success 'prompt - revert' '
>      +  printf " (main|REVERTING)" >expected &&
>      +  test_must_fail git revert b1^ b1 &&
>
>
>  contrib/completion/git-prompt.sh | 12 +++++-
>  t/t9903-bash-prompt.sh           | 70 ++++++++++++++++++++++++++++++--
>  2 files changed, 77 insertions(+), 5 deletions(-)
>
> diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
> index 1435548e004..57972c2845c 100644
> --- a/contrib/completion/git-prompt.sh
> +++ b/contrib/completion/git-prompt.sh
> @@ -84,6 +84,10 @@
>  # single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
>  # by setting GIT_PS1_OMITSPARSESTATE.
>  #
> +# If you would like to see a notification on the prompt when there are
> +# unresolved conflicts, set GIT_PS1_SHOWCONFLICTSTATE to "yes". The
> +# prompt will include "|CONFLICT".
> +#
>  # 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 +512,12 @@ __git_ps1 ()
>                 r="$r $step/$total"
>         fi
>
> +       local conflict="" # state indicator for unresolved conflicts
> +       if [[ "${GIT_PS1_SHOWCONFLICTSTATE}" == "yes" ]] &&
> +          [[ $(git ls-files --unmerged 2>/dev/null) ]]; then
> +               conflict="|CONFLICT"
> +       fi
> +
>         local w=""
>         local i=""
>         local s=""
> @@ -572,7 +582,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..47eb98893ef 100755
> --- a/t/t9903-bash-prompt.sh
> +++ b/t/t9903-bash-prompt.sh
> @@ -188,7 +188,23 @@ test_expect_success 'prompt - rebase merge' '
>         test_when_finished "git checkout main" &&
>         test_must_fail git rebase --merge b1 b2 &&
>         test_when_finished "git rebase --abort" &&
> -       __git_ps1 >"$actual" &&
> +       (
> +               sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
> +               __git_ps1 >"$actual"
> +       ) &&
> +       test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - rebase merge conflict' '
> +       printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
> +       git checkout b2 &&
> +       test_when_finished "git checkout main" &&
> +       test_must_fail git rebase --merge b1 b2 &&
> +       test_when_finished "git rebase --abort" &&
> +       (
> +               GIT_PS1_SHOWCONFLICTSTATE="yes" &&
> +               __git_ps1 >"$actual"
> +       ) &&
>         test_cmp expected "$actual"
>  '
>
> @@ -198,7 +214,23 @@ test_expect_success 'prompt - rebase am' '
>         test_when_finished "git checkout main" &&
>         test_must_fail git rebase --apply b1 b2 &&
>         test_when_finished "git rebase --abort" &&
> -       __git_ps1 >"$actual" &&
> +       (
> +               sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
> +               __git_ps1 >"$actual"
> +       ) &&
> +       test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - rebase am conflict' '
> +       printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
> +       git checkout b2 &&
> +       test_when_finished "git checkout main" &&
> +       test_must_fail git rebase --apply b1 b2 &&
> +       test_when_finished "git rebase --abort" &&
> +       (
> +               GIT_PS1_SHOWCONFLICTSTATE="yes" &&
> +               __git_ps1 >"$actual"
> +       ) &&
>         test_cmp expected "$actual"
>  '
>
> @@ -208,7 +240,23 @@ test_expect_success 'prompt - merge' '
>         test_when_finished "git checkout main" &&
>         test_must_fail git merge b2 &&
>         test_when_finished "git reset --hard" &&
> -       __git_ps1 >"$actual" &&
> +       (
> +               sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
> +               __git_ps1 >"$actual"
> +       ) &&
> +       test_cmp expected "$actual"
> +'
> +
> +test_expect_success 'prompt - merge conflict' '
> +       printf " (b1|MERGING|CONFLICT)" >expected &&
> +       git checkout b1 &&
> +       test_when_finished "git checkout main" &&
> +       test_must_fail git merge b2 &&
> +       test_when_finished "git reset --hard" &&
> +       (
> +               GIT_PS1_SHOWCONFLICTSTATE="yes" &&
> +               __git_ps1 >"$actual"
> +       ) &&
>         test_cmp expected "$actual"
>  '
>
> @@ -216,7 +264,10 @@ test_expect_success 'prompt - cherry-pick' '
>         printf " (main|CHERRY-PICKING)" >expected &&
>         test_must_fail git cherry-pick b1 b1^ &&
>         test_when_finished "git cherry-pick --abort" &&
> -       __git_ps1 >"$actual" &&
> +       (
> +               sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
> +               __git_ps1 >"$actual"
> +       ) &&
>         test_cmp expected "$actual" &&
>         git reset --merge &&
>         test_must_fail git rev-parse CHERRY_PICK_HEAD &&
> @@ -224,6 +275,17 @@ test_expect_success 'prompt - cherry-pick' '
>         test_cmp expected "$actual"
>  '
>
> +test_expect_success 'prompt - cherry-pick conflict' '
> +       printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
> +       test_must_fail git cherry-pick b1 b1^ &&
> +       test_when_finished "git cherry-pick --abort" &&
> +       (
> +               GIT_PS1_SHOWCONFLICTSTATE="yes" &&
> +               __git_ps1 >"$actual"
> +       ) &&
> +       test_cmp expected "$actual"
> +'
> +
>  test_expect_success 'prompt - revert' '
>         printf " (main|REVERTING)" >expected &&
>         test_must_fail git revert b1^ b1 &&
>
> base-commit: 6a475b71f8c4ce708d69fdc9317aefbde3769e25
> --
> gitgitgadget

[SubmittingPatches](https://github.com/git/git/blob/master/Documentation/SubmittingPatches)
recommends using `git-contacts` to determine who to "cc" on the email.
I used GitGitGadget, which doesn't cc anybody by default (I see now
that others manually add cc's to the PR description). So I've added
the following cc's to this email:
Ævar Arnfjörð Bjarmason
Junio C Hamano
Elijah Newren
Phillip Wood
Johannes Schindelin

I hope this is against protocol/etiquette, but after some initial
feedback from Junio, I haven't gotten any more. I wasn't sure if
nobody had seen the patch, or if there just wasn't any interest.

Thanks,
Justin

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  2022-08-14 21:06   ` Justin Donnelly
@ 2022-08-15  4:22     ` Junio C Hamano
  2022-08-15 12:50       ` Johannes Schindelin
  2022-08-15 13:04       ` Phillip Wood
  0 siblings, 2 replies; 17+ messages in thread
From: Junio C Hamano @ 2022-08-15  4:22 UTC (permalink / raw)
  To: Justin Donnelly
  Cc: Justin Donnelly via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, newren, phillip.wood,
	Johannes.Schindelin

Justin Donnelly <justinrdonnelly@gmail.com> writes:

> I hope this is against protocol/etiquette, but after some initial
> feedback from Junio, I haven't gotten any more. I wasn't sure if
> nobody had seen the patch, or if there just wasn't any interest.

It probably is a bit of both.  I personally did not see much point
in adding the long "conflicts" marker to the shell prompt (I did
worry about possible complaints by end users triggered by seeing
them suddenly without asking, which was why I commented on the
patch) and I was waiting for interested folks to speak out.

I do not know about other folks if they did see and did not find it
interesting, they are not looking at others' work, or your second
round came on a particularly bad day (busy with other topics, or
weekend just before the list was swamped with many new topics at the
beginning of a new week, or something).  Pinging on the thread like
you did would be the right thing to do to "kick" those, who did see
and who were in favor but who kept silent, into action ;-)




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  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
  1 sibling, 1 reply; 17+ messages in thread
From: Johannes Schindelin @ 2022-08-15 12:50 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Justin Donnelly, Justin Donnelly via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, newren, phillip.wood

Hi,

On Sun, 14 Aug 2022, Junio C Hamano wrote:

> Justin Donnelly <justinrdonnelly@gmail.com> writes:
>
> > I hope this is against protocol/etiquette, but after some initial
> > feedback from Junio, I haven't gotten any more. I wasn't sure if
> > nobody had seen the patch, or if there just wasn't any interest.
>
> It probably is a bit of both.  I personally did not see much point
> in adding the long "conflicts" marker to the shell prompt (I did
> worry about possible complaints by end users triggered by seeing
> them suddenly without asking, which was why I commented on the
> patch) and I was waiting for interested folks to speak out.

Speaking for myself, I was too busy elsewhere. But now that I looked over
the patch, I think it is fine. My only feedback is that it would be wise
to only add a single test case because that is plenty enough (after all,
it validates the `ls-files --unmerged` call and not the `cherry-pick`
code) and it is unnecessary to waste the electricity on additional tests
cases (even if somebody else foots the bill, it would do well for all of
us to start being more mindful about energy consumption).

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  2022-08-15  4:22     ` Junio C Hamano
  2022-08-15 12:50       ` Johannes Schindelin
@ 2022-08-15 13:04       ` Phillip Wood
  2022-08-15 16:00         ` Junio C Hamano
  1 sibling, 1 reply; 17+ messages in thread
From: Phillip Wood @ 2022-08-15 13:04 UTC (permalink / raw)
  To: Junio C Hamano, Justin Donnelly
  Cc: Justin Donnelly via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, newren, phillip.wood,
	Johannes.Schindelin

Hi

On 15/08/2022 05:22, Junio C Hamano wrote:
> Justin Donnelly <justinrdonnelly@gmail.com> writes:
> 
>> I hope this is against protocol/etiquette, but after some initial
>> feedback from Junio, I haven't gotten any more. I wasn't sure if
>> nobody had seen the patch, or if there just wasn't any interest.
> 
> It probably is a bit of both.  I personally did not see much point
> in adding the long "conflicts" marker to the shell prompt (I did
> worry about possible complaints by end users triggered by seeing
> them suddenly without asking, which was why I commented on the
> patch) and I was waiting for interested folks to speak out.
> 
> I do not know about other folks if they did see and did not find it
> interesting, they are not looking at others' work, or your second
> round came on a particularly bad day (busy with other topics, or
> weekend just before the list was swamped with many new topics at the
> beginning of a new week, or something).  Pinging on the thread like
> you did would be the right thing to do to "kick" those, who did see
> and who were in favor but who kept silent, into action ;-)

I had not commented as I don't use the prompt. I have just had a quick 
read and I wonder if it would be more efficient to use
     git diff --cached --quiet --diff-filter=U
rather than
     git ls-files --unmerged 2>/dev/null
to check if there are unmerged entries, but as we don't normally expect 
there to be that many unmerged entries it probably does not matter.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  2022-08-15 13:04       ` Phillip Wood
@ 2022-08-15 16:00         ` Junio C Hamano
  2022-08-16  4:20           ` Justin Donnelly
  0 siblings, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2022-08-15 16:00 UTC (permalink / raw)
  To: Phillip Wood
  Cc: Justin Donnelly, Justin Donnelly via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, newren, phillip.wood,
	Johannes.Schindelin

Phillip Wood <phillip.wood123@gmail.com> writes:

> I had not commented as I don't use the prompt. I have just had a quick
> read and I wonder if it would be more efficient to use
>     git diff --cached --quiet --diff-filter=U
> rather than
>     git ls-files --unmerged 2>/dev/null
> to check if there are unmerged entries,

The former reads the on-disk index into in-core index, and reads
tree objects (recursively for subdirectories) referenced by the
HEAD, walks both in parallel to find differences and filters out the
result to unmerged (I am not sure how well diff-filter works with
unmerged paths, though).

The latter rads the on-disk index into in-core index, scans the
entries and finds unmerged entries.

So if we compare the overhead to run either command standalone, I am
reasonably sure that the latter would be a lot more efficient.

But if the shell prompt code already needs to run the diff-index for
other reasons (e.g. to show if there is any modification added to
the index), that may change the equation.  Instead of adding a
separate and extra call to "ls-files -u", it might be more efficient
if you can somehow piggy-back on an existing diff-index call.  For
example, you may be running "git diff --cached --quiet" for exit code
to show if any change has been added, but you can instead run "git
diff --no-ext-diff --no-renames --cached --name-status" and (1) if
there is any output, then the index is dirty, and (2) if there is a
line that begins with "U", you have an unmerged path right there.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  2022-08-15 12:50       ` Johannes Schindelin
@ 2022-08-16  3:36         ` Justin Donnelly
  0 siblings, 0 replies; 17+ messages in thread
From: Justin Donnelly @ 2022-08-16  3:36 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Junio C Hamano, Justin Donnelly via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, newren, phillip.wood

On Mon, Aug 15, 2022 at 8:50 AM Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
>
> Hi,
>
> On Sun, 14 Aug 2022, Junio C Hamano wrote:
>
> > Justin Donnelly <justinrdonnelly@gmail.com> writes:
> >
> > > I hope this is against protocol/etiquette, but after some initial
> > > feedback from Junio, I haven't gotten any more. I wasn't sure if
> > > nobody had seen the patch, or if there just wasn't any interest.
> >
> > It probably is a bit of both.  I personally did not see much point
> > in adding the long "conflicts" marker to the shell prompt (I did
> > worry about possible complaints by end users triggered by seeing
> > them suddenly without asking, which was why I commented on the
> > patch) and I was waiting for interested folks to speak out.
>
> Speaking for myself, I was too busy elsewhere. But now that I looked over
> the patch, I think it is fine. My only feedback is that it would be wise
> to only add a single test case because that is plenty enough (after all,
> it validates the `ls-files --unmerged` call and not the `cherry-pick`
> code) and it is unnecessary to waste the electricity on additional tests
> cases (even if somebody else foots the bill, it would do well for all of
> us to start being more mindful about energy consumption).

That makes sense. I'll get started on a re-roll to just have a single
test that focuses specifically on the conflict indicator.

>
> Ciao,
> Dscho

Thanks,
Justin

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  2022-08-15 16:00         ` Junio C Hamano
@ 2022-08-16  4:20           ` Justin Donnelly
  2022-08-16 23:32             ` Justin Donnelly
  0 siblings, 1 reply; 17+ messages in thread
From: Justin Donnelly @ 2022-08-16  4:20 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Phillip Wood, Justin Donnelly via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, newren, phillip.wood,
	Johannes.Schindelin

On Mon, Aug 15, 2022 at 12:00 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Phillip Wood <phillip.wood123@gmail.com> writes:
>
> > I had not commented as I don't use the prompt. I have just had a quick
> > read and I wonder if it would be more efficient to use
> >     git diff --cached --quiet --diff-filter=U
> > rather than
> >     git ls-files --unmerged 2>/dev/null
> > to check if there are unmerged entries,
>
> The former reads the on-disk index into in-core index, and reads
> tree objects (recursively for subdirectories) referenced by the
> HEAD, walks both in parallel to find differences and filters out the
> result to unmerged (I am not sure how well diff-filter works with
> unmerged paths, though).
>
> The latter rads the on-disk index into in-core index, scans the
> entries and finds unmerged entries.
>
> So if we compare the overhead to run either command standalone, I am
> reasonably sure that the latter would be a lot more efficient.
>

Here's how I tested performance. The setup and test execution code are
below. I tested each technique (`git ls-files --unmerged 2>/dev/null`
and `git diff --cached --quiet --diff-filter=U`) 3 times and listed
the results. Please let me know if you see any problems with the
methodology.

Setup:
mkdir -p /tmp/perf/base && cd /tmp/perf/base
git clone https://github.com/torvalds/linux.git

Each test:
cd /tmp/perf
rm -rf test
cp -r base test
cd test/linux/drivers/watchdog
git switch --detach v6.0-rc1 # pick some commit for consistency
for f in *; do echo "/* a */" >> $f; done # 182 files
git stash
for f in *; do echo "/* b */" >> $f; done
git commit -am "adding to end of files in watchdog directory"
git stash pop
time [[ $(git ls-files --unmerged 2>/dev/null) ]]
# OR run the next one instead
# time ! git diff --cached --quiet --diff-filter=U 2>/dev/null

Results (hopefully this text lines up better for others than it does for me):
time [[ $(git ls-files --unmerged 2>/dev/null) ]]
      run 1     run 2     run3
real  0m0.008s  0m0.009s  0m0.008s
user  0m0.005s  0m0.001s  0m0.004s
sys   0m0.004s  0m0.008s  0m0.004s

time ! git diff --cached --quiet --diff-filter=U 2>/dev/null
      run 1     run 2     run3
real  0m0.009s  0m0.009s  0m0.007s
user  0m0.004s  0m0.009s  0m0.007s
sys   0m0.004s  0m0.000s  0m0.000s

As you can see, the results are basically the same. I'm not sure if
real world usage would yield different results. So for now, I'll defer
to Junio's analysis and stick with `ls-files --unmerged`.

> But if the shell prompt code already needs to run the diff-index for
> other reasons (e.g. to show if there is any modification added to
> the index), that may change the equation.  Instead of adding a
> separate and extra call to "ls-files -u", it might be more efficient
> if you can somehow piggy-back on an existing diff-index call.  For
> example, you may be running "git diff --cached --quiet" for exit code
> to show if any change has been added, but you can instead run "git
> diff --no-ext-diff --no-renames --cached --name-status" and (1) if
> there is any output, then the index is dirty, and (2) if there is a
> line that begins with "U", you have an unmerged path right there.

It had not occurred to me to consolidate and piggy-back off of other
commands. I find the idea intriguing, but am not sure it makes sense
to do it for only a single feature (especially this feature since the
time to determine the conflict is short). I think it would make the
code more complex, and it might be better to take a holistic approach
to such an effort. Let me know if you strongly disagree.

Thanks,
Justin

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2] git-prompt: show presence of unresolved conflicts at command prompt
  2022-08-16  4:20           ` Justin Donnelly
@ 2022-08-16 23:32             ` Justin Donnelly
  0 siblings, 0 replies; 17+ messages in thread
From: Justin Donnelly @ 2022-08-16 23:32 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Phillip Wood, Justin Donnelly via GitGitGadget, git,
	Ævar Arnfjörð Bjarmason, newren, phillip.wood,
	Johannes.Schindelin

On Tue, Aug 16, 2022 at 12:20 AM Justin Donnelly
<justinrdonnelly@gmail.com> wrote:
>
> On Mon, Aug 15, 2022 at 12:00 PM Junio C Hamano <gitster@pobox.com> wrote:
> >
> > Phillip Wood <phillip.wood123@gmail.com> writes:
> >
> > > I had not commented as I don't use the prompt. I have just had a quick
> > > read and I wonder if it would be more efficient to use
> > >     git diff --cached --quiet --diff-filter=U
> > > rather than
> > >     git ls-files --unmerged 2>/dev/null
> > > to check if there are unmerged entries,
> >
> > The former reads the on-disk index into in-core index, and reads
> > tree objects (recursively for subdirectories) referenced by the
> > HEAD, walks both in parallel to find differences and filters out the
> > result to unmerged (I am not sure how well diff-filter works with
> > unmerged paths, though).
> >
> > The latter rads the on-disk index into in-core index, scans the
> > entries and finds unmerged entries.
> >
> > So if we compare the overhead to run either command standalone, I am
> > reasonably sure that the latter would be a lot more efficient.
> >
>
> Here's how I tested performance. The setup and test execution code are
> below. I tested each technique (`git ls-files --unmerged 2>/dev/null`
> and `git diff --cached --quiet --diff-filter=U`) 3 times and listed
> the results. Please let me know if you see any problems with the
> methodology.
>
> Setup:
> mkdir -p /tmp/perf/base && cd /tmp/perf/base
> git clone https://github.com/torvalds/linux.git
>
> Each test:
> cd /tmp/perf
> rm -rf test
> cp -r base test
> cd test/linux/drivers/watchdog
> git switch --detach v6.0-rc1 # pick some commit for consistency
> for f in *; do echo "/* a */" >> $f; done # 182 files
> git stash
> for f in *; do echo "/* b */" >> $f; done
> git commit -am "adding to end of files in watchdog directory"
> git stash pop
> time [[ $(git ls-files --unmerged 2>/dev/null) ]]
> # OR run the next one instead
> # time ! git diff --cached --quiet --diff-filter=U 2>/dev/null
>
> Results (hopefully this text lines up better for others than it does for me):
> time [[ $(git ls-files --unmerged 2>/dev/null) ]]
>       run 1     run 2     run3
> real  0m0.008s  0m0.009s  0m0.008s
> user  0m0.005s  0m0.001s  0m0.004s
> sys   0m0.004s  0m0.008s  0m0.004s
>
> time ! git diff --cached --quiet --diff-filter=U 2>/dev/null
>       run 1     run 2     run3
> real  0m0.009s  0m0.009s  0m0.007s
> user  0m0.004s  0m0.009s  0m0.007s
> sys   0m0.004s  0m0.000s  0m0.000s
>

Actually, what's probably more important is how long the commands take
when there is no conflict (that will be a far more common situation).
I tested that today, and the numbers were about the same.

> As you can see, the results are basically the same. I'm not sure if
> real world usage would yield different results. So for now, I'll defer
> to Junio's analysis and stick with `ls-files --unmerged`.
>
> > But if the shell prompt code already needs to run the diff-index for
> > other reasons (e.g. to show if there is any modification added to
> > the index), that may change the equation.  Instead of adding a
> > separate and extra call to "ls-files -u", it might be more efficient
> > if you can somehow piggy-back on an existing diff-index call.  For
> > example, you may be running "git diff --cached --quiet" for exit code
> > to show if any change has been added, but you can instead run "git
> > diff --no-ext-diff --no-renames --cached --name-status" and (1) if
> > there is any output, then the index is dirty, and (2) if there is a
> > line that begins with "U", you have an unmerged path right there.
>
> It had not occurred to me to consolidate and piggy-back off of other
> commands. I find the idea intriguing, but am not sure it makes sense
> to do it for only a single feature (especially this feature since the
> time to determine the conflict is short). I think it would make the
> code more complex, and it might be better to take a holistic approach
> to such an effort. Let me know if you strongly disagree.
>
> Thanks,
> Justin

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3] git-prompt: show presence of unresolved conflicts at command prompt
  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-17  0:18   ` Justin Donnelly via GitGitGadget
  2022-08-19 11:29     ` Johannes Schindelin
  1 sibling, 1 reply; 17+ messages in thread
From: Justin Donnelly via GitGitGadget @ 2022-08-17  0:18 UTC (permalink / raw)
  To: git; +Cc: Johannes Schindelin, Phillip Wood, Justin Donnelly,
	Justin Donnelly

From: Justin Donnelly <justinrdonnelly@gmail.com>

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>
---
    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. The feature is
    only enabled after setting an environment variable.
    
    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 very large projects (Linux kernel). I got similar
    performance running git diff --cached --quiet --diff-filter=U.
    
    ------------------------------------------------------------------------
    
    Changes since v2:
    
     * There is now a single new test focused explicitly on this feature
       instead of making copies of existing tests and modifying them.
    
    Changes since v1:
    
     * This feature is now disabled by default.
     * Created new tests for conflict state (instead of modifying existing
       tests).

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

Range-diff vs v2:

 1:  7154d695426 ! 1:  2ea5c403924 git-prompt: show presence of unresolved conflicts at command prompt
     @@ contrib/completion/git-prompt.sh: __git_ps1 ()
       		if [ "${__git_printf_supports_v-}" != yes ]; then
      
       ## t/t9903-bash-prompt.sh ##
     -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase merge' '
     - 	test_when_finished "git checkout main" &&
     - 	test_must_fail git rebase --merge b1 b2 &&
     - 	test_when_finished "git rebase --abort" &&
     --	__git_ps1 >"$actual" &&
     -+	(
     -+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
     -+		__git_ps1 >"$actual"
     -+	) &&
     -+	test_cmp expected "$actual"
     -+'
     -+
     -+test_expect_success 'prompt - rebase merge conflict' '
     -+	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
     -+	git checkout b2 &&
     -+	test_when_finished "git checkout main" &&
     -+	test_must_fail git rebase --merge b1 b2 &&
     -+	test_when_finished "git rebase --abort" &&
     -+	(
     -+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
     -+		__git_ps1 >"$actual"
     -+	) &&
     - 	test_cmp expected "$actual"
     - '
     - 
     -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase am' '
     - 	test_when_finished "git checkout main" &&
     - 	test_must_fail git rebase --apply b1 b2 &&
     - 	test_when_finished "git rebase --abort" &&
     --	__git_ps1 >"$actual" &&
     -+	(
     -+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
     -+		__git_ps1 >"$actual"
     -+	) &&
     -+	test_cmp expected "$actual"
     -+'
     -+
     -+test_expect_success 'prompt - rebase am conflict' '
     -+	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
     -+	git checkout b2 &&
     -+	test_when_finished "git checkout main" &&
     -+	test_must_fail git rebase --apply b1 b2 &&
     -+	test_when_finished "git rebase --abort" &&
     -+	(
     -+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
     -+		__git_ps1 >"$actual"
     -+	) &&
     - 	test_cmp expected "$actual"
     - '
     - 
     -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - merge' '
     - 	test_when_finished "git checkout main" &&
     - 	test_must_fail git merge b2 &&
     - 	test_when_finished "git reset --hard" &&
     --	__git_ps1 >"$actual" &&
     -+	(
     -+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
     -+		__git_ps1 >"$actual"
     -+	) &&
     -+	test_cmp expected "$actual"
     -+'
     -+
     -+test_expect_success 'prompt - merge conflict' '
     -+	printf " (b1|MERGING|CONFLICT)" >expected &&
     -+	git checkout b1 &&
     -+	test_when_finished "git checkout main" &&
     -+	test_must_fail git merge b2 &&
     -+	test_when_finished "git reset --hard" &&
     -+	(
     -+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
     -+		__git_ps1 >"$actual"
     -+	) &&
     - 	test_cmp expected "$actual"
     - '
     - 
     -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
     - 	printf " (main|CHERRY-PICKING)" >expected &&
     - 	test_must_fail git cherry-pick b1 b1^ &&
     - 	test_when_finished "git cherry-pick --abort" &&
     --	__git_ps1 >"$actual" &&
     -+	(
     -+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
     -+		__git_ps1 >"$actual"
     -+	) &&
     - 	test_cmp expected "$actual" &&
     - 	git reset --merge &&
     - 	test_must_fail git rev-parse CHERRY_PICK_HEAD &&
     -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
     +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - hide if pwd ignored - inside gitdir' '
       	test_cmp expected "$actual"
       '
       
     -+test_expect_success 'prompt - cherry-pick conflict' '
     -+	printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
     -+	test_must_fail git cherry-pick b1 b1^ &&
     -+	test_when_finished "git cherry-pick --abort" &&
     ++test_expect_success 'prompt - conflict indicator' '
     ++	printf " (main|CONFLICT)" >expected &&
     ++	echo "stash" >file &&
     ++	git stash &&
     ++	test_when_finished "git stash drop" &&
     ++	echo "commit" >file &&
     ++	git commit -m "commit" file &&
     ++	test_when_finished "git reset --hard HEAD~" &&
     ++	test_must_fail git stash apply &&
      +	(
      +		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
      +		__git_ps1 >"$actual"
     @@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
      +	test_cmp expected "$actual"
      +'
      +
     - test_expect_success 'prompt - revert' '
     - 	printf " (main|REVERTING)" >expected &&
     - 	test_must_fail git revert b1^ b1 &&
     + test_done


 contrib/completion/git-prompt.sh | 12 +++++++++++-
 t/t9903-bash-prompt.sh           | 16 ++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 1435548e004..57972c2845c 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -84,6 +84,10 @@
 # single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
 # by setting GIT_PS1_OMITSPARSESTATE.
 #
+# If you would like to see a notification on the prompt when there are
+# unresolved conflicts, set GIT_PS1_SHOWCONFLICTSTATE to "yes". The
+# prompt will include "|CONFLICT".
+#
 # 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 +512,12 @@ __git_ps1 ()
 		r="$r $step/$total"
 	fi
 
+	local conflict="" # state indicator for unresolved conflicts
+	if [[ "${GIT_PS1_SHOWCONFLICTSTATE}" == "yes" ]] &&
+	   [[ $(git ls-files --unmerged 2>/dev/null) ]]; then
+		conflict="|CONFLICT"
+	fi
+
 	local w=""
 	local i=""
 	local s=""
@@ -572,7 +582,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..d459fae6551 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -759,4 +759,20 @@ test_expect_success 'prompt - hide if pwd ignored - inside gitdir' '
 	test_cmp expected "$actual"
 '
 
+test_expect_success 'prompt - conflict indicator' '
+	printf " (main|CONFLICT)" >expected &&
+	echo "stash" >file &&
+	git stash &&
+	test_when_finished "git stash drop" &&
+	echo "commit" >file &&
+	git commit -m "commit" file &&
+	test_when_finished "git reset --hard HEAD~" &&
+	test_must_fail git stash apply &&
+	(
+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
 test_done

base-commit: 9bf691b78cf906751e65d65ba0c6ffdcd9a5a12c
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v3] git-prompt: show presence of unresolved conflicts at command prompt
  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
  0 siblings, 1 reply; 17+ messages in thread
From: Johannes Schindelin @ 2022-08-19 11:29 UTC (permalink / raw)
  To: Justin Donnelly via GitGitGadget
  Cc: git, Phillip Wood, Justin Donnelly, Justin Donnelly

Hi Justin,

On Wed, 17 Aug 2022, Justin Donnelly via GitGitGadget wrote:

> From: Justin Donnelly <justinrdonnelly@gmail.com>
>
> 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>
> ---
>     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. The feature is
>     only enabled after setting an environment variable.
>
>     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 very large projects (Linux kernel). I got similar
>     performance running git diff --cached --quiet --diff-filter=U.
>
>     ------------------------------------------------------------------------
>
>     Changes since v2:
>
>      * There is now a single new test focused explicitly on this feature
>        instead of making copies of existing tests and modifying them.
>
>     Changes since v1:
>
>      * This feature is now disabled by default.
>      * Created new tests for conflict state (instead of modifying existing
>        tests).

This iteration looks good to me.

Thanks!
Dscho

>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1302%2Fjustinrdonnelly%2Fconflict-indicator-v3
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1302/justinrdonnelly/conflict-indicator-v3
> Pull-Request: https://github.com/gitgitgadget/git/pull/1302
>
> Range-diff vs v2:
>
>  1:  7154d695426 ! 1:  2ea5c403924 git-prompt: show presence of unresolved conflicts at command prompt
>      @@ contrib/completion/git-prompt.sh: __git_ps1 ()
>        		if [ "${__git_printf_supports_v-}" != yes ]; then
>
>        ## t/t9903-bash-prompt.sh ##
>      -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase merge' '
>      - 	test_when_finished "git checkout main" &&
>      - 	test_must_fail git rebase --merge b1 b2 &&
>      - 	test_when_finished "git rebase --abort" &&
>      --	__git_ps1 >"$actual" &&
>      -+	(
>      -+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
>      -+		__git_ps1 >"$actual"
>      -+	) &&
>      -+	test_cmp expected "$actual"
>      -+'
>      -+
>      -+test_expect_success 'prompt - rebase merge conflict' '
>      -+	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
>      -+	git checkout b2 &&
>      -+	test_when_finished "git checkout main" &&
>      -+	test_must_fail git rebase --merge b1 b2 &&
>      -+	test_when_finished "git rebase --abort" &&
>      -+	(
>      -+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
>      -+		__git_ps1 >"$actual"
>      -+	) &&
>      - 	test_cmp expected "$actual"
>      - '
>      -
>      -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - rebase am' '
>      - 	test_when_finished "git checkout main" &&
>      - 	test_must_fail git rebase --apply b1 b2 &&
>      - 	test_when_finished "git rebase --abort" &&
>      --	__git_ps1 >"$actual" &&
>      -+	(
>      -+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
>      -+		__git_ps1 >"$actual"
>      -+	) &&
>      -+	test_cmp expected "$actual"
>      -+'
>      -+
>      -+test_expect_success 'prompt - rebase am conflict' '
>      -+	printf " (b2|REBASE 1/3|CONFLICT)" >expected &&
>      -+	git checkout b2 &&
>      -+	test_when_finished "git checkout main" &&
>      -+	test_must_fail git rebase --apply b1 b2 &&
>      -+	test_when_finished "git rebase --abort" &&
>      -+	(
>      -+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
>      -+		__git_ps1 >"$actual"
>      -+	) &&
>      - 	test_cmp expected "$actual"
>      - '
>      -
>      -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - merge' '
>      - 	test_when_finished "git checkout main" &&
>      - 	test_must_fail git merge b2 &&
>      - 	test_when_finished "git reset --hard" &&
>      --	__git_ps1 >"$actual" &&
>      -+	(
>      -+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
>      -+		__git_ps1 >"$actual"
>      -+	) &&
>      -+	test_cmp expected "$actual"
>      -+'
>      -+
>      -+test_expect_success 'prompt - merge conflict' '
>      -+	printf " (b1|MERGING|CONFLICT)" >expected &&
>      -+	git checkout b1 &&
>      -+	test_when_finished "git checkout main" &&
>      -+	test_must_fail git merge b2 &&
>      -+	test_when_finished "git reset --hard" &&
>      -+	(
>      -+		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
>      -+		__git_ps1 >"$actual"
>      -+	) &&
>      - 	test_cmp expected "$actual"
>      - '
>      -
>      -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
>      - 	printf " (main|CHERRY-PICKING)" >expected &&
>      - 	test_must_fail git cherry-pick b1 b1^ &&
>      - 	test_when_finished "git cherry-pick --abort" &&
>      --	__git_ps1 >"$actual" &&
>      -+	(
>      -+		sane_unset GIT_PS1_SHOWCONFLICTSTATE &&
>      -+		__git_ps1 >"$actual"
>      -+	) &&
>      - 	test_cmp expected "$actual" &&
>      - 	git reset --merge &&
>      - 	test_must_fail git rev-parse CHERRY_PICK_HEAD &&
>      -@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
>      +@@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - hide if pwd ignored - inside gitdir' '
>        	test_cmp expected "$actual"
>        '
>
>      -+test_expect_success 'prompt - cherry-pick conflict' '
>      -+	printf " (main|CHERRY-PICKING|CONFLICT)" >expected &&
>      -+	test_must_fail git cherry-pick b1 b1^ &&
>      -+	test_when_finished "git cherry-pick --abort" &&
>      ++test_expect_success 'prompt - conflict indicator' '
>      ++	printf " (main|CONFLICT)" >expected &&
>      ++	echo "stash" >file &&
>      ++	git stash &&
>      ++	test_when_finished "git stash drop" &&
>      ++	echo "commit" >file &&
>      ++	git commit -m "commit" file &&
>      ++	test_when_finished "git reset --hard HEAD~" &&
>      ++	test_must_fail git stash apply &&
>       +	(
>       +		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
>       +		__git_ps1 >"$actual"
>      @@ t/t9903-bash-prompt.sh: test_expect_success 'prompt - cherry-pick' '
>       +	test_cmp expected "$actual"
>       +'
>       +
>      - test_expect_success 'prompt - revert' '
>      - 	printf " (main|REVERTING)" >expected &&
>      - 	test_must_fail git revert b1^ b1 &&
>      + test_done
>
>
>  contrib/completion/git-prompt.sh | 12 +++++++++++-
>  t/t9903-bash-prompt.sh           | 16 ++++++++++++++++
>  2 files changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
> index 1435548e004..57972c2845c 100644
> --- a/contrib/completion/git-prompt.sh
> +++ b/contrib/completion/git-prompt.sh
> @@ -84,6 +84,10 @@
>  # single '?' character by setting GIT_PS1_COMPRESSSPARSESTATE, or omitted
>  # by setting GIT_PS1_OMITSPARSESTATE.
>  #
> +# If you would like to see a notification on the prompt when there are
> +# unresolved conflicts, set GIT_PS1_SHOWCONFLICTSTATE to "yes". The
> +# prompt will include "|CONFLICT".
> +#
>  # 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 +512,12 @@ __git_ps1 ()
>  		r="$r $step/$total"
>  	fi
>
> +	local conflict="" # state indicator for unresolved conflicts
> +	if [[ "${GIT_PS1_SHOWCONFLICTSTATE}" == "yes" ]] &&
> +	   [[ $(git ls-files --unmerged 2>/dev/null) ]]; then
> +		conflict="|CONFLICT"
> +	fi
> +
>  	local w=""
>  	local i=""
>  	local s=""
> @@ -572,7 +582,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..d459fae6551 100755
> --- a/t/t9903-bash-prompt.sh
> +++ b/t/t9903-bash-prompt.sh
> @@ -759,4 +759,20 @@ test_expect_success 'prompt - hide if pwd ignored - inside gitdir' '
>  	test_cmp expected "$actual"
>  '
>
> +test_expect_success 'prompt - conflict indicator' '
> +	printf " (main|CONFLICT)" >expected &&
> +	echo "stash" >file &&
> +	git stash &&
> +	test_when_finished "git stash drop" &&
> +	echo "commit" >file &&
> +	git commit -m "commit" file &&
> +	test_when_finished "git reset --hard HEAD~" &&
> +	test_must_fail git stash apply &&
> +	(
> +		GIT_PS1_SHOWCONFLICTSTATE="yes" &&
> +		__git_ps1 >"$actual"
> +	) &&
> +	test_cmp expected "$actual"
> +'
> +
>  test_done
>
> base-commit: 9bf691b78cf906751e65d65ba0c6ffdcd9a5a12c
> --
> gitgitgadget
>

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3] git-prompt: show presence of unresolved conflicts at command prompt
  2022-08-19 11:29     ` Johannes Schindelin
@ 2022-08-19 17:57       ` Junio C Hamano
  0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2022-08-19 17:57 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Justin Donnelly via GitGitGadget, git, Phillip Wood,
	Justin Donnelly

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> Hi Justin,
>
> On Wed, 17 Aug 2022, Justin Donnelly via GitGitGadget wrote:
> ...
>>     Changes since v2:
>>
>>      * There is now a single new test focused explicitly on this feature
>>        instead of making copies of existing tests and modifying them.
>>
>>     Changes since v1:
>>
>>      * This feature is now disabled by default.
>>      * Created new tests for conflict state (instead of modifying existing
>>        tests).
>
> This iteration looks good to me.

Thanks, both.  Will queue.

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2022-08-19 18:11 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-26  1:23 [PATCH] git-prompt: show 'CONFLICT' indicator at command prompt Justin Donnelly via GitGitGadget
2022-07-27 20:13 ` 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

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).