From 30e2668c589e319fc0028f835e2a30fb254733cf Mon Sep 17 00:00:00 2001 From: gizmo385 Date: Tue, 17 Dec 2024 23:58:06 +0000 Subject: [PATCH] Default new PR head to current branch --- lazy_github/lib/context.py | 10 +++++++++- lazy_github/ui/screens/new_pull_request.py | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lazy_github/lib/context.py b/lazy_github/lib/context.py index e41741e..cb38d11 100644 --- a/lazy_github/lib/context.py +++ b/lazy_github/lib/context.py @@ -3,7 +3,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 current_local_repo_full_name +from lazy_github.lib.git_cli import current_local_branch_name, current_local_repo_full_name from lazy_github.lib.github.backends.protocol import BackendType from lazy_github.lib.github.client import GithubClient from lazy_github.lib.logging import LazyGithubLogFormatter, lg @@ -18,6 +18,7 @@ class LazyGithubContext: _config: Config | None = None _client: GithubClient | None = None _current_directory_repo: str | None = None + _current_directory_branch: str | None = None # Directly assigned attributes current_repo: Repository | None = None @@ -65,6 +66,13 @@ def current_directory_repo(cls) -> str | None: cls._current_directory_repo = current_local_repo_full_name() return cls._current_directory_repo + @classproperty + def current_directory_branch(cls) -> str | None: + """The owner/name of the repo associated with the current working directory (if one exists)""" + if not cls._current_directory_branch: + cls._current_directory_branch = current_local_branch_name() + return cls._current_directory_branch + def github_headers(accept: str = JSON_CONTENT_ACCEPT_TYPE, cache_duration: Optional[int] = None) -> dict[str, str]: """Helper function to build headers for Github API requests""" diff --git a/lazy_github/ui/screens/new_pull_request.py b/lazy_github/ui/screens/new_pull_request.py index a7369eb..481927d 100644 --- a/lazy_github/ui/screens/new_pull_request.py +++ b/lazy_github/ui/screens/new_pull_request.py @@ -64,8 +64,15 @@ def head_ref(self) -> str: def base_ref(self) -> str: return self._base_ref_input.value + @work + async def set_default_branch_value(self) -> None: + if LazyGithubContext.current_directory_repo == LazyGithubContext.current_repo.full_name: + if LazyGithubContext.current_directory_branch: + self.query_one("#head_ref", Input).value = LazyGithubContext.current_directory_branch + async def on_mount(self) -> None: self.fetch_branches() + self.set_default_branch_value() @on(BranchesLoaded) def handle_loaded_branches(self, message: BranchesLoaded) -> None: