diff --git a/lazy_github/lib/context.py b/lazy_github/lib/context.py index c876ec2..697dd66 100644 --- a/lazy_github/lib/context.py +++ b/lazy_github/lib/context.py @@ -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 @@ -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 @@ -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]: diff --git a/lazy_github/lib/git_cli.py b/lazy_github/lib/git_cli.py index fb54e64..8a1e12f 100644 --- a/lazy_github/lib/git_cli.py +++ b/lazy_github/lib/git_cli.py @@ -1,8 +1,6 @@ import subprocess import re -from dataclasses import dataclass - # Regex designed to match git@github.com:gizmo385/lazy-github.git: # ".+:" Match everything to the first colon # "([^\/]+)" Match everything until the forward slash, which should be owner @@ -12,16 +10,8 @@ _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: @@ -29,4 +19,15 @@ def current_directory_git_repo_remote_owner(remote: str = "origin") -> LocalGitR 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()) diff --git a/lazy_github/ui/widgets/repositories.py b/lazy_github/ui/widgets/repositories.py index 7704d9f..13c114e 100644 --- a/lazy_github/ui/widgets/repositories.py +++ b/lazy_github/ui/widgets/repositories.py @@ -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()