Skip to content

Commit

Permalink
implement WT and INDEX deleted files
Browse files Browse the repository at this point in the history
  • Loading branch information
Spitfire1900 committed Mar 28, 2024
1 parent 3ba1818 commit 3af348a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'gitstatus.ahead': <Prompt: gitstatus.ahead>, # DONE
'gitstatus.behind': <Prompt: gitstatus.behind>, # DONE
'gitstatus.untracked': <Prompt: gitstatus.untracked>, # DONE
'gitstatus.changed': <Prompt: gitstatus.changed>, # DONE
'gitstatus.changed': <Prompt: gitstatus.changed>, # PARTIAL
'gitstatus.deleted': <Prompt: gitstatus.deleted>, # DONE
'gitstatus.conflicts': <Prompt: gitstatus.conflicts>, # DONE
'gitstatus.staged': <Prompt: gitstatus.staged>, # DONE
Expand Down
76 changes: 67 additions & 9 deletions xontrib/pygitstatus/prompts.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,64 @@
import contextlib
import os
from typing import Optional
from typing import List, Optional

import pygit2
# pylint: disable=no-name-in-module
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, Diff, GitError)
from pygit2 import (GIT_STATUS_CONFLICTED, GIT_STATUS_INDEX_DELETED,
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, Diff, GitError)
from pygit2 import Repository as Repo
from xonsh.prompt.base import MultiPromptField, PromptField, PromptFields
from xonsh.prompt.gitstatus import operations as gitstatus_operations

### .venv/Lib/site-packages/xonsh/prompt/gitstatus.py


def __git_status_calulator(file_status: int) -> List[int]:
"""
"""
# pylint: disable=pointless-string-statement
"""
GIT_STATUS_WT_UNREADABLE: 4096
GIT_STATUS_WT_RENAMED: 2048
GIT_STATUS_WT_TYPECHANGE: 1024
GIT_STATUS_WT_DELETED: 512
GIT_STATUS_WT_MODIFIED: 256
GIT_STATUS_WT_NEW: 128
GIT_STATUS_INDEX_TYPECHANGE: 16
GIT_STATUS_INDEX_RENAMED: 8
GIT_STATUS_INDEX_DELETED: 4
GIT_STATUS_INDEX_MODIFIED: 2
GIT_STATUS_INDEX_NEW: 1
"""

statuses = []
_file_status_worker = file_status

# pylint: disable=no-member
for status_int in [
pygit2.GIT_STATUS_WT_UNREADABLE, # 4096
pygit2.GIT_STATUS_WT_RENAMED, # 2048
pygit2.GIT_STATUS_WT_TYPECHANGE, # 1024
pygit2.GIT_STATUS_WT_DELETED, # 512
pygit2.GIT_STATUS_WT_MODIFIED, # 256
pygit2.GIT_STATUS_WT_NEW, # 128
pygit2.GIT_STATUS_INDEX_TYPECHANGE, # 16
pygit2.GIT_STATUS_INDEX_RENAMED, # 8
pygit2.GIT_STATUS_INDEX_DELETED, # 4
pygit2.GIT_STATUS_INDEX_MODIFIED, # 2
pygit2.GIT_STATUS_INDEX_NEW, # 1
]:
if _file_status_worker == 0:
break
if _file_status_worker - status_int >= 0:
statuses.append(status_int)
_file_status_worker = _file_status_worker % status_int
return statuses


@PromptField.wrap(prefix='↑·', info='ahead', name='pygitstatus.ahead')
def ahead(fld: PromptField, ctx: PromptFields):
fld.value = ''
Expand Down Expand Up @@ -104,12 +149,25 @@ def curr_branch() -> Optional[str]:
name='pygitstatus.deleted')
def deleted(fld: PromptField, ctx: PromptFields):
fld.value = ''
deleted_count = 0

with contextlib.suppress(GitError):
repo = Repo('.')
untracked_count = len(
[v for k, v in repo.status().items() if v == GIT_STATUS_WT_DELETED])
if untracked_count > 0:
fld.value = str(untracked_count)

for k, v in repo.status().items():
statuses = __git_status_calulator(v)
is_deleted = False
for status in statuses:
if status in [
GIT_STATUS_INDEX_DELETED,
GIT_STATUS_WT_DELETED,
]:
is_deleted = True
break
if is_deleted:
deleted_count = deleted_count + 1
if deleted_count > 0:
fld.value = str(deleted_count)


@PromptField.wrap(prefix="{CYAN}+", suffix="{RESET}", name='pygitstatus.lines_added')
Expand Down

0 comments on commit 3af348a

Please sign in to comment.