From a5882b8695d65154daae5dfb4ab8ba766cc76380 Mon Sep 17 00:00:00 2001 From: Kyle Gottfried <6462596+Spitfire1900@users.noreply.github.com> Date: Wed, 27 Mar 2024 19:56:36 -0400 Subject: [PATCH] implement branch --- TODO.md | 4 ++-- xontrib/pygitstatus/entrypoint.py | 3 ++- xontrib/pygitstatus/prompt.py | 15 +++++++++++---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index cfc035e..c371313 100644 --- a/TODO.md +++ b/TODO.md @@ -1,12 +1,12 @@ ```python 'gitstatus.repo_path': , # DONE 'gitstatus.short_head': , # DONE -'gitstatus.tag': , # DONE +'gitstatus.tag': , # PARTIAL 'gitstatus.tag_or_hash': , # TODO 'gitstatus.stash_count': , # TODO 'gitstatus.operations': , # TODO 'gitstatus.porcelain': , # TODO -'gitstatus.branch': , # TODO +'gitstatus.branch': , # DONE 'gitstatus.ahead': , # DONE 'gitstatus.behind': , # DONE 'gitstatus.untracked': , # TODO diff --git a/xontrib/pygitstatus/entrypoint.py b/xontrib/pygitstatus/entrypoint.py index 5bf1aff..e313639 100644 --- a/xontrib/pygitstatus/entrypoint.py +++ b/xontrib/pygitstatus/entrypoint.py @@ -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, **_): @@ -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 diff --git a/xontrib/pygitstatus/prompt.py b/xontrib/pygitstatus/prompt.py index f69a5e7..b1c7a3e 100644 --- a/xontrib/pygitstatus/prompt.py +++ b/xontrib/pygitstatus/prompt.py @@ -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 @@ -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): @@ -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): @@ -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): @@ -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 @@ -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)