Skip to content

Commit

Permalink
Smaller improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Grub4K committed Sep 7, 2024
1 parent da07fb7 commit 4f657f0
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 47 deletions.
2 changes: 1 addition & 1 deletion git_pr_helper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import annotations

__version__ = "1.0.0"
__version__ = "1.1.0"
26 changes: 19 additions & 7 deletions git_pr_helper/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from __future__ import annotations

import contextlib
import inspect
import subprocess
import sys

import rich.text
from rich.console import Console

import git_pr_helper
import git_pr_helper.actions
import git_pr_helper.styles


def _main():
Expand Down Expand Up @@ -44,14 +48,24 @@ def _main():
help="the subcommand to get help for",
)
parsers["help"] = parser
parsers["version"] = subparsers.add_parser("version", help="show version and exit")

args = root_parser.parse_args()
if args.action is None:
root_parser.error("need to provide an action")
elif args.action == "help":
parser = parsers[args.subcommand] if args.subcommand else root_parser
print(parser.format_help())
exit(0)

elif args.action in ("help", "version"):
if args.action == "help":
parser = parsers[args.subcommand] if args.subcommand else root_parser
result = rich.text.Text(parser.format_help())
else:
result = rich.text.Text(
git_pr_helper.__version__, git_pr_helper.styles.ACCENT
)

console = Console()
console.print(result)
sys.exit(0)

runner = ALL_ACTIONS[args.action].run

Expand All @@ -68,10 +82,8 @@ def _main():


def main():
try:
with contextlib.suppress(KeyboardInterrupt):
_main()
except KeyboardInterrupt:
pass


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion git_pr_helper/actions/action_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def run(console: rich.console.Console, args: argparse.Namespace):
f"refs/remotes/{pr_remote}/pr/{pr_number}",
)
]

if not head_hashes:
console.print(
rich.text.Text.assemble(
Expand All @@ -63,7 +64,8 @@ def run(console: rich.console.Console, args: argparse.Namespace):
)
)
return 1
elif len(head_hashes) > 1:

if len(head_hashes) > 1:
console.print(
rich.text.Text.assemble(
("error", styles.ERROR),
Expand All @@ -75,6 +77,7 @@ def run(console: rich.console.Console, args: argparse.Namespace):
)
)
return 1

head_hash, pr_remote = head_hashes[0]
pr_remote = pr_remote.partition("/")[0]

Expand Down
33 changes: 19 additions & 14 deletions git_pr_helper/actions/action_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import rich.table
import rich.text

from git_pr_helper import styles
from git_pr_helper.utils import abbreviate_remote
from git_pr_helper.utils import git
from git_pr_helper.utils import read_pr_branch_infos
Expand Down Expand Up @@ -87,39 +88,43 @@ def run(console: rich.console.Console, args: argparse.Namespace):
remote_config = git("config", "--get-regexp", "remote.*.url")
remotes = {
path.removeprefix("remote.").removesuffix(".url"): abbreviate_remote(remote)
for path, remote in map(str.split, remote_config)
for path, _, remote in (x.partition(" ") for x in remote_config)
}

table = rich.table.Table(box=rich.box.SIMPLE)
table.add_column("", style="bold green")
table.add_column("commit", style="bright_yellow")
table.add_column("local", style="bright_cyan")
table.add_column("remote", style="bright_cyan")
table.add_column("push")
table.add_column("local", style=styles.ACCENT)
table.add_column("pr", style=styles.ACCENT)
table.add_column("remote", style="bright_magenta")
table.add_column("branch", style=styles.ACCENT)
table.add_column("note", style="bright_white")

# TODO(Grub4K): take this from the config actually
default_remote = "upstream"

for branch in branches.values():
head = "*" if branch.is_head else " "
local = rich.text.Text(branch.local)
if branch.ahead_local:
local.append(f" +{branch.ahead_local}", "green")
remote_name, _, pr_number = branch.remote.split("/")
remote = rich.text.Text("")
remote = rich.text.Text()
remote.append(
f"{remote_name}#{pr_number}",
("" if remote_name == default_remote else remote_name) + f"#{pr_number}",
f"link https://github.com/{remotes[remote_name]}/pull/{pr_number}",
)
if branch.ahead_remote:
remote.append(f" +{branch.ahead_remote}", "red")

