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 exercise_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ def run_command_no_exit(command: List[str], verbose: bool) -> Optional[str]:
if verbose:
print(e.stderr)
return None


def run_command_with_code(command: List[str], verbose: bool) -> tuple[Optional[str], int]:
"""Runs the given command and returns (output, return code)."""
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
check=True,
)
if verbose:
print(result.stdout)
return result.stdout, result.returncode
except subprocess.CalledProcessError as e:
if verbose:
print(e.stderr)
return e.stderr, e.returncode
5 changes: 5 additions & 0 deletions exercise_utils/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,8 @@ def track_remote_branch(remote: str, branch: str, verbose: bool) -> None:
def remove_remote(remote: str, verbose: bool) -> None:
"""Removes a given remote."""
run_command(["git", "remote", "rm", remote], verbose)


def add_remote(remote: str, remote_url: str, verbose: bool) -> None:
"""Adds a remote with the given name and URL."""
run_command(["git", "remote", "add", remote, remote_url], verbose)
69 changes: 69 additions & 0 deletions hands_on/populate_remote.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os

from exercise_utils.cli import run_command, run_command_with_code
from exercise_utils.file import create_or_update_file, append_to_file
from exercise_utils.git import add, init, commit, add_remote

__requires_git__ = True
__requires_github__ = True

REPO_NAME = "gitmastery-things"


def download(verbose: bool):
_setup_local_repository(verbose)
_create_things_repository(verbose)
_link_repositories(verbose)


def _setup_local_repository(verbose: bool):
_initialize_workspace(verbose)
create_or_update_file("fruits.txt", """
apples
bananas
cherries
dragon fruits
""",
)
add(["fruits.txt"], verbose)

append_to_file("fruits.txt", "figs")
add(["fruits.txt"], verbose)
commit("Insert figs into fruits.txt", verbose)

create_or_update_file("colours.txt", """
a file for colours
""",
)
create_or_update_file("shapes.txt", """
a file for shapes
""",
)
add(["colours.txt", "shapes.txt"], verbose)
commit("Add colours.txt, shapes.txt", verbose)


def _create_things_repository(verbose: bool):
"""Create the gitmastery-things repository, deleting any existing ones."""
_, return_code = run_command_with_code(["gh", "repo", "view", _get_full_repo_name(verbose)], verbose)
if return_code == 0:
run_command(["gh", "repo", "delete", REPO_NAME, "--yes"], verbose)

run_command(["gh", "repo", "create", REPO_NAME, "--public"], verbose)


def _link_repositories(verbose: bool):
full_repo_name = _get_full_repo_name(verbose)
add_remote("origin", f"https://github.com/{full_repo_name}", verbose)


def _initialize_workspace(verbose: bool):
os.makedirs("things")
os.chdir("things")
init(verbose)


def _get_full_repo_name(verbose: bool) -> str:
output = run_command(["gh", "api", "user", "-q", ".login"], verbose)
username = output.strip() if output else ""
return f"{username}/{REPO_NAME}"