From 787103d41b13a6d710c01d94d91607692dad572d Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 10 Mar 2025 16:08:29 +0000 Subject: [PATCH 1/2] Make git credential overwriting opt-in during codegen init Co-Authored-By: jay@codegen.com --- src/codegen/cli/auth/session.py | 3 ++- src/codegen/cli/commands/init/main.py | 5 +++-- src/codegen/cli/workspace/initialize_workspace.py | 2 +- src/codegen/git/repo_operator/repo_operator.py | 11 +++++++++-- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/codegen/cli/auth/session.py b/src/codegen/cli/auth/session.py index d06454330..9c3a56761 100644 --- a/src/codegen/cli/auth/session.py +++ b/src/codegen/cli/auth/session.py @@ -22,7 +22,7 @@ class CodegenSession: config: UserConfig existing: bool - def __init__(self, repo_path: Path, git_token: str | None = None) -> None: + def __init__(self, repo_path: Path, git_token: str | None = None, preserve_git_credentials: bool = False) -> None: if not repo_path.exists() or get_git_repo(repo_path) is None: rich.print(f"\n[bold red]Error:[/bold red] Path to git repo does not exist at {self.repo_path}") raise click.Abort() @@ -33,6 +33,7 @@ def __init__(self, repo_path: Path, git_token: str | None = None) -> None: self.config = UserConfig(env_filepath=repo_path / ENV_FILENAME) self.config.secrets.github_token = git_token or self.config.secrets.github_token self.existing = session_manager.get_session(repo_path) is not None + self.preserve_git_credentials = preserve_git_credentials self._initialize() session_manager.set_active_session(repo_path) diff --git a/src/codegen/cli/commands/init/main.py b/src/codegen/cli/commands/init/main.py index b10d6ca23..0def53c05 100644 --- a/src/codegen/cli/commands/init/main.py +++ b/src/codegen/cli/commands/init/main.py @@ -16,7 +16,8 @@ @click.option("--token", type=str, help="Access token for the git repository. Required for full functionality.") @click.option("--language", type=click.Choice(["python", "typescript"], case_sensitive=False), help="Override automatic language detection") @click.option("--fetch-docs", is_flag=True, help="Fetch docs and examples (requires auth)") -def init_command(path: str | None = None, token: str | None = None, language: str | None = None, fetch_docs: bool = False): +@click.option("--preserve-git-credentials", is_flag=True, help="Preserve existing git user.name and user.email credentials") +def init_command(path: str | None = None, token: str | None = None, language: str | None = None, fetch_docs: bool = False, preserve_git_credentials: bool = False): """Initialize or update the Codegen folder.""" # Print a message if not in a git repo path = Path.cwd() if path is None else Path(path) @@ -31,7 +32,7 @@ def init_command(path: str | None = None, token: str | None = None, language: st rich.print(format_command("codegen init")) sys.exit(1) - session = CodegenSession(repo_path=repo_path, git_token=token) + session = CodegenSession(repo_path=repo_path, git_token=token, preserve_git_credentials=preserve_git_credentials) if language: session.config.repository.language = language.upper() session.config.save() diff --git a/src/codegen/cli/workspace/initialize_workspace.py b/src/codegen/cli/workspace/initialize_workspace.py index 1ab32cfed..0f09c2ac7 100644 --- a/src/codegen/cli/workspace/initialize_workspace.py +++ b/src/codegen/cli/workspace/initialize_workspace.py @@ -18,7 +18,7 @@ from codegen.cli.workspace.venv_manager import VenvManager -def initialize_codegen(session: CodegenSession, status: Status | str = "Initializing", fetch_docs: bool = False) -> CodegenSession: +def initialize_codegen(session: CodegenSession, status: Status | str = "Initializing", fetch_docs: bool = False) -> tuple[Path, Path, Path]: """Initialize or update the codegen directory structure and content. Args: diff --git a/src/codegen/git/repo_operator/repo_operator.py b/src/codegen/git/repo_operator/repo_operator.py index b5febfe14..6398865e2 100644 --- a/src/codegen/git/repo_operator/repo_operator.py +++ b/src/codegen/git/repo_operator/repo_operator.py @@ -151,12 +151,19 @@ def git_cli(self) -> GitCLI: email_level = email_level or level # We need a username and email to commit, so if they're not set, set them to the bot's - if not username or self.bot_commit: + # Only set bot credentials if explicitly requested via bot_commit or if no credentials exist + if not username: self._set_bot_username(git_cli) - if not email or self.bot_commit: + elif self.bot_commit: + self._set_bot_username(git_cli) + + if not email: + self._set_bot_email(git_cli) + elif self.bot_commit: self._set_bot_email(git_cli) # If user config is set at a level above the repo level: unset it + # Only unset if bot_commit is False (preserving user credentials) if not self.bot_commit: if username and username != CODEGEN_BOT_NAME and user_level != "repository": self._unset_bot_username(git_cli) From 38b8867ff05039653149a7faa55d858877ffc82d Mon Sep 17 00:00:00 2001 From: "devin-ai-integration[bot]" <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 10 Mar 2025 16:09:30 +0000 Subject: [PATCH 2/2] Automated pre-commit update --- src/codegen/git/repo_operator/repo_operator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codegen/git/repo_operator/repo_operator.py b/src/codegen/git/repo_operator/repo_operator.py index 6398865e2..460a4d62c 100644 --- a/src/codegen/git/repo_operator/repo_operator.py +++ b/src/codegen/git/repo_operator/repo_operator.py @@ -156,7 +156,7 @@ def git_cli(self) -> GitCLI: self._set_bot_username(git_cli) elif self.bot_commit: self._set_bot_username(git_cli) - + if not email: self._set_bot_email(git_cli) elif self.bot_commit: