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
15 changes: 15 additions & 0 deletions exercise_utils/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ def tag(tag_name: str, verbose: bool) -> None:
run_command(["git", "tag", tag_name], verbose)


def annotated_tag(tag_name: str, verbose: bool) -> None:
"""Adds an annotated tag to the latest commit with the given tag_name."""
run_command(["git", "tag", "-a", tag_name], verbose)


def tag_with_options(tag_name: str, options: List[str], verbose: bool) -> None:
"""Tags with the given tag_name with specified options."""
run_command(["git", "tag", tag_name, *options], verbose)


def annotated_tag_with_options(tag_name: str, options: List[str], verbose: bool) -> None:
"""Adds an annotated tag with the given tag_name with specified options."""
run_command(["git", "tag", "-a", tag_name, *options], verbose)


def add(files: List[str], verbose: bool) -> None:
"""Adds a given list of file paths."""
run_command(["git", "add", *files], verbose)
Expand Down
68 changes: 68 additions & 0 deletions hands_on/push_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os
import subprocess
from exercise_utils.cli import run_command
from exercise_utils.git import tag_with_options, annotated_tag_with_options

__requires_git__ = True
__requires_github__ = True

REPO_NAME = "samplerepo-preferences"

def get_username() -> str:
# Cannot use run_command function because returned object should not be None.
return subprocess.run(
["gh", "api", "user", "-q", ".login"],
capture_output=True,
text=True,
).stdout.strip()


def check_same_repo_name(username: str, repo_name: str) -> None:
# Cannot use run_command function because return code is needed.
check_repo = subprocess.run(
["gh", "repo", "view", f"{username}/{repo_name}"],
capture_output=True,
text=True,
)
if check_repo.returncode == 0:
print(f"Warning: {username}/{REPO_NAME} already exists, the fork repo will be "
f"named as {username}/{REPO_NAME}-1")

def check_existing_fork(username: str, fork_owner_name: str, repo_name: str) -> None:
try:
result = subprocess.run(
["gh",
"api",
f"repos/{fork_owner_name}/{repo_name}/forks",
"-q",
f'''.[] | .owner.login | select(. =="{username}")''',
],
capture_output=True,
text=True,
check=True,
)
if result.stdout == username:
print(f"ERROR: A fork of {fork_owner_name}/{repo_name} already exists! "
"Please delete the fork and run this download operation again.\n"
"!Aborting...")
exit(1)
except subprocess.CalledProcessError as e:
print(e.stderr)
exit(1)




def download(verbose: bool):
username = get_username()
check_existing_fork(username, "git-mastery", REPO_NAME)
check_same_repo_name(username, REPO_NAME)

run_command(["gh", "repo", "fork", f"git-mastery/{REPO_NAME}", REPO_NAME, "--clone"], verbose)

os.chdir(REPO_NAME)

tag_with_options("v1.0", ["HEAD~1"], verbose)
annotated_tag_with_options("v0.9", ["HEAD~2", "-m", "First beta release"], verbose)

pass