8000 Extract co-author from gitconfig by bagel897 · Pull Request #883 · codegen-sh/codegen · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/codegen/git/repo_operator/repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,22 @@ def stage_and_commit_all_changes(self, message: str, verify: bool = False, exclu
logger.warning(f"Failed to exclude path {path}: {e}")
return self.commit_changes(message, verify)

def _get_username_email(self) -> tuple[str, str] | None:
for level in ["user", "global", "system"]:
with self.git_cli.config_reader(level) as reader:
if reader.has_section("user"):
user, email = reader.get_value("user", "name"), reader.get_value("user", "email")
if isinstance(user, str) and isinstance(email, str) and user != CODEGEN_BOT_NAME and email != CODEGEN_BOT_EMAIL:
return user, email
return None

def commit_changes(self, message: str, verify: bool = False) -> bool:
"""Returns True if a commit was made and False otherwise."""
staged_changes = self.git_cli.git.diff("--staged")
if staged_changes:
if self.bot_commit and (info := self._get_username_email()):
user, email = info
message += f"\n\n Co-authored-by: {user} <{email}>"
commit_args = ["-m", message]
if self.bot_commit:
commit_args.append(f"--author='{CODEGEN_BOT_NAME} <{CODEGEN_BOT_EMAIL}>'")
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/codegen/sdk/python/codebase/test_codebase_git.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import pytest

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