Skip to content

Commit

Permalink
implement branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Spitfire1900 committed Mar 27, 2024
1 parent 675c2b0 commit a5882b8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
```python
'gitstatus.repo_path': <Prompt: gitstatus.repo_path>, # DONE
'gitstatus.short_head': <Prompt: gitstatus.short_head>, # DONE
'gitstatus.tag': <Prompt: gitstatus.tag>, # DONE
'gitstatus.tag': <Prompt: gitstatus.tag>, # PARTIAL
'gitstatus.tag_or_hash': <Prompt: gitstatus.tag_or_hash>, # TODO
'gitstatus.stash_count': <Prompt: gitstatus.stash_count>, # TODO
'gitstatus.operations': <Prompt: gitstatus.operations>, # TODO
'gitstatus.porcelain': <Prompt: gitstatus.porcelain>, # TODO
'gitstatus.branch': <Prompt: gitstatus.branch>, # TODO
'gitstatus.branch': <Prompt: gitstatus.branch>, # DONE
'gitstatus.ahead': <Prompt: gitstatus.ahead>, # DONE
'gitstatus.behind': <Prompt: gitstatus.behind>, # DONE
'gitstatus.untracked': <Prompt: gitstatus.untracked>, # TODO
Expand Down
3 changes: 2 additions & 1 deletion xontrib/pygitstatus/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from xonsh.built_ins import XonshSession

from .prompt import ahead, behind, clean, repo_path, short_head, tag
from .prompt import ahead, behind, branch, clean, repo_path, short_head, tag


def _load_xontrib_(xsh: XonshSession, **_):
Expand All @@ -19,6 +19,7 @@ def _load_xontrib_(xsh: XonshSession, **_):
prompt_fields = xsh.env.get('PROMPT_FIELDS') # type: ignore
prompt_fields['pygitstatus.ahead'] = ahead
prompt_fields['pygitstatus.behind'] = behind
prompt_fields['pygitstatus.branch'] = branch
prompt_fields['pygitstatus.clean'] = clean
prompt_fields['pygitstatus.repo_path'] = repo_path
prompt_fields['pygitstatus.short_head'] = short_head
Expand Down
15 changes: 11 additions & 4 deletions xontrib/pygitstatus/prompt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import os

from pygit2 import Commit # pylint: disable=no-name-in-module
from pygit2 import GitError # pylint: disable=no-name-in-module
from pygit2 import Repository as Repo
from xonsh.prompt.base import MultiPromptField, PromptField, PromptFields
Expand All @@ -12,7 +13,7 @@
def ahead(fld: PromptField, ctx: PromptFields):
ahead, behind = (0, 0)
with contextlib.suppress(GitError):
repo = Repo(os.getcwd())
repo = Repo('.')

local_commit = repo.head.target
if local_branch := repo.branches.get(repo.head.shorthand):
Expand All @@ -27,7 +28,7 @@ def ahead(fld: PromptField, ctx: PromptFields):
def behind(fld: PromptField, ctx: PromptFields):
ahead, behind = (0, 0)
with contextlib.suppress(GitError):
repo = Repo(os.getcwd())
repo = Repo('.')

local_commit = repo.head.target
if local_branch := repo.branches.get(repo.head.shorthand):
Expand All @@ -38,6 +39,13 @@ def behind(fld: PromptField, ctx: PromptFields):
fld.value = str(behind) if behind else ''


@PromptField.wrap(prefix="{CYAN}", info="branch")
def branch(fld: PromptField, ctx: PromptFields):
with contextlib.suppress(GitError):
repo = Repo('.')
fld.value = repo.head.shorthand


@PromptField.wrap(prefix="{BOLD_GREEN}", suffix="{RESET}", symbol="✓")
def clean(fld: PromptField, ctx: PromptFields):

Expand All @@ -48,7 +56,7 @@ def clean(fld: PromptField, ctx: PromptFields):
value = ''

with contextlib.suppress(GitError):
repo = Repo(os.getcwd())
repo = Repo('.')
if len(repo.status()) == 0:
value = symbol

Expand Down Expand Up @@ -76,7 +84,6 @@ def short_head(fld: PromptField, ctx: PromptFields):

@PromptField.wrap()
def tag(fld: PromptField, ctx: PromptFields):
from pygit2 import Commit # pylint: disable=no-name-in-module
with contextlib.suppress(GitError):
repo = Repo('.')
head_commit = repo.get(repo.head.target)
Expand Down

0 comments on commit a5882b8

Please sign in to comment.