Skip to content

Commit b28521a

Browse files
authored
CF-815: check if branch is pushed for --file as well (#883)
* also check for file * fix failing tests * refresh lockfile * set no_pr * check if we're in a git repo only when we actually need to create a PR.
1 parent ef69713 commit b28521a

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

codeflash/cli_cmds/cli.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,25 +249,29 @@ def project_root_from_module_root(module_root: Path, pyproject_file_path: Path)
249249

250250

251251
def handle_optimize_all_arg_parsing(args: Namespace) -> Namespace:
252-
if hasattr(args, "all"):
253-
import git
254-
255-
from codeflash.code_utils.git_utils import check_and_push_branch, get_repo_owner_and_name
256-
from codeflash.code_utils.github_utils import require_github_app_or_exit
257-
258-
# Ensure that the user can actually open PRs on the repo.
259-
try:
260-
git_repo = git.Repo(search_parent_directories=True)
261-
except git.exc.InvalidGitRepositoryError:
262-
logger.exception(
263-
"I couldn't find a git repository in the current directory. "
264-
"I need a git repository to run --all and open PRs for optimizations. Exiting..."
265-
)
266-
apologize_and_exit()
267-
if not args.no_pr and not check_and_push_branch(git_repo, git_remote=args.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-
if not args.no_pr:
252+
if hasattr(args, "all") or (hasattr(args, "file") and args.file):
253+
no_pr = getattr(args, "no_pr", False)
254+
255+
if not no_pr:
256+
import git
257+
258+
from codeflash.code_utils.git_utils import check_and_push_branch, get_repo_owner_and_name
259+
from codeflash.code_utils.github_utils import require_github_app_or_exit
260+
261+
# Ensure that the user can actually open PRs on the repo.
262+
try:
263+
git_repo = git.Repo(search_parent_directories=True)
264+
except git.exc.InvalidGitRepositoryError:
265+
mode = "--all" if hasattr(args, "all") else "--file"
266+
logger.exception(
267+
f"I couldn't find a git repository in the current directory. "
268+
f"I need a git repository to run {mode} and open PRs for optimizations. Exiting..."
269+
)
270+
apologize_and_exit()
271+
git_remote = getattr(args, "git_remote", None)
272+
if not check_and_push_branch(git_repo, git_remote=git_remote):
273+
exit_with_message("Branch is not pushed...", error_on_exit=True)
274+
owner, repo = get_repo_owner_and_name(git_repo)
271275
require_github_app_or_exit(owner, repo)
272276
if not hasattr(args, "all"):
273277
args.all = None

tests/test_worktree.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def test_mirror_paths_for_worktree_mode(monkeypatch: pytest.MonkeyPatch):
1515
args = Namespace()
1616
args.benchmark = False
1717
args.benchmarks_root = None
18+
args.no_pr = True
1819

1920
args.config_file = project_root / "pyproject.toml"
2021
args.file = project_root / "src" / "app" / "main.py"
@@ -42,6 +43,7 @@ def test_mirror_paths_for_worktree_mode(monkeypatch: pytest.MonkeyPatch):
4243
args = Namespace()
4344
args.benchmark = False
4445
args.benchmarks_root = None
46+
args.no_pr = True
4547

4648
args.config_file = repo_root / "pyproject.toml"
4749
args.file = repo_root / "codeflash/optimization/optimizer.py"

0 commit comments

Comments
 (0)