Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions merge_undo/.gitmastery-exercise.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"exercise_name": "merge-undo",
"tags": [
"git-branch",
"git-merge",
"git-reset"
],
"requires_git": true,
"requires_github": false,
"base_files": {},
"exercise_repo": {
"repo_type": "local",
"repo_name": "play-characters",
"repo_title": null,
"create_fork": null,
"init": true
}
}
41 changes: 41 additions & 0 deletions merge_undo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# merge-undo

Scenario: You are keeping notes on the characters of a play that you are writing. In the main story line (which is in the `main` branch), you introduced two characters, Rick and Morty. You had two other characters in two separate branches `daughter` and `son-in-law`. Just now, you introduced these two characters to the main story line by merging the two branches to the `main` branch.

```mermaid
gitGraph
commit id: "Add Rick"
commit id: "Add morty"
branch daughter
branch son-in-law
checkout daughter
commit id: "Add Beth"
checkout son-in-law
commit id: "Add Jerry"
checkout main
commit id: "Mention Morty is grandson"
merge daughter id: "Introduce Beth"
merge son-in-law id: "Introduce Jerry"
```

However, now you realise this is premature, and wish to undo that change.

## Task

Undo the merging of branches `son-in-law` and `daughter`.

The result should be as follows:

```mermaid
gitGraph
commit id: "Add Rick"
commit id: "Add morty"
branch daughter
branch son-in-law
checkout daughter
commit id: "Add Beth"
checkout son-in-law
commit id: "Add Jerry"
checkout main
commit id: "Mention Morty is grandson"
```
Empty file added merge_undo/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions merge_undo/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
__resources__ = {"README.md": "README.md"}

from exercise_utils.file import create_or_update_file
from exercise_utils.git import add, checkout, commit, merge_with_message
from exercise_utils.gitmastery import create_start_tag


def setup(verbose: bool = False):
create_start_tag(verbose)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary


# main branch
checkout("main", False, verbose)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
checkout("main", False, verbose)

Not necessary. The default branch should always be main

create_or_update_file(
"rick.txt",
"""
Scientist
""",
)
add(["rick.txt"], verbose)
commit("Add Rick", verbose)
create_or_update_file(
"morty.txt",
"""
Boy
""",
)
add(["morty.txt"], verbose)
commit("Add morty", verbose)

# daughter branch
checkout("daughter", True, verbose)
create_or_update_file(
"beth.txt",
"""
Vet
""",
)
add(["beth.txt"], verbose)
commit("Add Beth", verbose)

# son-in-law branch
checkout("main", False, verbose) # switch to main first

checkout("son-in-law", True, verbose)
create_or_update_file(
"jerry.txt",
"""
Salesman
""",
)
add(["jerry.txt"], verbose)
commit("Add Herry", verbose)

# Append morty as a grandson
checkout("main", False, verbose)
create_or_update_file(
"morty.txt",
"""
Boy
Grandson
""",
)
add(["morty.txt"], verbose)
commit("Mention Morty is grandson", verbose)

# Merge daughter and son-in-law to main story
merge_with_message("daughter", False, "Introduce Beth", verbose)
merge_with_message("son-in-law", False, "Introduce Jerry", verbose)
41 changes: 41 additions & 0 deletions merge_undo/res/README.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to be a res/ file! The app automatically pulls the README.md

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# merge-undo

Scenario: You are keeping notes on the characters of a play that you are writing. In the main story line (which is in the `main` branch), you introduced two characters, Rick and Morty. You had two other characters in two separate branches `daughter` and `son-in-law`. Just now, you introduced these two characters to the main story line by merging the two branches to the `main` branch.

```mermaid
gitGraph
commit id: "Add Rick"
commit id: "Add morty"
branch daughter
branch son-in-law
checkout daughter
commit id: "Add Beth"
checkout son-in-law
commit id: "Add Jerry"
checkout main
commit id: "Mention Morty is grandson"
merge daughter id: "Introduce Beth"
merge son-in-law id: "Introduce Jerry"
```

However, now you realise this is premature, and wish to undo that change.

## Task

Undo the merging of branches `son-in-law` and `daughter`.

The result should be as follows:

```mermaid
gitGraph
commit id: "Add Rick"
commit id: "Add morty"
branch daughter
branch son-in-law
checkout daughter
commit id: "Add Beth"
checkout son-in-law
commit id: "Add Jerry"
checkout main
commit id: "Mention Morty is grandson"
```
Empty file added merge_undo/tests/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions merge_undo/tests/specs/base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start

# Add rick.txt and commit
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick

# Add morty.txt and commit
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit

# Create 'daughter' branch, add beth.txt and commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth

# Create 'son-in-law' branch, add jerry.txt and commit
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry

# Checkout back to main, edit morty.txt and commit.
- type: checkout
branch-name: main
- type: edit-file
filename: morty.txt
contents: |
Boy
Grandson
- type: commit
message: Mention Morty is grandson
id: final-main-commit

51 changes: 51 additions & 0 deletions merge_undo/tests/specs/detached_head.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry
- type: checkout
branch-name: main
- type: edit-file
filename: morty.txt
contents: |
Boy
Grandson
- type: commit
message: Mention Morty is grandson
- type: checkout
commit-hash: main

42 changes: 42 additions & 0 deletions merge_undo/tests/specs/main_wrong_commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry
- type: checkout
branch-name: main

56 changes: 56 additions & 0 deletions merge_undo/tests/specs/merges_not_undone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start
- type: new-file
filename: rick.txt
contents: Scientist
- type: commit
message: Add Rick
- type: new-file
filename: morty.txt
contents: Boy
- type: commit
message: Add morty
id: morty-initial-commit
- type: branch
branch-name: daughter
from-commit: morty-initial-commit
- type: checkout
branch-name: daughter
- type: new-file
filename: beth.txt
contents: Vet
- type: commit
message: Add Beth
- type: checkout
branch-name: main
- type: branch
branch-name: son-in-law
from-commit: morty-initial-commit
- type: checkout
branch-name: son-in-law
- type: new-file
filename: jerry.txt
contents: Salesman
- type: commit
message: Add Jerry
- type: checkout
branch-name: main
- type: edit-file
filename: morty.txt
contents: |
Boy
Grandson
- type: commit
message: Mention Morty is grandson
- type: merge
branch-name: daughter
message: Introduce Beth
no-ff: true
- type: merge
branch-name: son-in-law
message: introduce Jerry
no-ff: true
Loading