Skip to content
Closed
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
3 changes: 2 additions & 1 deletion src/codegen/cli/auth/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/codegen/cli/commands/init/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/cli/workspace/initialize_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 9 additions & 2 deletions src/codegen/git/repo_operator/repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading