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:
github-actions[bot]: **Warning!** No news item is found for this PR.
If this is an user facing change/feature/fix, please add a news item by copying the format from `news/TEMPLATE.rst`.


Patch:
diff --git a/news/fix-empty-gitstatus.rst b/news/fix-empty-gitstatus.rst
new file mode 100644
index 00000000..3a920da1
--- /dev/null
+++ b/news/fix-empty-gitstatus.rst
@@ -0,0 +1,23 @@
+**Added:**
+
+* <news item>
+
+**Changed:**
+
+* <news item>
+
+**Deprecated:**
+
+* <news item>
+
+**Removed:**
+
+* <news item>
+
+**Fixed:**
+
+* ``gitstatus`` Prompt-field would be empty on paths without git setup.
+
+**Security:**
+
+* <news item>
diff --git a/xonsh/prompt/gitstatus.py b/xonsh/prompt/gitstatus.py
index 683d8255..e3a41ca8 100644
--- a/xonsh/prompt/gitstatus.py
+++ b/xonsh/prompt/gitstatus.py
@@ -319,5 +319,11 @@ class GitStatus(MultiPromptField):
                 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
index 9e13628b..ce20044e 100644
--- 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