From d7dd6542a8d43ad72d770a49ce099853515a72f7 Mon Sep 17 00:00:00 2001 From: Kyle Gottfried <6462596+Spitfire1900@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:06:13 -0400 Subject: [PATCH] implement conflicts --- TODO.md | 2 +- xontrib/pygitstatus/entrypoint.py | 7 ++++--- xontrib/pygitstatus/prompts.py | 19 +++++++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/TODO.md b/TODO.md index 96aa040..59bed40 100644 --- a/TODO.md +++ b/TODO.md @@ -12,7 +12,7 @@ 'gitstatus.untracked': , # DONE 'gitstatus.changed': , # DONE 'gitstatus.deleted': , # DONE -'gitstatus.conflicts': , # TODO +'gitstatus.conflicts': , # DONE 'gitstatus.staged': , # DONE 'gitstatus.numstat': , # TODO 'gitstatus.lines_added': , # TODO diff --git a/xontrib/pygitstatus/entrypoint.py b/xontrib/pygitstatus/entrypoint.py index 972a102..98386e6 100644 --- a/xontrib/pygitstatus/entrypoint.py +++ b/xontrib/pygitstatus/entrypoint.py @@ -2,9 +2,9 @@ from xonsh.built_ins import XonshSession -from .prompts import (ahead, behind, branch, changed, clean, curr_branch, deleted, - repo_path, short_head, staged, stash_count, tag, tag_or_hash, - untracked) +from .prompts import (ahead, behind, branch, changed, clean, conflict, curr_branch, + deleted, repo_path, short_head, staged, stash_count, tag, + tag_or_hash, untracked) def _load_xontrib_(xsh: XonshSession, **_) -> dict: @@ -34,6 +34,7 @@ def _load_xontrib_(xsh: XonshSession, **_) -> dict: prompt_fields['pygitstatus.branch'] = branch prompt_fields['pygitstatus.changed'] = changed prompt_fields['pygitstatus.clean'] = clean + prompt_fields['pygitstatus.conflicts'] = conflict prompt_fields['pygitstatus.deleted'] = deleted prompt_fields['pygitstatus.repo_path'] = repo_path prompt_fields['pygitstatus.short_head'] = short_head diff --git a/xontrib/pygitstatus/prompts.py b/xontrib/pygitstatus/prompts.py index 2b342d6..ae730a2 100644 --- a/xontrib/pygitstatus/prompts.py +++ b/xontrib/pygitstatus/prompts.py @@ -3,10 +3,10 @@ from typing import Optional # pylint: disable=no-name-in-module -from pygit2 import (GIT_STATUS_INDEX_MODIFIED, GIT_STATUS_INDEX_NEW, - GIT_STATUS_INDEX_RENAMED, GIT_STATUS_INDEX_TYPECHANGE, - GIT_STATUS_WT_DELETED, GIT_STATUS_WT_MODIFIED, GIT_STATUS_WT_NEW, - Commit, GitError) +from pygit2 import (GIT_STATUS_CONFLICTED, GIT_STATUS_INDEX_MODIFIED, + GIT_STATUS_INDEX_NEW, GIT_STATUS_INDEX_RENAMED, + GIT_STATUS_INDEX_TYPECHANGE, GIT_STATUS_WT_DELETED, + GIT_STATUS_WT_MODIFIED, GIT_STATUS_WT_NEW, Commit, GitError) from pygit2 import Repository as Repo from xonsh.prompt.base import MultiPromptField, PromptField, PromptFields @@ -70,6 +70,17 @@ def changed(fld: PromptField, ctx: PromptFields): fld.value = str(untracked_count) +@PromptField.wrap(prefix="{RED}-", suffix="{RESET}", info="deleted") +def conflict(fld: PromptField, ctx: PromptFields): + fld.value = '' + with contextlib.suppress(GitError): + repo = Repo('.') + conflicted_count = len( + [v for k, v in repo.status().items() if v == GIT_STATUS_CONFLICTED]) + if conflicted_count > 0: + fld.value = str(conflicted_count) + + @PromptField.wrap(prefix="{RED}-", suffix="{RESET}", info="deleted") def deleted(fld: PromptField, ctx: PromptFields): fld.value = ''