Skip to content

Commit 2884e0c

Browse files
committed
[branch-rename] Setup verification
1 parent cdbb286 commit 2884e0c

1 file changed

Lines changed: 71 additions & 3 deletions

File tree

branch_rename/verify.py

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,79 @@
1+
from git import Repo
12
from git_autograder import (
2-
GitAutograderOutput,
33
GitAutograderExercise,
4+
GitAutograderOutput,
45
GitAutograderStatus,
56
)
67

8+
TRY_QUICK_FIX_STILL_EXISTS = (
9+
"Branch 'try-quick-fix' still exists! Remember to rename it to 'fix-scrolling-bug'"
10+
)
11+
FIX_SCROLLING_BUG_MISSING = "Branch 'fix-scrolling-bug' is missing, did you correctly rename the branch 'try-quick-fix' to 'fix-scrolling-bug'?"
12+
NO_RENAME_EVIDENCE_TRY_QUICK_FIX = (
13+
"Branch 'try-quick-fix' was not renamed to 'fix-scrolling-bug'!"
14+
)
15+
16+
IMPROVE_LOADING_LOCAL_MISSING = "Local branch 'improve-loading' is missing, did you correctly rename the branch 'improve-loadding' to 'improve-loading'?"
17+
NO_RENAME_EVIDENCE_IMPROVE_LOADING = (
18+
"Local branch 'improve-loadding' was not renamed to 'improve-loading'!"
19+
)
20+
IMPROVE_LOADING_REMOTE_MISSING = "Remote branch 'improve-loading' is missing, did you correctly push the renamed branch to the remote?"
21+
IMPROVE_LOADING_REMOTE_OLD_PRESENT = "Remote branch 'improve-loadding' still exists! Remember to rename it to 'improve-loadding'"
22+
23+
24+
def branch_has_rename_evidence(
25+
exercise: GitAutograderExercise, new_branch: str, old_branch: str
26+
) -> bool:
27+
branch = exercise.repo.branches.branch_or_none(new_branch)
28+
if branch is None:
29+
return False
30+
31+
expected = f"Branch: renamed refs/heads/{old_branch} to refs/heads/{new_branch}"
32+
for entry in branch.reflog:
33+
if entry.message == expected:
34+
return True
35+
return False
36+
737

838
def verify(exercise: GitAutograderExercise) -> GitAutograderOutput:
9-
# INSERT YOUR GRADING CODE HERE
39+
repo: Repo = exercise.repo.repo
40+
41+
# try-quick-fix -> fix-scrolling-bug
42+
local_branches = [h.name for h in repo.heads]
43+
if "try-quick-fix" in local_branches:
44+
raise exercise.wrong_answer([TRY_QUICK_FIX_STILL_EXISTS])
45+
46+
if "fix-scrolling-bug" not in local_branches:
47+
raise exercise.wrong_answer([FIX_SCROLLING_BUG_MISSING])
48+
49+
if not branch_has_rename_evidence(exercise, "fix-scrolling-bug", "try-quick-fix"):
50+
raise exercise.wrong_answer([NO_RENAME_EVIDENCE_TRY_QUICK_FIX])
51+
52+
# improve-loadding -> improve-loading
53+
if "improve-loading" not in local_branches:
54+
raise exercise.wrong_answer([IMPROVE_LOADING_LOCAL_MISSING])
55+
56+
if not branch_has_rename_evidence(exercise, "improve-loading", "improve-loadding"):
57+
raise exercise.wrong_answer([NO_RENAME_EVIDENCE_IMPROVE_LOADING])
58+
59+
# Fetch latest remote state
60+
for remote in repo.remotes:
61+
remote.fetch(prune=True)
62+
63+
# Remote branch checks
64+
remote_branches = []
65+
for remote in repo.remotes:
66+
remote_branches.extend([ref.name for ref in remote.refs])
67+
68+
if not any(ref.endswith("improve-loading") for ref in remote_branches):
69+
raise exercise.wrong_answer([IMPROVE_LOADING_REMOTE_MISSING])
70+
71+
if any(ref.endswith("improve-loadding") for ref in remote_branches):
72+
raise exercise.wrong_answer([IMPROVE_LOADING_REMOTE_OLD_PRESENT])
1073

11-
return exercise.to_output([], GitAutograderStatus.SUCCESSFUL)
74+
return exercise.to_output(
75+
[
76+
"Great work with renaming the branches on both your local and remote repositories!"
77+
],
78+
GitAutograderStatus.SUCCESSFUL,
79+
)

0 commit comments

Comments
 (0)