Skip to content
Merged
Changes from 1 commit
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
70 changes: 70 additions & 0 deletions hands_on/checkout_commits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os

from exercise_utils.cli import run_command
from exercise_utils.gitmastery import create_start_tag
from exercise_utils.git import init, add, commit, tag
from pathlib import Path

__requires_git__ = True
__requires_github__ = False

def download(verbose: bool):
"""
Copy link
Member

Choose a reason for hiding this comment

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

You can remove this comment

hp-checkout-commits (T4L4)
- Creates a local repo 'things' with multiple commits and tags.
- Students can practice checking out commits by hash, tag, or HEAD~N references.
"""

target_dir = Path("things")

# Clean existing sandbox if re-downloading
if target_dir.exists():
import shutil
shutil.rmtree(target_dir)
Copy link
Member

Choose a reason for hiding this comment

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

This already happens via the app: https://git-mastery.github.io/developers/docs/architecture/download-workflow/#hands-on-download

So this check can be omitted


# Create sandbox folder
target_dir.mkdir()
os.chdir(target_dir)
Copy link
Member

Choose a reason for hiding this comment

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

I think conventionally, os.makedirs would be more consistent with the existing code

Copy link
Member

Choose a reason for hiding this comment

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

target_dir is not a variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! I apologise for the oversight. Have made the corrections. Can review now!


# Initialize git repo
init(verbose)

# Step 1: Add fruits.txt (first commit)
with open("fruits.txt", "w") as f:
f.write("apples\nbananas\ncherries\ndragon fruits\n")
Copy link
Member

Choose a reason for hiding this comment

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

Please use the given create_or_update_file utility function

add(["fruits.txt"], verbose)
commit("Add fruits.txt", verbose)

# Step 2: Update fruits.txt and commit
with open("fruits.txt", "a") as f:
f.write("elderberries\nfigs\n")
Copy link
Member

Choose a reason for hiding this comment

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

Please use the given append_file utility function

add(["fruits.txt"], verbose)
commit("Add elderberries and figs into fruits.txt", verbose)

# Step 3: Create colours.txt and shapes.txt, commit, tag 0.9
with open("colours.txt", "w") as f:
f.write("a file for colours\n")
with open("shapes.txt", "w") as f:
f.write("a file for shapes\n")
add(["colours.txt", "shapes.txt"], verbose)
commit("Add colours.txt, shapes.txt", verbose)
tag("0.9", verbose)

# Step 4: Update fruits.txt again
with open("fruits.txt", "w") as f:
f.write("apples, apricots\nbananas\nblueberries\ncherries\ndragon fruits\nfigs\n")
Copy link
Member

Choose a reason for hiding this comment

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

Use the given create_or_update utility function

add(["fruits.txt"], verbose)
commit("Update fruits list", verbose)

# Step 5: Update colours.txt and commit, tag 1.0
with open("colours.txt", "a") as f:
f.write("blue\nred\nwhite\n")
Copy link
Member

Choose a reason for hiding this comment

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

Use append_file

add(["colours.txt"], verbose)
commit("colours.txt: Add some colours", verbose)
tag("1.0", verbose)

# Step 6: Update shapes.txt and commit
with open("shapes.txt", "a") as f:
f.write("circle\noval\nrectangle\nsquare\n")
Copy link
Member

Choose a reason for hiding this comment

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

Use append_file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have updated the file with the requested changes!

add(["shapes.txt"], verbose)
commit("shapes.txt: Add some shapes", verbose)