mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-21 21:04:59 +00:00
Implemented checks to ensure files with uncommitted changes or not present in previous commit cannot be undone safely.
This commit is contained in:
parent
e7f29c2dbc
commit
3ce3799f8d
2 changed files with 22 additions and 12 deletions
|
@ -306,14 +306,22 @@ class Commands:
|
|||
return
|
||||
|
||||
last_commit = self.coder.repo.repo.head.commit
|
||||
changed_files_last_commit = [
|
||||
item.a_path for item in last_commit.diff(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)]
|
||||
|
||||
if any(self.coder.repo.repo.is_dirty(path=fname) for fname in changed_files_last_commit):
|
||||
for fname in changed_files_last_commit:
|
||||
if self.coder.repo.repo.is_dirty(path=fname):
|
||||
self.io.tool_error(
|
||||
"The repository has uncommitted changes in files that were modified in the last"
|
||||
" commit. Please commit or stash them before undoing."
|
||||
f"The file {fname} has uncommitted changes. Please stash them before undoing."
|
||||
)
|
||||
return
|
||||
|
||||
# Check if the file was in the repo in the previous commit
|
||||
try:
|
||||
prev_commit.tree[fname]
|
||||
except KeyError:
|
||||
self.io.tool_error(
|
||||
f"The file {fname} was not in the repository in the previous commit. Cannot undo safely."
|
||||
)
|
||||
return
|
||||
|
||||
|
@ -346,6 +354,7 @@ class Commands:
|
|||
# Reset only the files which are part of `last_commit`
|
||||
for file_path in changed_files_last_commit:
|
||||
self.coder.repo.repo.git.checkout("HEAD~1", file_path)
|
||||
|
||||
# Move the HEAD back before the latest commit
|
||||
self.coder.repo.repo.git.reset("--soft", "HEAD~1")
|
||||
|
||||
|
|
|
@ -585,11 +585,12 @@ class TestCommands(TestCase):
|
|||
last_commit_hash = repo.head.commit.hexsha[:7]
|
||||
coder.aider_commit_hashes.add(last_commit_hash)
|
||||
|
||||
# Attempt to undo the last commit
|
||||
# Attempt to undo the last commit, should refuse
|
||||
commands.cmd_undo("")
|
||||
|
||||
# Check that the last commit was undone
|
||||
self.assertNotEqual(last_commit_hash, repo.head.commit.hexsha[:7])
|
||||
# Check that the last commit was not undone
|
||||
self.assertEqual(last_commit_hash, repo.head.commit.hexsha[:7])
|
||||
self.assertTrue(file_path.exists())
|
||||
|
||||
del coder
|
||||
del commands
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue