Repository: xonsh/xonsh

Issue:
{gitstatus: {}} is no longer autohiding gitstatus
`{gitstatus: {}}` in my right prompt no longer hides itself when I'm not in a git repository.

## xonfig

<details>

```
$ xonfig
+------------------+---------------------+
| xonsh            | 0.13.0              |
| Python           | 3.10.5              |
| PLY              | 3.11                |
| have readline    | True                |
| prompt toolkit   | 3.0.30              |
| shell type       | prompt_toolkit      |
| history backend  | sqlite              |
| pygments         | 2.12.0              |
| on posix         | True                |
| on linux         | True                |
| distro           | unknown             |
| on wsl           | False               |
| on darwin        | False               |
| on windows       | False               |
| on cygwin        | False               |
| on msys2         | False               |
| is superuser     | False               |
| default encoding | utf-8               |
| xonsh encoding   | utf-8               |
| encoding errors  | surrogateescape     |
| xontrib          | []                  |
| RC file 1        | /home/monk/.xonshrc |
+------------------+---------------------+
```
</details>

## Expected Behavior
```
$ xonsh --no-rc
monk@lychee ~ $ $RIGHT_PROMPT='<{gitstatus: {}}>'
monk@lychee ~ $ $PROMPT_FIELDS['gitstatus'].fragments = ()                    <>
monk@lychee ~ $                                                               <>
```

## Current Behavior
```
$ xonsh --no-rc
monk@lychee ~ $ $RIGHT_PROMPT='<{gitstatus: {}}>'
monk@lychee ~ $ $PROMPT_FIELDS['gitstatus'].fragments = ()                 < |✓>
monk@lychee ~ $                                                              < >
```

## Steps to Reproduce
see above

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**



Discussion:
Related to #4697
@jnoortheen please take a look

Resolving patch:
diff --git a/xonsh/prompt/gitstatus.py b/xonsh/prompt/gitstatus.py
--- a/xonsh/prompt/gitstatus.py
+++ b/xonsh/prompt/gitstatus.py
@@ -319,5 +319,11 @@ def get_frags(self, env):
                 continue
             yield frag
 
+    def _collect(self, ctx):
+        if not ctx.pick_val(repo_path):
+            # no need to display any other fragments
+            return
+        yield from super()._collect(ctx)
+
 
 gitstatus = GitStatus()


Test patch:
diff --git a/tests/prompt/test_gitstatus.py b/tests/prompt/test_gitstatus.py
--- a/tests/prompt/test_gitstatus.py
+++ b/tests/prompt/test_gitstatus.py
@@ -1,3 +1,5 @@
+import os
+
 import pytest
 
 from xonsh.prompt import gitstatus
@@ -14,6 +16,7 @@ def prompts(xession):
     fields = xession.env["PROMPT_FIELDS"]
     yield fields
     fields.clear()
+    fields.reset()
 
 
 @pytest.fixture
@@ -71,3 +74,24 @@ def test_gitstatus_clean(prompts, fake_proc):
     assert format(prompts.pick("gitstatus")) == exp
     assert _format_value(prompts.pick("gitstatus"), None, None) == exp
     assert _format_value(prompts.pick("gitstatus"), "{}", None) == exp
+
+
+def test_no_git(prompts, fake_process, tmp_path):
+    os.chdir(tmp_path)
+    err = b"fatal: not a git repository (or any of the parent directories): .git"
+    for cmd in (
+        "git status --porcelain --branch",
+        "git rev-parse --git-dir",
+        "git diff --numstat",
+    ):
+        fake_process.register_subprocess(
+            command=cmd,
+            stderr=err,
+            returncode=128,
+        )
+
+    exp = ""
+    assert prompts.pick_val("gitstatus.repo_path") == ""
+    assert format(prompts.pick("gitstatus")) == exp
+    assert _format_value(prompts.pick("gitstatus"), None, None) == exp
+    assert _format_value(prompts.pick("gitstatus"), "{}", None) == exp