Skip to content

Commit

Permalink
add stashed status (#259)
Browse files Browse the repository at this point in the history
* add stashed status

* fix unit tests
  • Loading branch information
nosarthur authored Jul 24, 2023
1 parent 318ee19 commit f5607ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
24 changes: 20 additions & 4 deletions gita/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ def has_untracked(flags: List[str], path) -> bool:
return bool(result.stdout)


def has_stashed(flags: List[str], path) -> bool:
"""
Return True if stashed content exists
"""
# FIXME: this doesn't work for repos like worktrees, bare, etc
p = Path(path) / ".git" / "logs" / "refs" / "stash"
got = False
try:
got = p.is_file()
except Exception:
pass
return got


def get_commit_msg(prop: Dict[str, str]) -> str:
"""
Return the last commit message.
Expand Down Expand Up @@ -199,6 +213,7 @@ def get_commit_time(prop: Dict[str, str]) -> str:
"dirty": "*",
"staged": "+",
"untracked": "?",
"stashed": "$",
"local_ahead": "↑",
"remote_ahead": "↓",
"diverged": "⇕",
Expand All @@ -225,9 +240,9 @@ def get_symbols() -> Dict[str, str]:

def get_repo_status(prop: Dict[str, str], no_colors=False) -> str:
branch = get_head(prop["path"])
dirty, staged, untracked, situ = _get_repo_status(prop)
dirty, staged, untracked, stashed, situ = _get_repo_status(prop)
symbols = get_symbols()
info = f"{branch:<10} [{symbols[dirty]+symbols[staged]+symbols[untracked]+symbols[situ]}]"
info = f"{branch:<10} [{symbols[dirty]}{symbols[staged]}{symbols[stashed]}{symbols[untracked]}{symbols[situ]}]"

if no_colors:
return f"{info:<18}"
Expand All @@ -240,7 +255,7 @@ def get_repo_branch(prop: Dict[str, str]) -> str:
return get_head(prop["path"])


def _get_repo_status(prop: Dict[str, str]) -> Tuple[str, str, str, str]:
def _get_repo_status(prop: Dict[str, str]) -> Tuple[str, str, str, str, str]:
"""
Return the status of one repo
"""
Expand All @@ -249,6 +264,7 @@ def _get_repo_status(prop: Dict[str, str]) -> Tuple[str, str, str, str]:
dirty = "dirty" if run_quiet_diff(flags, [], path) else ""
staged = "staged" if run_quiet_diff(flags, ["--cached"], path) else ""
untracked = "untracked" if has_untracked(flags, path) else ""
stashed = "stashed" if has_stashed(flags, path) else ""

diff_returncode = run_quiet_diff(flags, ["@{u}", "@{0}"], path)
if diff_returncode == 128:
Expand All @@ -263,7 +279,7 @@ def _get_repo_status(prop: Dict[str, str]) -> Tuple[str, str, str, str]:
situ = "diverged" if diverged else "remote_ahead"
else: # local is ahead of remote
situ = "local_ahead"
return dirty, staged, untracked, situ
return dirty, staged, untracked, stashed, situ


ALL_INFO_ITEMS = {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
setup(
name="gita",
packages=["gita"],
version="0.16.6.3",
version="0.16.6.4",
license="MIT",
description="Manage multiple git repos with sanity",
long_description=long_description,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_ls(self, monkeypatch, capfd):
@patch("gita.info.get_head", return_value="master")
@patch(
"gita.info._get_repo_status",
return_value=("dirty", "staged", "untracked", "diverged"),
return_value=("dirty", "staged", "untracked", "", "diverged"),
)
@patch("gita.info.get_commit_msg", return_value="msg")
@patch("gita.info.get_commit_time", return_value="")
Expand Down

0 comments on commit f5607ac

Please sign in to comment.