Skip to content

Commit defc59b

Browse files
authored
Merge pull request #50 from commit-0/lint-repo
Lint repo
2 parents 16065e0 + 71720e0 commit defc59b

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

.github/workflows/system.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
uv run commit0 evaluate --reference --rebuild
4040
uv run commit0 evaluate --reference
4141
- name: Lint
42-
run: uv run commit0 lint commit0/harness/lint.py
42+
run: uv run commit0 lint commit0/harness/
4343
- name: Save
4444
env:
4545
GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}

commit0/cli.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import typer
22
from pathlib import Path
3-
from typing import List, Union
3+
from typing import Union
44
from typing_extensions import Annotated
55
import commit0.harness.run_pytest_ids
66
import commit0.harness.get_pytest_ids
@@ -327,17 +327,24 @@ def evaluate(
327327

328328
@commit0_app.command()
329329
def lint(
330-
files: List[Path] = typer.Argument(..., help="Files to lint."),
330+
repo_or_repo_dir: str = typer.Argument(
331+
..., help="Directory of the repository to test"
332+
),
333+
commit0_dot_file_path: str = typer.Option(
334+
".commit0.yaml",
335+
help="Path to the commit0 dot file, where the setup config is stored",
336+
),
331337
) -> None:
332338
"""Lint given files if provided, otherwise lint all files in the base directory."""
333339
check_commit0_path()
334-
for path in files:
335-
if not path.is_file():
336-
raise FileNotFoundError(f"File not found: {str(path)}")
337-
typer.echo(
338-
f"Linting specific files: {', '.join(highlight(str(file), Colors.ORANGE) for file in files)}"
340+
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
341+
typer.echo(f"Linting repo: {highlight(str(repo_or_repo_dir), Colors.ORANGE)}")
342+
commit0.harness.lint.main(
343+
commit0_config["dataset_name"],
344+
commit0_config["dataset_split"],
345+
repo_or_repo_dir,
346+
commit0_config["base_dir"],
339347
)
340-
commit0.harness.lint.main(files)
341348

342349

343350
@commit0_app.command()

commit0/harness/lint.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import subprocess
22
import sys
3+
import os
4+
from datasets import load_dataset
35
from pathlib import Path
4-
from typing import List
6+
from typing import Iterator
7+
8+
from commit0.harness.constants import (
9+
RepoInstance,
10+
)
511

612

713
config = """repos:
@@ -28,7 +34,38 @@
2834
- id: pyright"""
2935

3036

31-
def main(files: List[Path]) -> None:
37+
def main(
38+
dataset_name: str, dataset_split: str, repo_or_repo_dir: str, base_dir: str
39+
) -> None:
40+
dataset: Iterator[RepoInstance] = load_dataset(dataset_name, split=dataset_split) # type: ignore
41+
example = None
42+
repo_name = None
43+
for example in dataset:
44+
repo_name = example["repo"].split("/")[-1]
45+
if repo_or_repo_dir.endswith("/"):
46+
repo_or_repo_dir = repo_or_repo_dir[:-1]
47+
if repo_name in os.path.basename(repo_or_repo_dir):
48+
break
49+
assert example is not None, "No example available"
50+
assert repo_name is not None, "No repo available"
51+
52+
repo_dir = os.path.join(base_dir, repo_name)
53+
if os.path.isdir(repo_or_repo_dir):
54+
repo = repo_or_repo_dir
55+
elif os.path.isdir(repo_dir):
56+
repo = repo_dir
57+
else:
58+
raise Exception(
59+
f"Neither {repo_dir} nor {repo_or_repo_dir} is a valid path.\nUsage: commit0 lint {{repo_or_repo_dir}}"
60+
)
61+
62+
files = []
63+
repo = os.path.join(repo, example["src_dir"])
64+
for root, dirs, fs in os.walk(repo):
65+
for file in fs:
66+
if file.endswith(".py"):
67+
files.append(os.path.join(root, file))
68+
3269
config_file = Path(".commit0.pre-commit-config.yaml")
3370
if not config_file.is_file():
3471
config_file.write_text(config)

0 commit comments

Comments
 (0)