Skip to content

chore(repo operator): pass shallow param through #56

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
merged 1 commit into from
Jan 24, 2025
Merged
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
23 changes: 14 additions & 9 deletions src/codegen/git/repo_operator/remote_repo_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
class RemoteRepoOperator(RepoOperator):
"""A wrapper around GitPython to make it easier to interact with a cloned lowside repo."""

# __init__ attributes
repo_config: RepoConfig
base_dir: str

# lazy attributes
_remote_git_repo: GitRepoClient | None = None
_codeowners_parser: CodeOwnersParser | None = None
_default_branch: str | None = None
bot_commit: bool = True

# TODO: allow setting the access scope level of the lowside repo (currently it's always WRITE)
def __init__(
Expand All @@ -38,9 +40,9 @@ def __init__(
base_dir: str = "/tmp",
setup_option: SetupOption = SetupOption.PULL_OR_CLONE,
shallow: bool = True,
bot_commit: bool = True,
github_type: GithubType = GithubType.GithubEnterprise,
) -> None:
super().__init__(repo_config=repo_config, base_dir=base_dir, bot_commit=bot_commit)
super().__init__(repo_config=repo_config, base_dir=base_dir)
self.setup_repo_dir(setup_option=setup_option, shallow=shallow)

####################################################################################################################
Expand All @@ -50,8 +52,7 @@ def __init__(
@property
def remote_git_repo(self) -> GitRepoClient:
if not self._remote_git_repo:
# NOTE: local repo operator by default points at lowside (i.e. origin remote is lowside remote)
self._remote_git_repo = GitRepoClient(self.repo_config, github_type=GithubType.GithubEnterprise, access_scope=GithubScope.WRITE)
self._remote_git_repo = GitRepoClient(self.repo_config, access_scope=GithubScope.WRITE)
return self._remote_git_repo

@property
Expand All @@ -69,27 +70,31 @@ def codeowners_parser(self) -> CodeOwnersParser | None:
####################################################################################################################
# SET UP
####################################################################################################################

@override
def pull_repo(self) -> None:
"""Pull the latest commit down to an existing local repo"""
pull_repo(repo=self.repo_config, path=self.base_dir)

def clone_or_pull_repo(self) -> None:
def clone_repo(self, shallow: bool = True) -> None:
clone_repo(repo=self.repo_config, path=self.base_dir, shallow=shallow)

def clone_or_pull_repo(self, shallow: bool = True) -> None:
"""If repo exists, pulls changes. otherwise, clones the repo."""
# TODO(CG-7804): if repo is not valid we should delete it and re-clone. maybe we can create a pull_repo util + use the existing clone_repo util
if self.repo_exists():
self.clean_repo()
clone_or_pull_repo(self.repo_config, path=self.base_dir, shallow=True)
clone_or_pull_repo(self.repo_config, path=self.base_dir, shallow=shallow)

def setup_repo_dir(self, setup_option: SetupOption = SetupOption.PULL_OR_CLONE, shallow: bool = True) -> None:
os.makedirs(self.base_dir, exist_ok=True)
os.chdir(self.base_dir)
if setup_option is SetupOption.CLONE:
# if repo exists delete, then clone, else clone
clone_repo(repo=self.repo_config, path=self.base_dir, shallow=shallow)
clone_repo(shallow=shallow)
elif setup_option is SetupOption.PULL_OR_CLONE:
# if repo exists, pull changes, else clone
self.clone_or_pull_repo()
self.clone_or_pull_repo(shallow=shallow)
elif setup_option is SetupOption.SKIP:
if not self.repo_exists():
logger.warning(f"Valid git repo does not exist at {self.repo_path}. Cannot skip setup with SetupOption.SKIP.")
Expand Down
Loading