Skip to content

Commit 1332f95

Browse files
christinewangcwtkucar
authored and
tkucar
committed
fix CG-9440 clean repo - clears from the default branch (#398)
1 parent cf24fbd commit 1332f95

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

src/codegen/git/repo_operator/remote_repo_operator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ def codeowners_parser(self) -> CodeOwnersParser | None:
7979
# SET UP
8080
####################################################################################################################
8181

82+
@override
83+
def clean_repo(self) -> None:
84+
"""Cleans the repo by:
85+
1. Discards any changes (tracked/untracked)
86+
2. Checks out the default branch (+ makes sure it's up to date with the remote)
87+
3. Deletes all branches except the default branch
88+
4. Deletes all remotes except origin
89+
90+
Used in SetupOption.PULL_OR_CLONE to allow people to re-use existing repos and start from a clean state.
91+
"""
92+
logger.info(f"Cleaning repo at {self.repo_path} ...")
93+
self.discard_changes()
94+
self.checkout_branch(self.default_branch, remote=True)
95+
self.clean_branches()
96+
self.clean_remotes()
97+
8298
@override
8399
def pull_repo(self) -> None:
84100
"""Pull the latest commit down to an existing local repo"""

src/codegen/git/repo_operator/repo_operator.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,11 @@ def repo_exists(self) -> bool:
159159
def clean_repo(self) -> None:
160160
"""Cleans the repo by:
161161
1. Discards any changes (tracked/untracked)
162-
2. Checks out the default branch (+ makes sure it's up to date with the remote)
163-
3. Deletes all branches except the default branch
164-
4. Deletes all remotes except origin
165-
166-
Used in SetupOption.PULL_OR_CLONE to allow people to re-use existing repos and start from a clean state.
162+
2. Deletes all branches except the checked out branch
163+
3. Deletes all remotes except origin
167164
"""
168165
logger.info(f"Cleaning repo at {self.repo_path} ...")
169166
self.discard_changes()
170-
self.checkout_branch(self.default_branch) # TODO(CG-9440): add back remote=True
171167
self.clean_branches()
172168
self.clean_remotes()
173169

@@ -277,20 +273,6 @@ def is_branch_checked_out(self, branch_name: str) -> bool:
277273
return False
278274
return self.git_cli.active_branch.name == branch_name
279275

280-
def delete_local_branch(self, branch_name: str) -> None:
281-
if branch_name not in self.git_cli.branches:
282-
logger.info(f"Branch {branch_name} does not exist locally. Skipping delete_local_branch.")
283-
return
284-
if branch_name is self.default_branch:
285-
msg = "Deleting the default branch is not implemented yet."
286-
raise NotImplementedError(msg)
287-
288-
if self.is_branch_checked_out(branch_name):
289-
self.checkout_branch(self.default_branch)
290-
291-
logger.info(f"Deleting local branch: {branch_name} ...")
292-
self.git_cli.delete_head(branch_name, force=True) # force deletes even if the branch has unmerged changes
293-
294276
def checkout_branch(self, branch_name: str | None, *, remote: bool = False, remote_name: str = "origin", create_if_missing: bool = True) -> CheckoutResult:
295277
"""Attempts to check out the branch in the following order:
296278
- Check out the local branch by name

tests/integration/codegen/git/repo_operator/test_remote_repo_operator.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
@pytest.fixture
1414
def op(repo_config, request, tmpdir):
15-
op = RemoteRepoOperator(repo_config, shallow=request.param, base_dir=tmpdir, bot_commit=False)
15+
op = RemoteRepoOperator(
16+
repo_config,
17+
shallow=request.param if hasattr(request, "param") else True,
18+
base_dir=tmpdir,
19+
bot_commit=False,
20+
)
1621
yield op
1722

1823

@@ -76,3 +81,24 @@ def test_checkout_branch_remote_already_checked_out_resets_branch(mock_git_clien
7681
assert res == CheckoutResult.SUCCESS
7782
assert len(op.git_cli.heads) == 1
7883
assert op.head_commit.hexsha == original_commit_head.hexsha
84+
85+
86+
def test_clean_repo(op: RemoteRepoOperator):
87+
num_branches = len(op.git_cli.branches)
88+
op.checkout_branch(branch_name="test_branch", create_if_missing=True)
89+
with open(f"{op.repo_path}/test.txt", "w") as f:
90+
f.write("test")
91+
op.git_cli.git.add(A=True)
92+
op.git_cli.create_remote(name="other-remote", url=op.clone_url)
93+
94+
assert op.git_cli.active_branch.name == "test_branch"
95+
assert len(op.git_cli.branches) == num_branches + 1
96+
assert len(op.git_cli.remotes) == 2
97+
assert op.git_cli.is_dirty()
98+
99+
op.clean_repo()
100+
assert not op.git_cli.is_dirty() # discards changes
101+
assert len(op.git_cli.branches) == 1 # deletes only the checked out branch
102+
assert op.git_cli.active_branch.name == op.default_branch
103+
assert len(op.git_cli.remotes) == 1 # deletes all remotes except origin
104+
assert op.git_cli.remotes[0].name == "origin"

0 commit comments

Comments
 (0)