diff --git a/aider/commands.py b/aider/commands.py index 5c016381a..1dd429eb0 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -306,6 +306,10 @@ class Commands: return last_commit = self.coder.repo.repo.head.commit + if not last_commit.parents: + self.io.tool_error("This is the first commit in the repository. Cannot undo.") + return + prev_commit = last_commit.parents[0] changed_files_last_commit = [item.a_path for item in last_commit.diff(prev_commit)] diff --git a/tests/basic/test_commands.py b/tests/basic/test_commands.py index f0be96232..abbcfecfc 100644 --- a/tests/basic/test_commands.py +++ b/tests/basic/test_commands.py @@ -617,6 +617,13 @@ class TestCommands(TestCase): # Attempt to undo the last commit commands.cmd_undo("") + # Check that the commit is still present + self.assertEqual(last_commit_hash, repo.head.commit.hexsha[:7]) + self.assertTrue(file_path.exists()) + + # Check that an error message was displayed + self.assertIn("This is the first commit in the repository. Cannot undo.", io.get_last_error()) + # Check that the last commit is still present self.assertEqual(last_commit_hash, repo.head.commit.hexsha[:7]) self.assertTrue(file_path.exists())