real_remote = abbreviate_remote(branch.real_remote)
push = rich.text.Text.assemble(
(real_remote, "bright_magenta"),
" @ ",
(branch.real_branch, "bright_cyan"),
table.add_row(
"*" if branch.is_head else " ",
branch.commit_hash,
local,
remote,
abbreviate_remote(branch.real_remote),
branch.real_branch,
branch.description,
)

table.add_row(head, branch.commit_hash, local, remote, push, branch.description)

console.print(table)
return 0
6 changes: 3 additions & 3 deletions git_pr_helper/actions/action_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


# Workaround for `argparse.REMAINDER`
PARSER_ARGS = dict(prefix_chars="`")
PARSER_ARGS = {"prefix_chars": "`"}


def configure_parser(parser: argparse.ArgumentParser):
Expand Down Expand Up @@ -49,9 +49,9 @@ def run(console: rich.console.Console, args: argparse.Namespace):
)
return 1

# TODO: lazy if already up to date
# TODO(Grub4K): lazy if already up to date
git("push", *args.remaining, pr_branch_info.remote, f"HEAD:{pr_branch_info.branch}")

ref_spec = f"refs/pull/{remote_ref}/head:pr/{remote_ref}"
git("fetch", "--update-head-ok", current_remote, ref_spec)
git("fetch", "--all", "--update-head-ok", current_remote, ref_spec)
return 0
8 changes: 4 additions & 4 deletions git_pr_helper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def read_pr_branch_infos(pattern: str | None = None):

descriptions = git("config", "--null", "--get-regexp", filter_pattern)
for description in descriptions:
name, _, description = description.partition("\n")
if not description.startswith(PR_BRANCH_PREFIX):
name, _, part = description.partition("\n")
if not part.startswith(PR_BRANCH_PREFIX):
continue
name = remove_around(name, "branch.", ".description")
_, remote, branch, *description = description.split("\n")
yield name, PrBranchInfo(remote, branch, description)
_, remote, branch, *part = part.split("\n")
yield name, PrBranchInfo(remote, branch, part)


def write_pr_branch_info(name: str, info: PrBranchInfo):
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml → pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ repos:
hooks:
- id: check
name: code check
entry: hatch run check
entry: hatch fmt --check
language: system
types: [python]
51 changes: 35 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ dependencies = [
[project.optional-dependencies]
dev = [
"pre-commit",
"ruff~=0.2.0",
"pyinstaller~=6.0",
"ruff==0.6.*",
"pyinstaller==6.*",
]

[project.urls]
Expand All @@ -55,29 +55,48 @@ path = "git_pr_helper/__init__.py"
packages = ["git_pr_helper"]

[tool.hatch.envs.default]
path = ".venv"
installer = "uv"
features = ["dev"]

[tool.hatch.envs.default.scripts]
install = "pre-commit install"
fix = [
"lint",
"format",
]
lint = "ruff check --fix {args:.}"
format = "ruff format {args:.}"
check = [
"ruff check {args:.}",
"ruff format --check {args:.}",
]
init = "pre-commit install -c pre-commit.yml"
deinit = "pre-commit uninstall"

[tool.hatch.envs.hatch-static-analysis]
dependencies = ["ruff==0.6.*"]
config-path = "pyproject.toml"

[tool.ruff]
line-length = 88

[tool.ruff.lint]
extend-select = [
"I",
select = [
"C4",
"E",
"F",
"I",
"PLC",
"PLE",
"PLW",
"PYI",
"RET",
"RUF",
"SIM",
"TD",
"TID",
"W",
]
ignore = [
"TD003",
"E402",
"E501",
"PLR09",
]

[tool.ruff.lint.isort]
force-single-line = true
required-imports = ["from __future__ import annotations"]
force-single-line = true

[tool.ruff.lint.flake8-tidy-imports]
ban-relative-imports = "all"

0 comments on commit 4f657f0

Please sign in to comment.