Skip to content

Commit 4211312

Browse files
authored
Extract co-author from gitconfig (#883)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed
1 parent dae8661 commit 4211312

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/codegen/git/repo_operator/repo_operator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,22 @@ def stage_and_commit_all_changes(self, message: str, verify: bool = False, exclu
471471
logger.warning(f"Failed to exclude path {path}: {e}")
472472
return self.commit_changes(message, verify)
473473

474+
def _get_username_email(self) -> tuple[str, str] | None:
475+
for level in ["user", "global", "system"]:
476+
with self.git_cli.config_reader(level) as reader:
477+
if reader.has_section("user"):
478+
user, email = reader.get_value("user", "name"), reader.get_value("user", "email")
479+
if isinstance(user, str) and isinstance(email, str) and user != CODEGEN_BOT_NAME and email != CODEGEN_BOT_EMAIL:
480+
return user, email
481+
return None
482+
474483
def commit_changes(self, message: str, verify: bool = False) -> bool:
475484
"""Returns True if a commit was made and False otherwise."""
476485
staged_changes = self.git_cli.git.diff("--staged")
477486
if staged_changes:
487+
if self.bot_commit and (info := self._get_username_email()):
488+
user, email = info
489+
message += f"\n\n Co-authored-by: {user} <{email}>"
478490
commit_args = ["-m", message]
479491
if self.bot_commit:
480492
commit_args.append(f"--author='{CODEGEN_BOT_NAME} <{CODEGEN_BOT_EMAIL}>'")

tests/unit/codegen/sdk/python/codebase/test_codebase_git.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pytest
24

35
from codegen.sdk.codebase.factory.get_session import get_codebase_session
@@ -22,6 +24,10 @@ def test_codebase_git(tmpdir, commit: bool, sync: bool) -> None:
2224
codebase.get_file("dir/file0.py").insert_after("a = 1")
2325
c2 = codebase.git_commit("boop")
2426
commit = codebase.op.head_commit
27+
message = commit.message
28+
if not os.environ.get("CI"):
29+
assert "Co-authored-by:" in message
30+
assert f"Co-authored-by: {commit.author.name} <{commit.author.email}>" not in message
2531
codebase.sync_to_commit(commit)
2632
assert c1 != c2
2733
assert codebase.get_symbol("a", optional=True) is not None

0 commit comments

Comments
 (0)