From 04084883e85559766bc9f8fb46a92ff90830b15c Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 18 May 2024 18:35:33 -0700 Subject: [PATCH] Added a new command to run the linter on dirty files, fix problems, and then commit. --- aider/args.py | 6 ++++++ aider/commands.py | 24 ++++++++++++++++++++++++ aider/linter.py | 5 ++++- aider/main.py | 4 ++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/aider/args.py b/aider/args.py index 3b42d074d..9ff40f61a 100644 --- a/aider/args.py +++ b/aider/args.py @@ -311,6 +311,12 @@ def get_parser(default_config_files, git_root): help="Commit all pending changes with a suitable commit message, then exit", default=False, ) + group.add_argument( + "--lint", + action="store_true", + help="Run the linter on all dirty files, fix problems and then commit.", + default=False, + ) ########## group = parser.add_argument_group("Other Settings") diff --git a/aider/commands.py b/aider/commands.py index c592a5d7f..19ffa9b70 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -153,6 +153,30 @@ class Commands: commit_message = args.strip() self.coder.repo.commit(message=commit_message) + def cmd_lint(self, args): + "Run the linter on all dirty files, fix problems and then commit" + + if not self.coder.repo: + self.io.tool_error("No git repository found.") + return + + if not self.coder.repo.is_dirty(): + self.io.tool_error("No more changes to commit.") + return + + fnames = self.coder.repo.get_dirty_files() + + for fname in fnames: + errors = self.coder.linter.lint(fname) + if errors: + self.io.tool_error(errors) + + abs_file_path = self.coder.abs_root_path(fname) + self.coder.abs_fnames.add(abs_file_path) + self.coder.run(errors) + + self.cmd_commit("") + def cmd_clear(self, args): "Clear the chat history" diff --git a/aider/linter.py b/aider/linter.py index 98a0ebc15..42670fc58 100644 --- a/aider/linter.py +++ b/aider/linter.py @@ -24,7 +24,8 @@ class Linter: py_cmd = f"flake8 --select={fatal} --show-source" # noqa: F841 self.languages = dict( - python=self.py_lint, + # python=self.py_lint, + python="pre-commit run --files" ) def set_linter(self, lang, cmd): @@ -41,8 +42,10 @@ class Linter: cmd = cmd.split() try: subprocess.check_output(cmd, cwd=self.root).decode() + print("zero") return # zero exit status except subprocess.CalledProcessError as err: + print("non-zero") return err.output.decode() # non-zero exit status def lint(self, fname): diff --git a/aider/main.py b/aider/main.py index 2074cf803..34aca61ca 100644 --- a/aider/main.py +++ b/aider/main.py @@ -355,6 +355,10 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F coder.commands.cmd_commit("") return + if args.lint: + coder.commands.cmd_lint("") + return + if args.show_repo_map: repo_map = coder.get_repo_map() if repo_map: