diff --git a/branch_forward/.gitmastery-exercise.json b/branch_forward/.gitmastery-exercise.json new file mode 100644 index 0000000..a68c6c0 --- /dev/null +++ b/branch_forward/.gitmastery-exercise.json @@ -0,0 +1,17 @@ +{ + "exercise_name": "branch-forward", + "tags": [ + "git-branch", + "git-merge" + ], + "requires_git": true, + "requires_github": false, + "base_files": {}, + "exercise_repo": { + "repo_type": "local", + "repo_name": "love-story", + "repo_title": null, + "create_fork": null, + "init": true + } +} diff --git a/branch_forward/README.md b/branch_forward/README.md new file mode 100644 index 0000000..f32c1fe --- /dev/null +++ b/branch_forward/README.md @@ -0,0 +1,26 @@ +# branch-forward + +You are outlining a story and experimenting with different plotlines. Each version lives on its own branch, and you now need to fold the right storyline back into `main` without cluttering the history. + +## Learning objectives + +- Identify when a branch can be fast-forward merged +- Use fast-forward merges to keep the commit graph linear +- [Fast-forward merges](https://nus-cs2103-ay2526s1.github.io/website/book/gitAndGithub/merge/index.html) + +## Task + +1. Review the `with-sally` and `with-ginny` branches. +2. Merge only the branch(es) that can be fast-forwarded into `main`. +3. Leave other branches (if any) unmerged. + +## Hints + + +
+ +Hint 1 + +Ensure you have switched to the destination branch before initiating the merge. + +
diff --git a/branch_forward/__init__.py b/branch_forward/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/branch_forward/download.py b/branch_forward/download.py new file mode 100644 index 0000000..e75ed57 --- /dev/null +++ b/branch_forward/download.py @@ -0,0 +1,63 @@ +from exercise_utils.file import append_to_file, create_or_update_file +from exercise_utils.git import add, checkout, commit + + +def setup(verbose: bool = False): + create_or_update_file( + "story.txt", + """ + Harry was single. + """, + ) + add(["story.txt"], verbose) + commit("Introduce Harry", verbose) + + append_to_file( + "story.txt", + """ + Harry did not have a family. + """, + ) + add(["story.txt"], verbose) + commit("Add about family", verbose) + + checkout("with-ginny", True, verbose) + append_to_file( + "story.txt", + """ + Then he met Ginny. + """, + ) + add(["story.txt"], verbose) + commit("Add about Ginny", verbose) + + checkout("main", False, verbose) + create_or_update_file( + "cast.txt", + """ + Harry + """, + ) + add(["cast.txt"], verbose) + commit("Add cast.txt", verbose) + + checkout("with-sally", True, verbose) + append_to_file( + "story.txt", + """ + Then he met Sally + """, + ) + add(["story.txt"], verbose) + commit("Mention Sally", verbose) + + checkout("with-ginny", False, verbose) + append_to_file( + "story.txt", + """ + Ginny was single too + """, + ) + add(["story.txt"], verbose) + commit("Mention Ginny is single", verbose) + diff --git a/branch_forward/tests/__init__.py b/branch_forward/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/branch_forward/tests/specs/base.yml b/branch_forward/tests/specs/base.yml new file mode 100644 index 0000000..6ddea2c --- /dev/null +++ b/branch_forward/tests/specs/base.yml @@ -0,0 +1,43 @@ +initialization: + steps: + - type: commit + empty: true + message: Introduce Harry + id: start + - type: commit + empty: true + message: Add about family + + - type: branch + branch-name: with-ginny + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Add about Ginny + + - type: checkout + branch-name: main + - type: commit + empty: true + message: Add cast.txt + + - type: branch + branch-name: with-sally + - type: checkout + branch-name: with-sally + - type: commit + empty: true + message: Mention Sally + + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Mention Ginny is single + + - type: checkout + branch-name: main + - type: merge + branch-name: with-sally + diff --git a/branch_forward/tests/specs/merge_with_sally_no_ff.yml b/branch_forward/tests/specs/merge_with_sally_no_ff.yml new file mode 100644 index 0000000..75169f8 --- /dev/null +++ b/branch_forward/tests/specs/merge_with_sally_no_ff.yml @@ -0,0 +1,44 @@ +initialization: + steps: + - type: commit + empty: true + message: Introduce Harry + id: start + - type: commit + empty: true + message: Add about family + + - type: branch + branch-name: with-ginny + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Add about Ginny + + - type: checkout + branch-name: main + - type: commit + empty: true + message: Add cast.txt + + - type: branch + branch-name: with-sally + - type: checkout + branch-name: with-sally + - type: commit + empty: true + message: Mention Sally + + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Mention Ginny is single + + - type: checkout + branch-name: main + - type: merge + branch-name: with-sally + no-ff: true + diff --git a/branch_forward/tests/specs/no_merges.yml b/branch_forward/tests/specs/no_merges.yml new file mode 100644 index 0000000..2d0ece0 --- /dev/null +++ b/branch_forward/tests/specs/no_merges.yml @@ -0,0 +1,41 @@ +initialization: + steps: + - type: commit + empty: true + message: Introduce Harry + id: start + - type: commit + empty: true + message: Add about family + + - type: branch + branch-name: with-ginny + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Add about Ginny + + - type: checkout + branch-name: main + - type: commit + empty: true + message: Add cast.txt + + - type: branch + branch-name: with-sally + - type: checkout + branch-name: with-sally + - type: commit + empty: true + message: Mention Sally + + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Mention Ginny is single + + - type: checkout + branch-name: main + diff --git a/branch_forward/tests/specs/other_branch_ff.yml b/branch_forward/tests/specs/other_branch_ff.yml new file mode 100644 index 0000000..e7f6377 --- /dev/null +++ b/branch_forward/tests/specs/other_branch_ff.yml @@ -0,0 +1,53 @@ +initialization: + steps: + - type: commit + empty: true + message: Introduce Harry + id: start + - type: commit + empty: true + message: Add about family + + - type: branch + branch-name: with-ginny + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Add about Ginny + + - type: checkout + branch-name: main + - type: commit + empty: true + message: Add cast.txt + + - type: branch + branch-name: with-sally + - type: checkout + branch-name: with-sally + - type: commit + empty: true + message: Mention Sally + + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Mention Ginny is single + + - type: checkout + branch-name: main + - type: branch + branch-name: with-ron + - type: checkout + branch-name: with-ron + - type: commit + empty: true + message: Mention Ron + + - type: checkout + branch-name: main + - type: merge + branch-name: with-ron + diff --git a/branch_forward/tests/specs/other_branch_non_ff.yml b/branch_forward/tests/specs/other_branch_non_ff.yml new file mode 100644 index 0000000..0bb536f --- /dev/null +++ b/branch_forward/tests/specs/other_branch_non_ff.yml @@ -0,0 +1,43 @@ +initialization: + steps: + - type: commit + empty: true + message: Introduce Harry + id: start + - type: commit + empty: true + message: Add about family + + - type: branch + branch-name: with-ginny + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Add about Ginny + + - type: checkout + branch-name: main + - type: commit + empty: true + message: Add cast.txt + + - type: branch + branch-name: with-sally + - type: checkout + branch-name: with-sally + - type: commit + empty: true + message: Mention Sally + + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Mention Ginny is single + + - type: checkout + branch-name: main + - type: merge + branch-name: with-ginny + diff --git a/branch_forward/tests/specs/with_sally_non_ff_then_ff.yml b/branch_forward/tests/specs/with_sally_non_ff_then_ff.yml new file mode 100644 index 0000000..1464a99 --- /dev/null +++ b/branch_forward/tests/specs/with_sally_non_ff_then_ff.yml @@ -0,0 +1,46 @@ +initialization: + steps: + - type: commit + empty: true + message: Introduce Harry + id: start + - type: commit + empty: true + message: Add about family + + - type: branch + branch-name: with-ginny + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Add about Ginny + + - type: checkout + branch-name: main + - type: commit + empty: true + message: Add cast.txt + + - type: branch + branch-name: with-sally + - type: checkout + branch-name: with-sally + - type: commit + empty: true + message: Mention Sally + + - type: checkout + branch-name: with-ginny + - type: commit + empty: true + message: Mention Ginny is single + + - type: checkout + branch-name: main + - type: bash + runs: | + git merge with-sally --no-ff -m "Non fast-forward merge" + git reset --hard 'HEAD^' + git merge with-sally + diff --git a/branch_forward/tests/test_verify.py b/branch_forward/tests/test_verify.py new file mode 100644 index 0000000..8bac588 --- /dev/null +++ b/branch_forward/tests/test_verify.py @@ -0,0 +1,60 @@ +from git_autograder import GitAutograderStatus, GitAutograderTestLoader, assert_output + +from ..verify import ( + FAST_FORWARD_REQUIRED, + ONLY_WITH_SALLY_MERGED, + verify, +) + +REPOSITORY_NAME = "branch-forward" + +loader = GitAutograderTestLoader(__file__, REPOSITORY_NAME, verify) + + +def test_success(): + with loader.load("specs/base.yml", "start") as output: + assert_output(output, GitAutograderStatus.SUCCESSFUL) + + +def test_no_merges(): + with loader.load("specs/no_merges.yml", "start") as output: + assert_output( + output, + GitAutograderStatus.UNSUCCESSFUL, + [ONLY_WITH_SALLY_MERGED], + ) + + +def test_other_branch_non_ff(): + with loader.load("specs/other_branch_non_ff.yml", "start") as output: + assert_output( + output, + GitAutograderStatus.UNSUCCESSFUL, + [ONLY_WITH_SALLY_MERGED], + ) + + +def test_other_branch_ff(): + with loader.load("specs/other_branch_ff.yml", "start") as output: + assert_output( + output, + GitAutograderStatus.UNSUCCESSFUL, + [ONLY_WITH_SALLY_MERGED], + ) + + +def test_merge_with_sally_no_ff(): + with loader.load("specs/merge_with_sally_no_ff.yml", "start") as output: + assert_output( + output, + GitAutograderStatus.UNSUCCESSFUL, + [FAST_FORWARD_REQUIRED], + ) + + +def test_merge_with_sally_fix_after_non_ff(): + with loader.load( + "specs/with_sally_non_ff_then_ff.yml", "start" + ) as output: + assert_output(output, GitAutograderStatus.SUCCESSFUL) + diff --git a/branch_forward/verify.py b/branch_forward/verify.py new file mode 100644 index 0000000..bd74085 --- /dev/null +++ b/branch_forward/verify.py @@ -0,0 +1,43 @@ +from git_autograder import ( + GitAutograderExercise, + GitAutograderOutput, + GitAutograderStatus, +) + +FAST_FORWARD_REQUIRED = ( + "You must use a fast-forward merge to bring a branch into 'main'." +) + +ONLY_WITH_SALLY_MERGED = ( + "Only one of the two starting branches can be fast-forward merged into 'main'. Do not create new branches." +) + +def verify(exercise: GitAutograderExercise) -> GitAutograderOutput: + main_branch = exercise.repo.branches.branch("main") + merge_logs = [ + entry for entry in main_branch.reflog if entry.action.startswith("merge ") + ] + + merged_branches = [entry.action[len("merge ") :] for entry in merge_logs] + + if "with-sally" not in merged_branches: + raise exercise.wrong_answer([ONLY_WITH_SALLY_MERGED]) + + latest_with_sally_merge = next( + (entry for entry in merge_logs if entry.action == "merge with-sally"), None + ) + + if latest_with_sally_merge is None: + raise exercise.wrong_answer([FAST_FORWARD_REQUIRED]) + + if latest_with_sally_merge.message != "Fast-forward": + raise exercise.wrong_answer([FAST_FORWARD_REQUIRED]) + + if any(branch != "with-sally" for branch in merged_branches): + raise exercise.wrong_answer([ONLY_WITH_SALLY_MERGED]) + + return exercise.to_output( + ["Great job fast-forward merging only 'with-sally' and cleaning up the branch!"], + GitAutograderStatus.SUCCESSFUL, + ) +