Handled case where first commit cannot be undone

This commit is contained in:
Paul Gauthier (aider) 2024-07-18 16:32:47 +01:00
parent 3ce3799f8d
commit 6fdb4f8214
2 changed files with 11 additions and 0 deletions

View file

@ -306,6 +306,10 @@ class Commands:
return return
last_commit = self.coder.repo.repo.head.commit 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] prev_commit = last_commit.parents[0]
changed_files_last_commit = [item.a_path for item in last_commit.diff(prev_commit)] changed_files_last_commit = [item.a_path for item in last_commit.diff(prev_commit)]

View file

@ -617,6 +617,13 @@ class TestCommands(TestCase):
# Attempt to undo the last commit # Attempt to undo the last commit
commands.cmd_undo("") 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 # Check that the last commit is still present
self.assertEqual(last_commit_hash, repo.head.commit.hexsha[:7]) self.assertEqual(last_commit_hash, repo.head.commit.hexsha[:7])
self.assertTrue(file_path.exists()) self.assertTrue(file_path.exists())