Improved the handling of last commit detection and diff generation in the aider commands.

This commit is contained in:
Paul Gauthier 2024-07-05 13:06:47 -03:00 committed by Paul Gauthier (aider)
parent 44e6fefc23
commit d0267f0ece

View file

@ -330,8 +330,8 @@ class Commands:
) )
return return
last_commit = self.coder.repo.repo.head.commit last_commit_hash = self.coder.repo.repo.head.commit.hexsha[:7]
if last_commit.hexsha[:7] != self.coder.last_aider_commit_hash: if last_commit_hash not in self.coder.aider_commit_hashes:
self.io.tool_error("The last commit was not made by aider in this chat session.") self.io.tool_error("The last commit was not made by aider in this chat session.")
self.io.tool_error( self.io.tool_error(
"You could try `/git reset --hard HEAD^` but be aware that this is a destructive" "You could try `/git reset --hard HEAD^` but be aware that this is a destructive"
@ -346,7 +346,7 @@ class Commands:
self.coder.repo.repo.git.reset("--soft", "HEAD~1") self.coder.repo.repo.git.reset("--soft", "HEAD~1")
self.io.tool_output( self.io.tool_output(
f"Commit `{self.coder.last_aider_commit_hash}` was reset and removed from git.\n" f"Commit `{last_commit_hash}` was reset and removed from git.\n"
) )
if self.coder.main_model.send_undo_reply: if self.coder.main_model.send_undo_reply:
@ -358,16 +358,17 @@ class Commands:
self.io.tool_error("No git repository found.") self.io.tool_error("No git repository found.")
return return
if not self.coder.last_aider_commit_hash: last_commit_hash = self.coder.repo.repo.head.commit.hexsha[:7]
self.io.tool_error("No previous aider commit found.")
if last_commit_hash not in self.coder.aider_commit_hashes:
self.io.tool_error(f"Last commit {last_commit_hash} was not an aider commit.")
self.io.tool_error("You could try `/git diff` or `/git diff HEAD^`.") self.io.tool_error("You could try `/git diff` or `/git diff HEAD^`.")
return return
commits = f"{self.coder.last_aider_commit_hash}~1"
diff = self.coder.repo.diff_commits( diff = self.coder.repo.diff_commits(
self.coder.pretty, self.coder.pretty,
commits, 'HEAD^',
self.coder.last_aider_commit_hash, 'HEAD',
) )
# don't use io.tool_output() because we don't want to log or further colorize # don't use io.tool_output() because we don't want to log or further colorize