Skip to content

Commit 4b95135

Browse files
committed
when branch is not specified, defaults to active branch.
1 parent 3794ffc commit 4b95135

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

commit0/cli.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import commit0.harness.lint
1111
import commit0.harness.save
1212
from commit0.harness.constants import SPLIT, SPLIT_ALL
13+
from commit0.harness.utils import get_active_branch
1314
import subprocess
1415
import yaml
1516
import os
@@ -245,14 +246,11 @@ def test(
245246

246247
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
247248

248-
if not branch and not reference:
249-
raise typer.BadParameter(
250-
f"Invalid {highlight('BRANCH', Colors.RED)}. Either --reference or provide a branch name.",
251-
param_hint="BRANCH",
252-
)
253249
if reference:
254250
branch = "reference"
255-
assert branch is not None, "branch is not specified"
251+
if branch is None and not reference:
252+
git_path = os.path.join(commit0_config["base_dir"], repo_or_repo_path.split("/")[-1])
253+
branch = get_active_branch(git_path)
256254

257255
if verbose == 2:
258256
typer.echo(f"Running tests for repository: {repo_or_repo_path}")
@@ -294,14 +292,8 @@ def evaluate(
294292
) -> None:
295293
"""Evaluate Commit0 split you choose in Setup Stage."""
296294
check_commit0_path()
297-
if not branch and not reference:
298-
raise typer.BadParameter(
299-
f"Invalid {highlight('BRANCH', Colors.RED)}. Either --reference or provide a branch name",
300-
param_hint="BRANCH",
301-
)
302295
if reference:
303296
branch = "reference"
304-
assert branch is not None, "branch is not specified"
305297

306298
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
307299
check_valid(commit0_config["repo_split"], SPLIT)

commit0/harness/evaluate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from commit0.harness.run_pytest_ids import main as run_tests
1111
from commit0.harness.get_pytest_ids import main as get_tests
1212
from commit0.harness.constants import RepoInstance, SPLIT, RUN_PYTEST_LOG_DIR
13-
from commit0.harness.utils import get_hash_string
13+
from commit0.harness.utils import get_hash_string, get_active_branch
1414

1515
logging.basicConfig(
1616
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
@@ -40,6 +40,9 @@ def main(
4040
continue
4141
pairs.append((repo_name, example["test"]["test_dir"]))
4242
hashed_test_ids = get_hash_string(example["test"]["test_dir"])
43+
if branch is None:
44+
git_path = os.path.join(base_dir, repo_name)
45+
branch = get_active_branch(git_path)
4346
log_dir = RUN_PYTEST_LOG_DIR / repo_name / branch / hashed_test_ids
4447
log_dirs.append(str(log_dir))
4548

commit0/harness/utils.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time
77
import sys
88
from pathlib import Path
9-
from typing import Optional
9+
from typing import Optional, Union
1010

1111
from fastcore.net import HTTP404NotFoundError, HTTP403ForbiddenError # type: ignore
1212
from ghapi.core import GhApi
@@ -189,4 +189,28 @@ def generate_patch_between_commits(
189189
raise Exception(f"Error generating patch: {e}")
190190

191191

192+
def get_active_branch(repo_path: Union[str, Path]) -> str:
193+
"""
194+
Retrieve the current active branch of a Git repository.
195+
196+
Args:
197+
repo_path (Path): The path to git repo.
198+
199+
Returns:
200+
str: The name of the active branch.
201+
202+
Raises:
203+
Exception: If the repository is in a detached HEAD state.
204+
"""
205+
repo = git.Repo(repo_path)
206+
try:
207+
# Get the current active branch
208+
branch = repo.active_branch.name
209+
except TypeError as e:
210+
raise Exception(f"{e}\nThis means the repository is in a detached HEAD state. "
211+
"To proceed, please specify a valid branch.")
212+
213+
return branch
214+
215+
192216
__all__ = []

0 commit comments

Comments
 (0)