git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] git-prompt.sh: make '+' work for unborn branches

From
MBMaurice Bos <m-ou.se@m-ou.se>
Date
Mar 6, 2014, 21:19 UTC
Message-ID
<CABbCQwsKFWjk1Nzc9jmpyNdbXC+mOf5qfNg77PoiXrUOmrq6uA@mail.gmail.com>
In-Reply-To
<CABbCQwskqZzzRXm5K3dnOfRoAcx8jpsdskngmN7f1EvY6BONgw@mail.gmail.com>

I have no clue why git diff --cached isn't used instead of git diff-index. I was wondering about it, but I decided I don't know enough about git and there are probably valid reasons for doing it this way. Though, replacing it with with git diff --cached seems to have the exact same behaviour, as far as I tested. That would make the patch a little prettier, as it doesn't contain the empty tree id any more:

@@ -407,12 +407,11 @@ __git_ps1 ()
         if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
            [ "$(git config --bool bash.showDirtyState)" != "false" ]
         then
-            git diff --no-ext-diff --quiet --exit-code || w="*"
-            if [ -n "$short_sha" ]; then
-                git diff-index --cached --quiet HEAD -- || i="+"
-            else
+            if [ -z "$short_sha" ]; then
                 i="#"
             fi
+            git diff --no-ext-diff --quiet --exit-code || w="*"
+            git diff --no-ext-diff --quiet --exit-code --cached || i="$i+"
         fi
         if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] &&
            [ -r "$g/refs/stash" ]; then


Unfortunately, I don't know much about git diff-files, so I don't know
whether it could be used instead.

In another version I used myself, I indeed used 'git status
--porcelain' and used regexes /^\w/ /^.\w/ and /\?/ on the output. It
seemed to work fine, but it's a bit stupid that it lists all the files
when it could stop almost directly. An
--extremly-short-porcelain-or-whatever-you-would-call-this option that
would just output whether any file is dirty and/or indexed or
something might be useful, though maybe a bit too specific.
(--summarized ?)

-Maurice-


 2014-03-06 21:40 GMT+01:00 Jeff King <peff@peff.net>:

> On Thu, Mar 06, 2014 at 12:05:24AM +0100, Maurice Bos wrote:
>
> > For unborn branches, it now compares the index against the empty tree.
> > (Just like git status does.)
>
> Sounds sensible, although...
>
> > -                     git diff --no-ext-diff --quiet --exit-code || w="*"
> > -                     if [ -n "$short_sha" ]; then
> > -                             git diff-index --cached --quiet HEAD -- || i="+"
> > -                     else
>
> I notice the old code uses "git diff" immediately above. If we used "git
> diff --cached" here, too, I think it already knows about this empty-tree
> magic.
>
> That being said, it seems odd that we are using "git diff" in the first
> place, and not "git diff-files". This seems to blame all the way back to
> 738a94a, when the functionality was added in the first place. Am I
> missing some reason we can't use diff-files (maybe we want the
> index-refreshing side effect)?
>
> -Peff
>
> PS I thought at first that this could just use "git status --porcelain",
>    which also knows the empty-tree trick.  But that command has no way
>    to quit early on the first change.
Previous: Maurice BosNext: Jeff King
Message 2 of 3 in “git-prompt.sh: make '+' work for unborn branches”
  1. git-prompt.sh: make '+' work for unborn branchesMaurice Bos, Mar 5, 2014
  2. Maurice BosMar 6, 2014
  3. Jeff KingMar 27, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.