Skip to content

Commit 357d502

Browse files
committed
improve messaging
1 parent 9cc7e2e commit 357d502

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

codeflash/api/aiservice.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import time
77
from typing import TYPE_CHECKING, Any, cast
88

9+
import git
910
import requests
1011
from pydantic.json import pydantic_encoder
1112

@@ -637,7 +638,14 @@ def get_aiservice_base_url(self) -> str:
637638
def safe_get_repo_owner_and_name() -> tuple[str | None, str | None]:
638639
try:
639640
git_repo_owner, git_repo_name = get_repo_owner_and_name()
641+
except (ValueError, git.exc.InvalidGitRepositoryError):
642+
# Expected errors when:
643+
# - No remotes are configured (e.g., local-only repos) -> ValueError
644+
# - Not in a git repository -> InvalidGitRepositoryError
645+
# These are normal and shouldn't be logged as warnings
646+
git_repo_owner, git_repo_name = None, None
640647
except Exception as e:
648+
# Unexpected errors should still be logged
641649
logger.warning(f"Could not determine repo owner and name: {e}")
642650
git_repo_owner, git_repo_name = None, None
643651
return git_repo_owner, git_repo_name

codeflash/cli_cmds/cli.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,15 @@ def process_and_validate_cmd_args(args: Namespace) -> Namespace:
143143
exit_with_message(f"File {args.file} does not exist", error_on_exit=True)
144144
args.file = Path(args.file).resolve()
145145
if not args.no_pr:
146-
owner, repo = get_repo_owner_and_name()
147-
require_github_app_or_exit(owner, repo)
146+
try:
147+
owner, repo = get_repo_owner_and_name()
148+
require_github_app_or_exit(owner, repo)
149+
except ValueError as e:
150+
exit_with_message(
151+
f"Cannot determine repository information: {e}\n"
152+
f"Please ensure your git repository has a remote configured, or use '--no-pr' to optimize locally.",
153+
error_on_exit=True
154+
)
148155
if args.replay_test:
149156
for test_path in args.replay_test:
150157
if not Path(test_path).is_file():
@@ -265,9 +272,16 @@ def handle_optimize_all_arg_parsing(args: Namespace) -> Namespace:
265272
apologize_and_exit()
266273
git_remote = getattr(args, "git_remote", None)
267274
if not check_and_push_branch(git_repo, git_remote=git_remote):
268-
exit_with_message("Branch is not pushed...", error_on_exit=True)
269-
owner, repo = get_repo_owner_and_name(git_repo)
270-
require_github_app_or_exit(owner, repo)
275+
exit_with_message("Cannot proceed without a valid git remote configuration. See error message above.", error_on_exit=True)
276+
try:
277+
owner, repo = get_repo_owner_and_name(git_repo)
278+
require_github_app_or_exit(owner, repo)
279+
except ValueError as e:
280+
exit_with_message(
281+
f"Cannot determine repository information: {e}\n"
282+
f"Please ensure your git repository has a remote configured, or use '--no-pr' to optimize locally.",
283+
error_on_exit=True
284+
)
271285
if not hasattr(args, "all"):
272286
args.all = None
273287
elif args.all == "":

codeflash/code_utils/git_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ def get_current_branch(repo: Repo | None = None) -> str:
8080

8181
def get_remote_url(repo: Repo | None = None, git_remote: str | None = "origin") -> str:
8282
repository: Repo = repo if repo else git.Repo(search_parent_directories=True)
83+
available_remotes = get_git_remotes(repository)
84+
if not available_remotes:
85+
raise ValueError(f"No git remotes configured in this repository")
86+
if git_remote not in available_remotes:
87+
raise ValueError(f"Git remote '{git_remote}' does not exist. Available remotes: {', '.join(available_remotes)}")
8388
return repository.remote(name=git_remote).url
8489

8590

@@ -128,6 +133,30 @@ def confirm_proceeding_with_no_git_repo() -> str | bool:
128133
def check_and_push_branch(repo: git.Repo, git_remote: str | None = "origin", *, wait_for_push: bool = False) -> bool:
129134
current_branch = repo.active_branch
130135
current_branch_name = current_branch.name
136+
available_remotes = get_git_remotes(repo)
137+
if not available_remotes:
138+
logger.error(
139+
f"❌ No git remotes configured in this repository.\n"
140+
f"This appears to be a local-only git repository. To use codeflash with PR features, you need to:\n"
141+
f" 1. Create a repository on GitHub (or another git hosting service)\n"
142+
f" 2. Add it as a remote: git remote add origin <repository-url>\n"
143+
f" 3. Push your branch: git push -u origin {current_branch_name}\n\n"
144+
f"Alternatively, you can run codeflash with the '--no-pr' flag to optimize locally without creating PRs."
145+
)
146+
return False
147+
148+
# Check if the specified remote exists
149+
if git_remote not in available_remotes:
150+
logger.error(
151+
f"❌ Git remote '{git_remote}' does not exist in this repository.\n"
152+
f"Available remotes: {', '.join(available_remotes)}\n\n"
153+
f"You can either:\n"
154+
f" 1. Use one of the existing remotes by setting 'git-remote' in pyproject.toml\n"
155+
f" 2. Add the '{git_remote}' remote: git remote add {git_remote} <repository-url>\n"
156+
f" 3. Run codeflash with '--no-pr' to optimize locally without creating PRs."
157+
)
158+
return False
159+
131160
remote = repo.remote(name=git_remote)
132161

133162
# Check if the branch is pushed

codeflash/discovery/functions_to_optimize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ def was_function_previously_optimized(
598598
# already_optimized_count = 0
599599
try:
600600
owner, repo = get_repo_owner_and_name()
601-
except git.exc.InvalidGitRepositoryError:
602-
logger.warning("No git repository found")
601+
except (git.exc.InvalidGitRepositoryError, ValueError) as e:
602+
logger.debug(f"Cannot get repository info: {e}")
603603
owner, repo = None, None
604604
pr_number = get_pr_number()
605605

0 commit comments

Comments
 (0)