Skip to content
Open
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
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)
Comment on lines +18 to +23
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)
Comment on lines +25 to +27
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


# 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")
Comment on lines +33 to +34
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")
Comment on lines +39 to +40
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")
Comment on lines +45 to +48
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_file utility function

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")
Comment on lines +54 to +55
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")
Comment on lines +60 to +61
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")
Comment on lines +67 to +68
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(["shapes.txt"], verbose)
commit("shapes.txt: Add some shapes", verbose)