append (aider) to author if aider wrote the code

This commit is contained in:
Paul Gauthier 2024-06-18 11:18:52 -07:00
parent 39f10aefe0
commit 9e228670a2
3 changed files with 19 additions and 10 deletions

View file

@ -332,7 +332,7 @@ class Commands:
last_commit = self.coder.repo.repo.head.commit last_commit = self.coder.repo.repo.head.commit
if ( if (
"(aider)" not in last_commit.committer.name not last_commit.author.name.endswith(" (aider)")
or last_commit.hexsha[:7] != self.coder.last_aider_commit_hash or last_commit.hexsha[:7] != self.coder.last_aider_commit_hash
): ):
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.")

View file

@ -88,11 +88,15 @@ class GitRepo:
else: else:
cmd += ["-a"] cmd += ["-a"]
original_user_name = self.repo.config_reader().get_value("user", "name")
original_committer_name_env = os.environ.get("GIT_COMMITTER_NAME")
committer_name = f"{original_user_name} (aider)"
os.environ["GIT_COMMITTER_NAME"] = committer_name
if aider_edits: if aider_edits:
user_name = self.repo.config_reader().get_value("user", "name") original_auther_name_env = os.environ.get("GIT_AUTHOR_NAME")
committer_name = f"{user_name} (aider)" os.environ["GIT_AUTHOR_NAME"] = committer_name
original_committer_name = os.environ.get("GIT_COMMITTER_NAME")
os.environ["GIT_COMMITTER_NAME"] = committer_name
self.repo.git.commit(cmd) self.repo.git.commit(cmd)
commit_hash = self.repo.head.commit.hexsha[:7] commit_hash = self.repo.head.commit.hexsha[:7]
@ -100,10 +104,15 @@ class GitRepo:
# Restore the original GIT_COMMITTER_NAME # Restore the original GIT_COMMITTER_NAME
if aider_edits: if aider_edits:
if original_committer_name is not None: if original_auther_name_env is not None:
os.environ["GIT_COMMITTER_NAME"] = original_committer_name os.environ["GIT_AUTHOR_NAME"] = original_auther_name_env
else: else:
del os.environ["GIT_COMMITTER_NAME"] del os.environ["GIT_AUTHOR_NAME"]
if original_committer_name_env is not None:
os.environ["GIT_COMMITTER_NAME"] = original_committer_name_env
else:
del os.environ["GIT_COMMITTER_NAME"]
return commit_hash, commit_message return commit_hash, commit_message

View file

@ -523,7 +523,7 @@ class TestCommands(TestCase):
other_path.write_text("other content") other_path.write_text("other content")
repo.git.add(str(other_path)) repo.git.add(str(other_path))
os.environ["GIT_COMMITTER_NAME"] = "Foo (aider)" os.environ["GIT_AUTHOR_NAME"] = "Foo (aider)"
# Create and commit a file # Create and commit a file
filename = "test_file.txt" filename = "test_file.txt"
@ -536,7 +536,7 @@ class TestCommands(TestCase):
repo.git.add(filename) repo.git.add(filename)
repo.git.commit("-m", "second commit") repo.git.commit("-m", "second commit")
del os.environ["GIT_COMMITTER_NAME"] del os.environ["GIT_AUTHOR_NAME"]
# Store the commit hash # Store the commit hash
last_commit_hash = repo.head.commit.hexsha[:7] last_commit_hash = repo.head.commit.hexsha[:7]