Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add spaceship status #254

Merged
merged 3 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,19 @@ For example, the default setting corresponds to
branch,commit_msg,commit_time
```

Here `branch` includes both branch name and status. To get the branch name alone, use `branch_name`.
Here `branch` includes both branch name and status.

Another choice is to enable `spaceship_status`, which mimics
the symbols used in [spaceship-prompt](https://spaceship-prompt.sh/sections/git/#Git-status-git_status).
For example,

symbol | meaning
---|---
∅| local has no remote
| local is the same as remote
⇕ | local has diverged from remote
⇡| local is ahead of remote (good for push)
⇣| local is behind remote (good for merge)


### customize git command flags
Expand Down
19 changes: 13 additions & 6 deletions gita/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,6 @@ def main(argv=None):
"--directory",
help="Change to DIRECTORY before doing anything.",
)
p_clone.add_argument(
"-f",
"--from-file",
action="store_true",
help="If set, clone repos in a config file rendered from `gita freeze`",
)
p_clone.add_argument(
"-p",
"--preserve-path",
Expand All @@ -515,6 +509,19 @@ def main(argv=None):
action="store_true",
help="If set, show command without execution",
)
xgroup = p_clone.add_mutually_exclusive_group()
xgroup.add_argument(
"-g",
"--group",
choices=utils.get_groups(),
help="If set, add repo to the specified group after cloning, otherwise add to gita without group.",
)
xgroup.add_argument(
"-f",
"--from-file",
action="store_true",
help="If set, clone repos in a config file rendered from `gita freeze`",
)
p_clone.set_defaults(func=f_clone)

p_rename = subparsers.add_parser(
Expand Down
52 changes: 36 additions & 16 deletions gita/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def get_info_funcs(no_colors=False) -> List[Callable[[str], str]]:
all_info_items = {
"branch": partial(get_repo_status, no_colors=no_colors),
"branch_name": get_repo_branch,
"spaceship_status": get_spaceship_status,
"commit_msg": get_commit_msg,
"commit_time": get_commit_time,
"path": get_path,
Expand Down Expand Up @@ -197,18 +198,39 @@ def get_commit_time(prop: Dict[str, str]) -> str:

def get_repo_status(prop: Dict[str, str], no_colors=False) -> str:
head = get_head(prop["path"])
dirty, staged, untracked, color = _get_repo_status(prop, no_colors)
colors = {situ: Color[name].value for situ, name in get_color_encoding().items()}
dirty, staged, untracked, situ = _get_repo_status(prop, skip_situ=no_colors)
info = f"{head:<10} [{dirty+staged+untracked}]"
if color:
return f"{color}{info:<17}{Color.end}"
return f"{info:<17}"

if no_colors:
return f"{info:<17}"
color = colors[situ]
return f"{color}{info:<17}{Color.end}"


spaceship = {
"local-ahead": "⇡",
"remote-ahead": "⇣",
"diverged": "⇕",
"in-sync": "",
"no-remote": "∅",
}


def get_spaceship_status(prop: Dict[str, str]) -> str:
head = get_head(prop["path"])
dirty, staged, untracked, situ = _get_repo_status(prop)
info = f"{head:<10} [{dirty+staged+untracked+spaceship[situ]}]"
return f"{info:<18}"


def get_repo_branch(prop: Dict[str, str]) -> str:
return get_head(prop["path"])


def _get_repo_status(prop: Dict[str, str], no_colors: bool) -> Tuple[str]:
def _get_repo_status(
prop: Dict[str, str], skip_situ=False
) -> Tuple[str, str, str, str]:
"""
Return the status of one repo
"""
Expand All @@ -218,31 +240,29 @@ def _get_repo_status(prop: Dict[str, str], no_colors: bool) -> Tuple[str]:
staged = "+" if run_quiet_diff(flags, ["--cached"], path) else ""
untracked = "?" if has_untracked(flags, path) else ""

if no_colors:
if skip_situ:
return dirty, staged, untracked, ""

colors = {situ: Color[name].value for situ, name in get_color_encoding().items()}
diff_returncode = run_quiet_diff(flags, ["@{u}", "@{0}"], path)
has_no_remote = diff_returncode == 128
has_no_diff = diff_returncode == 0
if has_no_remote:
color = colors["no-remote"]
elif has_no_diff:
color = colors["in-sync"]
if diff_returncode == 128:
situ = "no-remote"
elif diff_returncode == 0:
situ = "in-sync"
else:
common_commit = get_common_commit(path)
outdated = run_quiet_diff(flags, ["@{u}", common_commit], path)
if outdated:
diverged = run_quiet_diff(flags, ["@{0}", common_commit], path)
color = colors["diverged"] if diverged else colors["remote-ahead"]
situ = "diverged" if diverged else "remote-ahead"
else: # local is ahead of remote
color = colors["local-ahead"]
return dirty, staged, untracked, color
situ = "local-ahead"
return dirty, staged, untracked, situ


ALL_INFO_ITEMS = {
"branch",
"branch_name",
"spaceship_status",
"commit_msg",
"commit_time",
"path",
Expand Down
9 changes: 5 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ def test_ls(self, monkeypatch, capfd):
[
(
PATH_FNAME,
"repo1 cmaster [dsu] \x1b[0m msg \nrepo2 cmaster [dsu] \x1b[0m msg \nxxx cmaster [dsu] \x1b[0m msg \n",
"repo1 \x1b[31mmaster [dsu] \x1b[0m msg \nrepo2 \x1b[31mmaster [dsu] \x1b[0m msg \nxxx \x1b[31mmaster [dsu] \x1b[0m msg \n",
),
(PATH_FNAME_EMPTY, ""),
(
PATH_FNAME_CLASH,
"repo1 cmaster [dsu] \x1b[0m msg \nrepo2 cmaster [dsu] \x1b[0m msg \n",
"repo1 \x1b[31mmaster [dsu] \x1b[0m msg \nrepo2 \x1b[31mmaster [dsu] \x1b[0m msg \n",
),
],
)
@patch("gita.utils.is_git", return_value=True)
@patch("gita.info.get_head", return_value="master")
@patch("gita.info._get_repo_status", return_value=("d", "s", "u", "c"))
@patch("gita.info._get_repo_status", return_value=("d", "s", "u", "diverged"))
@patch("gita.info.get_commit_msg", return_value="msg")
@patch("gita.info.get_commit_time", return_value="")
@patch("gita.common.get_config_fname")
Expand Down Expand Up @@ -532,7 +532,8 @@ def test_ll(self, _, capfd):
__main__.f_info(args)
out, err = capfd.readouterr()
assert (
"In use: branch,commit_msg,commit_time\nUnused: branch_name,path\n" == out
"In use: branch,commit_msg,commit_time\nUnused: branch_name,path,spaceship_status\n"
== out
)
assert err == ""

Expand Down