|
| 1 | +import re |
1 | 2 | from git_autograder import ( |
2 | 3 | GitAutograderOutput, |
3 | 4 | GitAutograderExercise, |
|
11 | 12 | RENAMED_LOCAL_MISSING = f"Local branch {RENAMED_BRANCH} is missing." |
12 | 13 | STU_REMOTE_PRESENT = f"Remote branch {STU_BRANCH} still exists." |
13 | 14 | RENAMED_REMOTE_MISSING = f"Remote branch {RENAMED_BRANCH} is missing." |
| 15 | +NO_RENAME_EVIDENCE = f"Local branch '{STU_BRANCH}' was not renamed to '{RENAMED_BRANCH}'!" |
| 16 | +RESET_MESSAGE = 'If needed, reset the repository using "gitmastery progress reset" and start again.' |
| 17 | + |
| 18 | + |
| 19 | +def branch_has_rename_evidence( |
| 20 | + exercise: GitAutograderExercise, new_branch: str, old_branch: str |
| 21 | +) -> bool: |
| 22 | + """Performs a DFS on the branch renames starting with STU till S-to-Z. |
| 23 | +
|
| 24 | + This is necessary since the renames could be performed in parts: |
| 25 | +
|
| 26 | + STU -> S-to-U -> S-to-Z |
| 27 | + """ |
| 28 | + branch = exercise.repo.branches.branch(new_branch) |
| 29 | + |
| 30 | + rename_regex = re.compile("^renamed refs/heads/(.+) to refs/heads/(.+)$") |
| 31 | + for entry in branch.reflog[::-1]: |
| 32 | + match_group = rename_regex.match(entry.message) |
| 33 | + if match_group is None: |
| 34 | + continue |
| 35 | + original = match_group.group(1) |
| 36 | + new = match_group.group(2) |
| 37 | + if original == old_branch: |
| 38 | + old_branch = new |
| 39 | + |
| 40 | + return old_branch == new_branch |
14 | 41 |
|
15 | 42 |
|
16 | 43 | def verify(exercise: GitAutograderExercise) -> GitAutograderOutput: |
@@ -42,6 +69,9 @@ def verify(exercise: GitAutograderExercise) -> GitAutograderOutput: |
42 | 69 |
|
43 | 70 | if comments: |
44 | 71 | raise exercise.wrong_answer(comments) |
| 72 | + |
| 73 | + if not branch_has_rename_evidence(exercise, RENAMED_BRANCH, STU_BRANCH): |
| 74 | + raise exercise.wrong_answer([NO_RENAME_EVIDENCE, RESET_MESSAGE]) |
45 | 75 |
|
46 | 76 | return exercise.to_output( |
47 | 77 | ["Nice work renaming the branch locally and on the remote!"], |
|
0 commit comments