Skip to content

Deprecate default_branch from LocalRepoOperator #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
17 changes: 6 additions & 11 deletions src/codegen/git/repo_operator/local_repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@ class LocalRepoOperator(RepoOperator):

_repo_path: str
_repo_name: str
_default_branch: str
_git_cli: GitCLI
repo_config: BaseRepoConfig

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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/git/repo_operator/repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/sdk/code_generation/current_code_codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/shared/codemod/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading