Skip to content

Commit fd62595

Browse files
committed
make lint aider compatible
1 parent 7c882a9 commit fd62595

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

commit0/cli.py

Lines changed: 22 additions & 2 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 Union
3+
from typing import Union, List
44
from typing_extensions import Annotated
55
import commit0.harness.run_pytest_ids
66
import commit0.harness.get_pytest_ids
@@ -328,19 +328,39 @@ def lint(
328328
repo_or_repo_dir: str = typer.Argument(
329329
..., help="Directory of the repository to test"
330330
),
331+
files: Union[List[Path], None] = typer.Option(
332+
None, help="Files to lint. If not provided, all files will be linted."
333+
),
331334
commit0_dot_file_path: str = typer.Option(
332335
".commit0.yaml",
333336
help="Path to the commit0 dot file, where the setup config is stored",
334337
),
338+
verbose: int = typer.Option(
339+
1,
340+
"--verbose",
341+
"-v",
342+
help="Set this to 2 for more logging information",
343+
count=True,
344+
),
335345
) -> None:
336346
"""Lint given files if provided, otherwise lint all files in the base directory."""
337347
check_commit0_path()
338348
commit0_config = read_commit0_dot_file(commit0_dot_file_path)
339-
typer.echo(f"Linting repo: {highlight(str(repo_or_repo_dir), Colors.ORANGE)}")
349+
appended_files = None
350+
if files is not None:
351+
appended_files = []
352+
for path in files:
353+
path = Path(commit0_config["base_dir"]) / Path(repo_or_repo_dir) / path
354+
if not path.is_file():
355+
raise FileNotFoundError(f"File not found: {str(path)}")
356+
appended_files.append(path)
357+
if verbose == 2:
358+
typer.echo(f"Linting repo: {highlight(str(repo_or_repo_dir), Colors.ORANGE)}")
340359
commit0.harness.lint.main(
341360
commit0_config["dataset_name"],
342361
commit0_config["dataset_split"],
343362
repo_or_repo_dir,
363+
appended_files,
344364
commit0_config["base_dir"],
345365
)
346366

commit0/harness/lint.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
from datasets import load_dataset
55
from pathlib import Path
6-
from typing import Iterator
6+
from typing import Iterator, Union, List
77

88
from commit0.harness.constants import (
99
RepoInstance,
@@ -35,7 +35,7 @@
3535

3636

3737
def main(
38-
dataset_name: str, dataset_split: str, repo_or_repo_dir: str, base_dir: str
38+
dataset_name: str, dataset_split: str, repo_or_repo_dir: str, files: Union[List[Path], None], base_dir: str
3939
) -> None:
4040
dataset: Iterator[RepoInstance] = load_dataset(dataset_name, split=dataset_split) # type: ignore
4141
example = None
@@ -49,22 +49,23 @@ def main(
4949
assert example is not None, "No example available"
5050
assert repo_name is not None, "No repo available"
5151

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-
)
52+
if files is None:
53+
repo_dir = os.path.join(base_dir, repo_name)
54+
if os.path.isdir(repo_or_repo_dir):
55+
repo = repo_or_repo_dir
56+
elif os.path.isdir(repo_dir):
57+
repo = repo_dir
58+
else:
59+
raise Exception(
60+
f"Neither {repo_dir} nor {repo_or_repo_dir} is a valid path.\nUsage: commit0 lint {{repo_or_repo_dir}}"
61+
)
6162

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))
63+
files = []
64+
repo = os.path.join(repo, example["src_dir"])
65+
for root, dirs, fs in os.walk(repo):
66+
for file in fs:
67+
if file.endswith(".py"):
68+
files.append(os.path.join(root, file))
6869

6970
config_file = Path(".commit0.pre-commit-config.yaml")
7071
if not config_file.is_file():
@@ -75,7 +76,8 @@ def main(
7576
print(result.stdout)
7677
sys.exit(result.returncode)
7778
except subprocess.CalledProcessError as e:
78-
raise Exception(f"Pre-commit checks failed\n{e.output}")
79+
print(e.output)
80+
sys.exit(e.returncode)
7981
except FileNotFoundError:
8082
raise FileNotFoundError("Error: pre-commit command not found. Is it installed?")
8183
except Exception as e:

0 commit comments

Comments
 (0)