From 6b24072973719d3f83411ccc005c6bbdd18d311c Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 10 May 2023 12:39:45 -0700 Subject: [PATCH] aider: Added `cmd_commit` method to commit outstanding changes to the chat files with a sensible commit message. # Context: USER: finish implementing cmd_commit ASSISTANT: To finish implementing the `cmd_commit` method in the `Coder` class, you can add the following code to `aider/coder.py`. This method will commit outstanding changes to the chat files with a sensible commit message. ```python aider/coder.py <<<<<<< ORIGINAL def cmd_commit(self, args): "Commit outstanding changes to the chat files with a sensible commit message" ======= def cmd_commit(self, args): "Commit outstanding changes to the chat files with a sensible commit message" if not self.repo: self.console.print("[red]No git repository found.") return if not self.repo.is_dirty(): self.console.print("[red]No changes to commit.") return commit_message = args.strip() if not commit_message: commit_message = "aider: Manual commit of outstanding changes" self.repo.git.add(*[os.path.relpath(fname, self.repo.working_tree_dir) for fname in self.fnames]) self.repo.git.commit("-m", commit_message, "--no-verify") commit_hash = self.repo.head.commit.hexsha[:7] self.console.print(f"[red]{commit_hash} {commit_message}") >>>>>>> UPDATED ``` This code checks if there is a git repository and if there are any outstanding changes. If there are changes, it commits them with the provided commit message or a default one if no message is provided. --- aider/coder.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/aider/coder.py b/aider/coder.py index 8fe543f84..d0cbb9ec4 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -486,6 +486,24 @@ class Coder: return files + def cmd_commit(self, args): + "Commit outstanding changes to the chat files with a sensible commit message" + if not self.repo: + self.console.print("[red]No git repository found.") + return + + if not self.repo.is_dirty(): + self.console.print("[red]No changes to commit.") + return + + commit_message = args.strip() + if not commit_message: + commit_message = "aider: Manual commit of outstanding changes" + + self.repo.git.add(*[os.path.relpath(fname, self.repo.working_tree_dir) for fname in self.fnames]) + self.repo.git.commit("-m", commit_message, "--no-verify") + commit_hash = self.repo.head.commit.hexsha[:7] + self.console.print(f"[red]{commit_hash} {commit_message}") def cmd_undo(self, args): "Undo the last git commit if it was done by aider" if not self.repo: @@ -494,10 +512,11 @@ class Coder: last_commit = self.repo.head.commit if not last_commit.message.startswith("aider:") or last_commit.hexsha[:7] != self.last_aider_commit_hash: - self.console.print("[red]The last commit was not made by Aider or the commit hash does not match.") + self.console.print("[red]The last commit was not made by aider in this chat session.") return self.repo.git.reset("--hard", "HEAD~1") self.console.print(f"[red]Undid the last commit: {last_commit.message.strip()}") + def cmd_add(self, args): "Add matching files to the chat"