-
Notifications
You must be signed in to change notification settings - Fork 25
Implement T8L4/glossary-branch-rename #243
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
jovnc
merged 22 commits into
git-mastery:main
from
SAN-MUYUN:exercise/glossary-branch-rename
Feb 4, 2026
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
28d630e
implement exercise glossary-branch-rename using Codex
SAN-MUYUN 3d87724
update exercise to simplify workflow
SAN-MUYUN 966b28b
update exercise
SAN-MUYUN 0dec2c9
update download logic
SAN-MUYUN c05bcb5
update download logic
SAN-MUYUN 09de6b1
update download logic
SAN-MUYUN bafc026
fix github repo name error
SAN-MUYUN f39b398
fix local repo name
SAN-MUYUN 34fad84
fix download error
SAN-MUYUN 31ce652
fix download error
SAN-MUYUN 55101a3
update forking
SAN-MUYUN 1d1b5c4
update exercise set up logic
SAN-MUYUN a1881a6
implement verify.py using Codex
SAN-MUYUN 6580f79
improve code quality
SAN-MUYUN b68fa26
improve code quality
SAN-MUYUN 40b1136
update README
SAN-MUYUN 4cb06bc
improve code readability
SAN-MUYUN eb02aaf
Merge branch 'main' of https://github.com/SAN-MUYUN/git-mastery-exerc…
SAN-MUYUN 1cd04fb
implement test_verify
SAN-MUYUN 23d4919
improve code quality
SAN-MUYUN 6ed3d5f
fix download issues
SAN-MUYUN 2e80024
chore: remove unnecessary import and format code
jovnc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "exercise_name": "glossary-branch-rename", | ||
| "tags": [ | ||
| "git-push" | ||
| ], | ||
| "requires_git": true, | ||
| "requires_github": true, | ||
| "base_files": {}, | ||
| "exercise_repo": { | ||
| "repo_type": "local", | ||
| "repo_name": "funny-glossary", | ||
| "repo_title": null, | ||
| "create_fork": null, | ||
| "init": false | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| See https://git-mastery.org/lessons/remoteBranchRename/exercise-glossary-branch-rename.html |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from exercise_utils.github_cli import clone_repo_with_gh, delete_repo, fork_repo, get_github_username, has_repo | ||
| from exercise_utils.git import remove_remote, track_remote_branch | ||
|
|
||
| UPSTREAM_REPO = "git-mastery/samplerepo-funny-glossary" | ||
| BRANCHES = ["ABC", "DEF", "STU", "VWX"] | ||
|
|
||
|
|
||
| def setup(verbose: bool = False): | ||
| username = get_github_username(verbose) | ||
| FORK_NAME = f"{username}-gitmastery-samplerepo-funny-glossary" | ||
|
|
||
| if has_repo(FORK_NAME, True, verbose): | ||
| delete_repo(FORK_NAME, verbose) | ||
|
|
||
| fork_repo(UPSTREAM_REPO, FORK_NAME, verbose, default_branch_only=False) | ||
|
|
||
| clone_repo_with_gh( | ||
| f"{username}/{FORK_NAME}", | ||
| verbose, | ||
| ".", | ||
| ) | ||
| remove_remote("upstream", verbose) | ||
|
|
||
| for branch in BRANCHES: | ||
| track_remote_branch("origin", branch, verbose) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| from contextlib import contextmanager | ||
| from typing import Iterator, Tuple | ||
|
|
||
| from git_autograder import GitAutograderStatus | ||
| from exercise_utils.test import ( | ||
| GitAutograderTest, | ||
| GitAutograderTestLoader, | ||
| assert_output, | ||
| ) | ||
| from repo_smith.repo_smith import RepoSmith | ||
|
|
||
| from .verify import ( | ||
| verify, | ||
| STU_LOCAL_PRESENT, | ||
| STU_REMOTE_PRESENT, | ||
| RENAMED_LOCAL_MISSING, | ||
| RENAMED_REMOTE_MISSING, | ||
| ) | ||
|
|
||
| REPOSITORY_NAME = "glossary-branch-rename" | ||
| BRANCHES = ["ABC", "DEF", "STU", "VWX"] | ||
| EXPECTED_NEW_BRANCH_NAME = "S-to-Z" | ||
| BRANCH_TO_RENAME = "STU" | ||
|
|
||
| loader = GitAutograderTestLoader(REPOSITORY_NAME, verify) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def base_setup() -> Iterator[Tuple[GitAutograderTest, RepoSmith]]: | ||
| with loader.start(include_remote_repo=True) as (test, rs, rs_remote): | ||
| remote_worktree_dir = rs_remote.repo.working_tree_dir | ||
|
|
||
| # set up local repo | ||
| rs.git.commit(message="Empty", allow_empty=True) | ||
| rs.git.remote_add("origin", str(remote_worktree_dir)) | ||
|
|
||
| for remote_branch_name in BRANCHES: | ||
| rs_remote.git.branch(remote_branch_name) | ||
|
|
||
| rs.git.push("origin", all=True) | ||
|
|
||
| yield test, rs | ||
|
|
||
|
|
||
| def test_base(): | ||
| with base_setup() as (test, rs): | ||
| rs.git.branch(EXPECTED_NEW_BRANCH_NAME, old_branch=BRANCH_TO_RENAME, move=True) | ||
| rs.git.push("origin", EXPECTED_NEW_BRANCH_NAME) | ||
| rs.git.push("origin", f":{BRANCH_TO_RENAME}") | ||
|
|
||
| output = test.run() | ||
| assert_output(output, GitAutograderStatus.SUCCESSFUL) | ||
|
|
||
|
|
||
| def test_no_change(): | ||
| with base_setup() as (test, _): | ||
| output = test.run() | ||
| assert_output( | ||
| output, | ||
| GitAutograderStatus.UNSUCCESSFUL, | ||
| [ | ||
| STU_LOCAL_PRESENT, | ||
| RENAMED_LOCAL_MISSING, | ||
| STU_REMOTE_PRESENT, | ||
| RENAMED_REMOTE_MISSING, | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| def test_changed_local_only(): | ||
| with base_setup() as (test, rs): | ||
| rs.git.branch(EXPECTED_NEW_BRANCH_NAME, old_branch=BRANCH_TO_RENAME, move=True) | ||
| output = test.run() | ||
| assert_output( | ||
| output, | ||
| GitAutograderStatus.UNSUCCESSFUL, | ||
| [STU_REMOTE_PRESENT, RENAMED_REMOTE_MISSING], | ||
| ) | ||
|
|
||
|
|
||
| def test_changed_local_wrong_name(): | ||
| with base_setup() as (test, rs): | ||
| rs.git.branch("S-to-X", old_branch=BRANCH_TO_RENAME, move=True) | ||
| output = test.run() | ||
| assert_output( | ||
| output, | ||
| GitAutograderStatus.UNSUCCESSFUL, | ||
| [ | ||
| RENAMED_LOCAL_MISSING, | ||
| STU_REMOTE_PRESENT, | ||
| RENAMED_REMOTE_MISSING, | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| def test_changed_remote_wrong_name(): | ||
| with base_setup() as (test, rs): | ||
| rs.git.branch("S-to-X", old_branch=BRANCH_TO_RENAME, move=True) | ||
| rs.git.push("origin", "S-to-X") | ||
| rs.git.push("origin", f":{BRANCH_TO_RENAME}") | ||
| output = test.run() | ||
| assert_output( | ||
| output, | ||
| GitAutograderStatus.UNSUCCESSFUL, | ||
| [ | ||
| RENAMED_LOCAL_MISSING, | ||
| RENAMED_REMOTE_MISSING, | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| def test_local_old_branch_still_exists(): | ||
| with base_setup() as (test, rs): | ||
| rs.git.branch(EXPECTED_NEW_BRANCH_NAME) | ||
| rs.git.push("origin", EXPECTED_NEW_BRANCH_NAME) | ||
| rs.git.push("origin", f":{BRANCH_TO_RENAME}") | ||
|
|
||
| output = test.run() | ||
| assert_output(output, GitAutograderStatus.UNSUCCESSFUL, [STU_LOCAL_PRESENT]) | ||
|
|
||
|
|
||
| def test_remote_old_branch_still_exists(): | ||
| with base_setup() as (test, rs): | ||
| rs.git.branch(EXPECTED_NEW_BRANCH_NAME, old_branch=BRANCH_TO_RENAME, move=True) | ||
| rs.git.push("origin", EXPECTED_NEW_BRANCH_NAME) | ||
|
|
||
| output = test.run() | ||
| assert_output(output, GitAutograderStatus.UNSUCCESSFUL, [STU_REMOTE_PRESENT]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from git_autograder import ( | ||
| GitAutograderOutput, | ||
| GitAutograderExercise, | ||
| GitAutograderStatus, | ||
| ) | ||
|
|
||
| STU_BRANCH = "STU" | ||
| RENAMED_BRANCH = "S-to-Z" | ||
|
|
||
| STU_LOCAL_PRESENT = f"Local branch {STU_BRANCH} still exists." | ||
| RENAMED_LOCAL_MISSING = f"Local branch {RENAMED_BRANCH} is missing." | ||
| STU_REMOTE_PRESENT = f"Remote branch {STU_BRANCH} still exists." | ||
| RENAMED_REMOTE_MISSING = f"Remote branch {RENAMED_BRANCH} is missing." | ||
|
|
||
|
|
||
| def verify(exercise: GitAutograderExercise) -> GitAutograderOutput: | ||
| local_raw = exercise.repo.repo.git.branch("--list") | ||
| local_branches = [line.strip().lstrip("* ").strip() for line in local_raw.splitlines()] | ||
|
|
||
| remote_raw = exercise.repo.repo.git.ls_remote("--heads", "origin") | ||
| remote_branches = [] | ||
| for line in remote_raw.splitlines(): | ||
| parts = line.split() | ||
| if len(parts) == 2 and parts[1].startswith("refs/heads/"): | ||
| remote_branches.append(parts[1].split("/")[-1]) | ||
|
|
||
| comments = [] | ||
|
|
||
| if STU_BRANCH in local_branches: | ||
| comments.append(STU_LOCAL_PRESENT) | ||
|
|
||
| if RENAMED_BRANCH not in local_branches: | ||
| comments.append(RENAMED_LOCAL_MISSING) | ||
|
|
||
| if STU_BRANCH in remote_branches: | ||
| comments.append(STU_REMOTE_PRESENT) | ||
|
|
||
| if RENAMED_BRANCH not in remote_branches: | ||
| comments.append(RENAMED_REMOTE_MISSING) | ||
|
|
||
| if comments: | ||
| raise exercise.wrong_answer(comments) | ||
|
|
||
| return exercise.to_output( | ||
| ["Nice work renaming the branch locally and on the remote!"], | ||
| GitAutograderStatus.SUCCESSFUL, | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be unnecessary now, why not just set rs_remote as the origin remote of rs first, then push the necessary updates from rs to rs_remote?