diff --git a/src/codegen/git/repo_operator/local_repo_operator.py b/src/codegen/git/repo_operator/local_repo_operator.py index d739e1252..635e703c5 100644 --- a/src/codegen/git/repo_operator/local_repo_operator.py +++ b/src/codegen/git/repo_operator/local_repo_operator.py @@ -27,7 +27,6 @@ class LocalRepoOperator(RepoOperator): _repo_path: str _repo_name: str - _default_branch: str _git_cli: GitCLI repo_config: BaseRepoConfig @@ -35,12 +34,10 @@ def __init__( self, repo_config: BaseRepoConfig, repo_path: str, # full path to the repo - default_branch: str, # default branch of the repo bot_commit: bool = True, ) -> None: self._repo_path = repo_path self._repo_name = os.path.basename(repo_path) - self._default_branch = default_branch os.makedirs(self.repo_path, exist_ok=True) GitCLI.init(self.repo_path) super().__init__(repo_config, self.repo_path, bot_commit) @@ -66,15 +63,15 @@ def create_from_files(cls, repo_path: str, files: dict[str, str], bot_commit: bo create_files(base_dir=repo_path, files=files) # Step 2: Init git repo - op = cls(repo_path=repo_path, default_branch="main", bot_commit=bot_commit, repo_config=repo_config) + op = cls(repo_path=repo_path, bot_commit=bot_commit, repo_config=repo_config) if op.stage_and_commit_all_changes("[Codegen] initial commit"): - op.checkout_branch(op.default_branch, create_if_missing=True) + op.checkout_branch(None, create_if_missing=True) return op @classmethod - def create_from_commit(cls, repo_path: str, default_branch: str, commit: str, url: str) -> Self: + def create_from_commit(cls, repo_path: str, commit: str, url: str) -> Self: """Do a shallow checkout of a particular commit to get a repository from a given remote URL.""" - op = cls(repo_config=BaseRepoConfig(), repo_path=repo_path, default_branch=default_branch, bot_commit=False) + op = cls(repo_config=BaseRepoConfig(), repo_path=repo_path, bot_commit=False) if op.get_active_branch_or_commit() != commit: op.discard_changes() op.create_remote("origin", url) @@ -104,8 +101,7 @@ def create_from_repo(cls, repo_path: str, url: str) -> Self: remote_head = git_cli.remotes.origin.refs[git_cli.active_branch.name].commit # If up to date, use existing repo if local_head.hexsha == remote_head.hexsha: - default_branch = git_cli.active_branch.name - return cls(repo_config=BaseRepoConfig(), repo_path=repo_path, default_branch=default_branch, bot_commit=False) + return cls(repo_config=BaseRepoConfig(), repo_path=repo_path, bot_commit=False) except Exception: # If any git operations fail, fallback to fresh clone pass @@ -121,9 +117,8 @@ def create_from_repo(cls, repo_path: str, url: str) -> Self: # Initialize with the cloned repo git_cli = GitCLI(repo_path) - default_branch = git_cli.active_branch.name - return cls(repo_config=BaseRepoConfig(), repo_path=repo_path, default_branch=default_branch, bot_commit=False) + return cls(repo_config=BaseRepoConfig(), repo_path=repo_path, bot_commit=False) #################################################################################################################### # PROPERTIES diff --git a/src/codegen/git/repo_operator/repo_operator.py b/src/codegen/git/repo_operator/repo_operator.py index a5b4bd46d..4f12f06bb 100644 --- a/src/codegen/git/repo_operator/repo_operator.py +++ b/src/codegen/git/repo_operator/repo_operator.py @@ -86,7 +86,7 @@ def git_diff(self) -> str: @property def default_branch(self) -> str: - return self._default_branch + return self._default_branch or self.git_cli.active_branch.name @abstractmethod def codeowners_parser(self) -> CodeOwnersParser | None: ... diff --git a/src/codegen/sdk/code_generation/current_code_codebase.py b/src/codegen/sdk/code_generation/current_code_codebase.py index ba670e548..1a483d1c0 100644 --- a/src/codegen/sdk/code_generation/current_code_codebase.py +++ b/src/codegen/sdk/code_generation/current_code_codebase.py @@ -36,7 +36,7 @@ def get_current_code_codebase(config: CodebaseConfig = DefaultConfig, subdirecto """Returns a Codebase for the code that is *currently running* (i.e. the Codegen repo)""" codegen_repo_path = get_graphsitter_repo_path() logger.info(f"Creating codebase from repo at: {codegen_repo_path} with base_path {get_codegen_codebase_base_path()}") - op = LocalRepoOperator(repo_path=codegen_repo_path, default_branch="develop", bot_commit=False, repo_config=BaseRepoConfig(respect_gitignore=False)) + op = LocalRepoOperator(repo_path=codegen_repo_path, bot_commit=False, repo_config=BaseRepoConfig(respect_gitignore=False)) config = config.model_copy(update={"base_path": get_codegen_codebase_base_path()}) projects = [ProjectConfig(repo_operator=op, programming_language=ProgrammingLanguage.PYTHON, subdirectories=subdirectories, base_path=get_codegen_codebase_base_path())] codebase = Codebase(projects=projects, config=config) diff --git a/src/codegen/sdk/core/codebase.py b/src/codegen/sdk/core/codebase.py index 539864e41..82123d2db 100644 --- a/src/codegen/sdk/core/codebase.py +++ b/src/codegen/sdk/core/codebase.py @@ -142,7 +142,7 @@ def __init__( repo_path = os.path.abspath(repo_path) repo_config = BaseRepoConfig() main_project = ProjectConfig( - repo_operator=LocalRepoOperator(repo_config=repo_config, repo_path=repo_path, default_branch="main"), + repo_operator=LocalRepoOperator(repo_config=repo_config, repo_path=repo_path), programming_language=determine_project_language(repo_path), ) projects = [main_project] diff --git a/tests/shared/codemod/models.py b/tests/shared/codemod/models.py index 35106d27b..247b06193 100644 --- a/tests/shared/codemod/models.py +++ b/tests/shared/codemod/models.py @@ -73,7 +73,7 @@ def to_op(self, name: str, token: str | None) -> LocalRepoOperator: if '[credential "https://github.codegen.app"]' not in (Path.home() / ".gitconfig").read_text(): os.system("gh auth login -h github.codegen.app") os.system("gh auth setup-git -h github.codegen.app") - return LocalRepoOperator.create_from_commit(str(base_path), self.default_branch, self.commit, url) + return LocalRepoOperator.create_from_commit(str(base_path), self.commit, url) @dataclass