Exemple #1
0
def setup():
    # Copy repo so we can practice pulling.
    repo_name = os.getcwd()
    pull_repo_name = repo_name + PULL_SUFFIX

    if os.path.exists(pull_repo_name):
        delete = typer.confirm(
            f"The directory {pull_repo_name} already exists. Do you want to delete it?"
        )
        if not delete:
            cli.info("Not deleting.")
            raise typer.Abort()
        cli.info(f"Deleting {pull_repo_name}.")
        utils.rmtree_readonly(pull_repo_name)

    shutil.copytree(repo_name, pull_repo_name)

    # Setup original repo so we can practice pushing.
    with open(".gsc_id", "w") as f:
        f.write("push_and_pull")

    res = subprocess.run(["git", "add", ".gsc_id"], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to add gsc_id. Contact us for help.")

    res = subprocess.run(["git", "commit", "-m", COMMIT_MSG], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError(
            "Failed to setup Git Scientist. Contact us for help."
        )
Exemple #2
0
def setup():
    # Make sure we're on the master branch
    subprocess.run(["git", "checkout", "master"], stdout=PIPE, stderr=PIPE)

    # Commit a change which needs to be amended
    codefile = pathlib.Path(FILE_NAME)
    code = codefile.read_text()
    changed = code.replace(
        """
def subtract(x, y):
    return x + y
""",
        """
def subtract(x, y):
    # TODO: delete this comment
    return x - y
""",
    )
    codefile.write_text(changed)

    res = subprocess.run(["git", "add", FILE_NAME], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to add code change to master.")

    res = subprocess.run(["git", "commit", "-m", MASTER_COMMIT_MSG],
                         stdout=PIPE,
                         stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to commit to master.")

    cli.info(
        "\nUse `git status` and `git log` to take a look at what's changed in your local repo.\n"
        "When you're ready to start, amend the most recent commit.\n")
Exemple #3
0
def setup():
    state = {}
    # Make sure we're on the master branch
    subprocess.run(["git", "checkout", "master"], stdout=PIPE, stderr=PIPE)
    # Create branch
    res = subprocess.run(["git", "branch", BRANCH_NAME], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError(
            f"Failed to create branch `{BRANCH_NAME}`. Run `gsc reset`."
        )

    codefile = pathlib.Path(FILE_NAME)

    # Create commit on master
    cli.info("Implementing the divide function incorrectly on master.")
    code = codefile.read_text()
    implemented_divide = code.replace(
        """
def divide(x, y):
    return "Hello World"
""",
        """
def divide(x, y):
    # This is probably correct - don't have time to test
    return x % y
""",
    )
    codefile.write_text(implemented_divide)

    res = subprocess.run(["git", "add", FILE_NAME], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to add code change to master.")

    res = subprocess.run(
        ["git", "commit", "-m", MASTER_COMMIT_MSG], stdout=PIPE, stderr=PIPE
    )
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to commit to master.")

    res = subprocess.run(["git", "rev-parse", "HEAD"], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to get hash of commit on master.")
    state["master_hash"] = res.stdout.decode("utf-8").strip()

    # Push master branch
    cli.info("Pushing master.")
    res = subprocess.run(["git", "push"], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to push commit.")

    # Switch branch
    cli.info("Switching branch.")
    subprocess.run(["git", "checkout", BRANCH_NAME], stdout=PIPE, stderr=PIPE)

    # Create local commit
    cli.info(f"Implementing the divide function correctly on branch `{BRANCH_NAME}`.")
    implemented_divide = code.replace(
        """
def divide(x, y):
    return "Hello World"
""",
        """
def divide(x, y):
    return x / y
""",
    )
    codefile.write_text(implemented_divide)

    res = subprocess.run(["git", "add", FILE_NAME], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to add code change to branch.")

    res = subprocess.run(
        ["git", "commit", "-m", BRANCH_COMMIT_MSG], stdout=PIPE, stderr=PIPE
    )
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to commit to branch.")

    # Push local branch
    cli.info(f"Pushing {BRANCH_NAME}.")
    res = subprocess.run(
        ["git", "push", "--set-upstream", "origin", BRANCH_NAME],
        stdout=PIPE,
        stderr=PIPE,
    )
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to push commit.")

    # Back to master
    cli.info("Switching back to master.")
    subprocess.run(["git", "checkout", "master"], stdout=PIPE, stderr=PIPE)

    # Stash master commit hash for verification later
    pathlib.Path(".gsc_state").write_text(json.dumps(state))

    cli.info(
        "\nUse `git status`, `git log`, and `git branch` to take a look at what's changed in your local repo.\n"
        f"When you're ready to start, rebase the `{BRANCH_NAME}` branch onto master and push it.\n"
    )
Exemple #4
0
def setup():
    state = {}
    # Make sure we're on the master branch
    subprocess.run(["git", "checkout", "master"], stdout=PIPE, stderr=PIPE)
    # Create branch
    res = subprocess.run(["git", "branch", BRANCH_NAME],
                         stdout=PIPE,
                         stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError(
            f"Failed to create branch `{BRANCH_NAME}`. Run `gsc reset`.")

    codefile = pathlib.Path(FILE_NAME)

    # Create commit on master
    cli.info("Implementing the subtract function incorrectly on master.")
    code = codefile.read_text()
    implemented_subtract = code.replace(
        """
def subtract(x, y):
    return x + y
""",
        """
def subtract(x, y):
    if x > 0:
        return x - y
    return x + y
""",
    )
    codefile.write_text(implemented_subtract)

    res = subprocess.run(["git", "add", FILE_NAME], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to add code change to master.")

    res = subprocess.run(["git", "commit", "-m", MASTER_COMMIT_MSG],
                         stdout=PIPE,
                         stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to commit to master.")

    res = subprocess.run(["git", "rev-parse", "HEAD"],
                         stdout=PIPE,
                         stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError(
            "Failed to get hash of commit on master.")
    state["master_hash"] = res.stdout.decode("utf-8").strip()

    # Switch branch
    cli.info("Switching branch.")
    subprocess.run(["git", "checkout", BRANCH_NAME], stdout=PIPE, stderr=PIPE)

    # Create local commit
    cli.info(
        f"Implementing the subtract function correctly on branch `{BRANCH_NAME}`."
    )
    implemented_subtract = code.replace(
        """
def subtract(x, y):
    return x + y
""",
        """
def subtract(x, y):
    return x - y
""",
    )
    codefile.write_text(implemented_subtract)

    res = subprocess.run(["git", "add", FILE_NAME], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to add code change to branch.")

    res = subprocess.run(["git", "commit", "-m", BRANCH_COMMIT_MSG],
                         stdout=PIPE,
                         stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to commit to branch.")

    cli.info("Switching back to master.")
    subprocess.run(["git", "checkout", "master"], stdout=PIPE, stderr=PIPE)

    # Stash master commit hash for verification later
    pathlib.Path(".gsc_state").write_text(json.dumps(state))

    cli.info(
        "\nUse `git status`, `git log`, and `git branch` to take a look at what's changed in your local repo.\n"
        "When you're ready to start the exercise, switch to the `fix-subtract` branch and try to rebase onto master.\n"
    )
Exemple #5
0
def setup():
    state = {}
    # Backup .git
    shutil.copytree(".git", ".git.bak")

    codefile = pathlib.Path(FILE_NAME)

    # Create remote commit & push
    cli.info("Implementing the subtract function.")
    code = codefile.read_text()
    implemented_subtract = code.replace(
        """
def subtract(x, y):
    pass
""",
        """
def subtract(x, y):
    return x - y
""",
    )
    codefile.write_text(implemented_subtract)

    res = subprocess.run(["git", "add", FILE_NAME], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to add first code change.")

    res = subprocess.run(
        ["git", "commit", "-m", REMOTE_COMMIT_MSG], stdout=PIPE, stderr=PIPE
    )
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to commit first code change.")

    res = subprocess.run(["git", "rev-parse", "HEAD"], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to get hash of first commit.")
    state["remote_hash"] = res.stdout.decode("utf-8").strip()

    cli.info("Pushing the commit.")
    res = subprocess.run(["git", "push"], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to push commit.")

    # Restore .git
    cli.info("Forgetting about remote commit.")
    utils.rmtree_readonly(".git")
    shutil.copytree(".git.bak", ".git")
    utils.rmtree_readonly(".git.bak")

    # Create local commit
    cli.info("Implementing the divide function.")
    implemented_divide = code.replace(
        """
def divide(x, y):
    pass
""",
        """
def divide(x, y):
    return x / y
""",
    )
    codefile.write_text(implemented_divide)

    res = subprocess.run(["git", "add", FILE_NAME], stdout=PIPE, stderr=PIPE)
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to add second code change.")

    res = subprocess.run(
        ["git", "commit", "-m", LOCAL_COMMIT_MSG], stdout=PIPE, stderr=PIPE
    )
    if res.returncode != 0:
        raise setup_exercise.SetupError("Failed to commit second code change.")

    # Stash remote commit hash for verification later
    pathlib.Path(".gsc_state").write_text(json.dumps(state))

    cli.info(
        "\nUse `git status` and `git log` to take a look at what's changed in your local repo.\n"
        "When you're ready to start the exercise, try `git push`.\n"
    )