Skip to content

Commit

Permalink
Auto select current directory repo
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo385 committed Sep 14, 2024
1 parent e5b6084 commit 32787b1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
10 changes: 7 additions & 3 deletions lazy_github/lib/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from lazy_github.lib.config import Config
from lazy_github.lib.constants import JSON_CONTENT_ACCEPT_TYPE
from lazy_github.lib.git_cli import LocalGitRepo, current_directory_git_repo_remote_owner
from lazy_github.lib.git_cli import current_local_repo_full_name
from lazy_github.lib.github.auth import token
from lazy_github.lib.github.client import GithubClient
from lazy_github.lib.utils import classproperty
Expand All @@ -15,6 +15,7 @@ class LazyGithubContext:
# Attributes exposed via properties
_config: Config | None = None
_client: GithubClient | None = None
_current_directory_repo: str | None = None

# Directly assigned attributes
current_repo: Repository | None = None
Expand All @@ -33,8 +34,11 @@ def client(cls) -> GithubClient:
return cls._client

@classproperty
def current_directory_repo(cls) -> LocalGitRepo | None:
return current_directory_git_repo_remote_owner()
def current_directory_repo(cls) -> str | None:
"""The owner/name of the repo associated with the current working directory (if one exists)"""
if not cls._current_directory_repo:
cls._current_directory_repo = current_local_repo_full_name()
return cls._current_directory_repo


def github_headers(accept: str = JSON_CONTENT_ACCEPT_TYPE, cache_duration: Optional[int] = None) -> dict[str, str]:
Expand Down
27 changes: 14 additions & 13 deletions lazy_github/lib/git_cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import subprocess
import re

from dataclasses import dataclass

# Regex designed to match [email protected]:gizmo385/lazy-github.git:
# ".+:" Match everything to the first colon
# "([^\/]+)" Match everything until the forward slash, which should be owner
Expand All @@ -12,21 +10,24 @@
_GIT_REMOTE_REGEX = re.compile(r".+:([^\/]+)\/([^.]+).git")


@dataclass
class LocalGitRepo:
owner: str
name: str


def current_directory_git_repo_remote_owner(remote: str = "origin") -> LocalGitRepo | None:
"""
Returns the name and owner associated with the remote of the git repo in the current working directory.
"""
def current_local_repo_full_name(remote: str = "origin") -> str | None:
"""Returns the owner/name associated with the remote of the git repo in the current working directory."""
try:
output = subprocess.check_output(["git", "remote", "get-url", remote]).decode().strip()
except subprocess.SubprocessError:
return None

if matches := re.match(_GIT_REMOTE_REGEX, output):
owner, name = matches.groups()
return LocalGitRepo(owner, name)
return f"{owner}/{name}"


def current_local_branch_name() -> str | None:
"""Returns the name of the current branch for the git repo in the current working directory."""
try:
return subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode().strip()
except subprocess.SubprocessError:
return None


print(current_local_branch_name())
6 changes: 6 additions & 0 deletions lazy_github/ui/widgets/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ async def add_repos_to_table(self, repos: Iterable[Repository]) -> None:
self.repos[repo.full_name] = repo
self.searchable_table.add_rows(rows)

# If the current user's directory is a git repo and they don't already have a git repo selected, try and mark
# that repo as the current repo
if LazyGithubContext.current_directory_repo and not LazyGithubContext.current_repo:
if repo := self.repos.get(LazyGithubContext.current_directory_repo):
self.post_message(RepoSelected(repo))

@work
async def load_repos(self) -> None:
repos = await repos_api.list_all()
Expand Down

0 comments on commit 32787b1

Please sign in to comment.