Skip to content

Commit 92ebb97

Browse files
committed
added a new command lint
1 parent b50a4d5 commit 92ebb97

File tree

7 files changed

+91
-10
lines changed

7 files changed

+91
-10
lines changed

baselines/agents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def run(
5858
fnames=fnames,
5959
auto_lint=auto_lint,
6060
auto_test=auto_test,
61-
lint_cmds=lint_cmd,
61+
lint_cmds={"python": lint_cmd},
6262
test_cmd=test_cmd,
6363
io=io,
6464
)

baselines/commit0_utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,38 @@ def args2string(agent_config: AgentConfig) -> str:
242242
result_list.append(f"{key}-{value}")
243243
concatenated_string = "__".join(result_list)
244244
return concatenated_string
245+
246+
247+
def get_changed_files(repo: git.Repo) -> list[str]:
248+
"""
249+
Get a list of files that were changed in the latest commit of the provided Git repository.
250+
251+
Args:
252+
repo (git.Repo): An instance of GitPython's Repo object representing the Git repository.
253+
254+
Returns:
255+
list[str]: A list of filenames (as strings) that were changed in the latest commit.
256+
"""
257+
latest_commit = repo.head.commit
258+
# Get the list of files changed in the latest commit
259+
files_changed = latest_commit.stats.files
260+
return list(files_changed.keys())
261+
262+
def get_lint_cmd(repo: git.Repo, use_lint_info: bool) -> str:
263+
"""
264+
Generate a linting command based on whether to include files changed in the latest commit.
265+
266+
Args:
267+
repo (git.Repo): An instance of GitPython's Repo object representing the Git repository.
268+
use_lint_info (bool): A flag indicating whether to include changed files in the lint command.
269+
270+
Returns:
271+
str: The generated linting command string. If `use_lint_info` is True, the command includes
272+
the list of changed files. If False, returns an empty string.
273+
"""
274+
lint_cmd = "python -m commit0 lint "
275+
if use_lint_info:
276+
lint_cmd += " ".join(get_changed_files(repo))
277+
else:
278+
lint_cmd = ""
279+
return lint_cmd

baselines/configs/agent.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ agent_config:
1111
use_repo_info: false
1212
use_unit_tests_info: false
1313
use_spec_info: false
14-
use_lint_info: false
14+
use_lint_info: true
1515
pre_commit_config_path: .pre-commit-config.yaml
1616
run_tests: false

baselines/run_agent.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
create_branch,
1010
get_message,
1111
get_target_edit_files,
12+
get_lint_cmd,
1213
)
1314
from baselines.agents import AiderAgents
1415
from typing import Optional, Type
@@ -63,8 +64,6 @@ def run_agent_for_repo(
6364
f"{repo_path} is not a git repo. Check if base_dir is correctly specified."
6465
)
6566

66-
target_edit_files = get_target_edit_files(repo_path)
67-
6867
if agent_config.agent_name == "aider":
6968
agent = AiderAgents(agent_config.max_iteration, agent_config.model_name)
7069
else:
@@ -81,24 +80,20 @@ def run_agent_for_repo(
8180
# TODO: ask user for permission
8281
if latest_commit.hexsha != example["base_commit"]:
8382
local_repo.git.reset("--hard", example["base_commit"])
83+
target_edit_files = get_target_edit_files(repo_path)
8484

8585
with DirContext(repo_path):
8686
if commit0_config is None or agent_config is None:
8787
raise ValueError("Invalid input")
8888

8989
message = get_message(agent_config, repo_path, example["test"]["test_dir"])
90-
91-
if agent_config.use_lint_info:
92-
lint_cmd = "pre-commit run --config ../../.pre-commit-config.yaml --files"
93-
else:
94-
lint_cmd = ""
95-
9690
if agent_config.run_tests:
9791
# when unit test feedback is available, iterate over test files
9892
for test_file in test_files:
9993
test_cmd = f"python -m commit0 test {repo_path} {run_id} {test_file}"
10094
test_file_name = test_file.replace(".py", "").replace("/", "__")
10195
log_dir = RUN_AIDER_LOG_DIR / "with_tests" / test_file_name
96+
lint_cmd = get_lint_cmd(local_repo, agent_config.use_lint_info)
10297

10398
agent.run(
10499
message,
@@ -112,6 +107,7 @@ def run_agent_for_repo(
112107
for f in target_edit_files:
113108
file_name = f.replace(".py", "").replace("/", "__")
114109
log_dir = RUN_AIDER_LOG_DIR / "no_tests" / file_name
110+
lint_cmd = get_lint_cmd(local_repo, agent_config.use_lint_info)
115111

116112
agent.run(message, "", lint_cmd, [f], log_dir)
117113

commit0/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import commit0.harness.build
44
import commit0.harness.setup
55
import commit0.harness.evaluate
6+
import commit0.harness.lint
67
import commit0.harness.save
78
import copy
89
import sys
@@ -141,6 +142,9 @@ def main() -> None:
141142
config.num_cpus,
142143
config.num_workers,
143144
)
145+
elif command == "lint":
146+
files = sys.argv[1:]
147+
commit0.harness.lint.main(config.base_dir, files)
144148
elif command == "save":
145149
if len(sys.argv) != 5:
146150
raise ValueError(

commit0/harness/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Files(TypedDict):
4040
"get-tests",
4141
"evaluate",
4242
"evaluate-reference",
43+
"lint",
4344
"save",
4445
]
4546
# repo splits

commit0/harness/lint.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import subprocess
2+
import sys
3+
from pathlib import Path
4+
5+
6+
config = \
7+
"""repos:
8+
# Standard hooks
9+
- repo: https://github.com/pre-commit/pre-commit-hooks
10+
rev: v4.3.0
11+
hooks:
12+
- id: check-case-conflict
13+
- id: mixed-line-ending
14+
15+
- repo: https://github.com/astral-sh/ruff-pre-commit
16+
# Ruff version.
17+
rev: v0.6.1
18+
hooks:
19+
# Run the linter.
20+
- id: ruff
21+
args: [ --fix ]
22+
# Run the formatter.
23+
- id: ruff-format
24+
25+
- repo: https://github.com/RobertCraigie/pyright-python
26+
rev: v1.1.376
27+
hooks:
28+
- id: pyright"""
29+
30+
31+
def main(base_dir: str, files: list[str]) -> None:
32+
config_file = Path(".commit0.pre-commit-config.yaml")
33+
if not config_file.is_file():
34+
config_file.write_text(config)
35+
command = ['pre-commit', 'run', '--config', config_file, '--files'] + files
36+
try:
37+
result = subprocess.run(command, capture_output=True, text=True, check=True)
38+
print(result.stdout)
39+
sys.exit(result.returncode)
40+
except subprocess.CalledProcessError as e:
41+
raise Exception(f"Pre-commit checks failed\n{e.output}")
42+
except FileNotFoundError as e:
43+
raise FileNotFoundError("Error: pre-commit command not found. Is it installed?")
44+
except Exception as e:
45+
raise Exception(f"An unexpected error occurred: {e}")

0 commit comments

Comments
 (0)