Skip to content

Commit 7eb3771

Browse files
authored
Merge pull request #55 from commit-0/lint
Lint
2 parents 7c882a9 + 4b56431 commit 7eb3771

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-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: 24 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,11 @@
3535

3636

3737
def main(
38-
dataset_name: str, dataset_split: str, repo_or_repo_dir: str, base_dir: str
38+
dataset_name: str,
39+
dataset_split: str,
40+
repo_or_repo_dir: str,
41+
files: Union[List[Path], None],
42+
base_dir: str,
3943
) -> None:
4044
dataset: Iterator[RepoInstance] = load_dataset(dataset_name, split=dataset_split) # type: ignore
4145
example = None
@@ -49,22 +53,23 @@ def main(
4953
assert example is not None, "No example available"
5054
assert repo_name is not None, "No repo available"
5155

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

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))
67+
files = []
68+
repo = os.path.join(repo, example["src_dir"])
69+
for root, dirs, fs in os.walk(repo):
70+
for file in fs:
71+
if file.endswith(".py"):
72+
files.append(Path(os.path.join(root, file)))
6873

6974
config_file = Path(".commit0.pre-commit-config.yaml")
7075
if not config_file.is_file():
@@ -75,7 +80,8 @@ def main(
7580
print(result.stdout)
7681
sys.exit(result.returncode)
7782
except subprocess.CalledProcessError as e:
78-
raise Exception(f"Pre-commit checks failed\n{e.output}")
83+
print(e.output)
84+
sys.exit(e.returncode)
7985
except FileNotFoundError:
8086
raise FileNotFoundError("Error: pre-commit command not found. Is it installed?")
8187
except Exception as e:

0 commit comments

Comments
 (0)