From 7ebeb33a4d2419b70f434dd4f9a45c9aab02d206 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 31 Aug 2024 10:41:53 -0700 Subject: [PATCH] feat: enhance undo command with error handling and merge commit checks --- aider/commands.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/aider/commands.py b/aider/commands.py index b2b1a600e..3b9fe7f06 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -425,6 +425,12 @@ class Commands: self.io.tool_error("This is the first commit in the repository. Cannot undo.") return + if len(last_commit.parents) > 1: + self.io.tool_error( + f"The last commit {last_commit.hexsha} has more than 1 parent, can't undo." + ) + return + prev_commit = last_commit.parents[0] changed_files_last_commit = [item.a_path for item in last_commit.diff(prev_commit)] @@ -473,8 +479,14 @@ class Commands: return # Reset only the files which are part of `last_commit` + modified = set() for file_path in changed_files_last_commit: - self.coder.repo.repo.git.checkout("HEAD~1", file_path) + try: + self.coder.repo.repo.git.checkout("HEAD~1", file_path) + modified.add(file_path) + except ANY_GIT_ERROR: + self.io.tool_error(f"Error restoring {file_path}, aborting undo.") + return # Move the HEAD back before the latest commit self.coder.repo.repo.git.reset("--soft", "HEAD~1")