Refactored linting process and error handling in commands.

This commit is contained in:
Paul Gauthier 2024-05-20 08:55:49 -07:00
parent deb13c060c
commit 9c28bbc98e
5 changed files with 64 additions and 29 deletions

View file

@ -153,40 +153,51 @@ class Commands:
commit_message = args.strip()
self.coder.repo.commit(message=commit_message)
def cmd_lint(self, args):
def cmd_lint(self, args="", fnames=None):
"Commit, run the linter on all dirty files, fix problems and commit again"
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
if not fnames:
fnames = self.coder.repo.get_dirty_files()
fnames = self.coder.repo.get_dirty_files()
linted = False
if not fnames:
self.io.tool_error("No dirty files to lint.")
return
lint_coder = None
for fname in fnames:
try:
errors = self.coder.linter.lint(fname, cmd=args)
linted = True
except FileNotFoundError as err:
self.io.tool_error(f"Unable to lint {fname}")
self.io.tool_error(str(err))
continue
if errors:
# Commit everything before we start fixing lint errors
if self.coder.repo.is_dirty():
self.cmd_commit("")
if not errors:
continue
self.io.tool_error(errors)
# Commit everything before we start fixing lint errors
if self.coder.repo.is_dirty():
self.cmd_commit("")
abs_file_path = self.coder.abs_root_path(fname)
self.coder.abs_fnames.add(abs_file_path)
self.coder.run(errors)
self.io.tool_error(errors)
if linted and self.coder.repo.is_dirty():
if not lint_coder:
lint_coder = self.coder.clone(
# Clear the chat history, fnames
cur_messages=[],
done_messages=[],
fnames=None,
)
lint_coder.add_rel_fname(fname)
lint_coder.run(errors)
lint_coder.abs_fnames = set()
if lint_coder and self.coder.repo.is_dirty():
self.cmd_commit("")
def cmd_clear(self, args):