Skip to content

Log and order #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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 log_and_order/.gitmastery-exercise.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"exercise_name": "log-and-order",
"tags": [
"git-log"
],
"requires_git": true,
"requires_github": false,
"base_files": {
"answers.txt": "answers.txt"
},
"exercise_repo": {
"repo_type": "local",
"repo_name": "crime-spree",
"repo_title": null,
"create_fork": null,
"init": true
}
}
16 changes: 16 additions & 0 deletions log_and_order/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# log-and-order

You are tasked with tracking down a criminal who was on a crime spree and has managed to escape. He has hidden his crimes in his log which he stores as a commit history, along with the victims of his crimes and the times he has committed them.

## Task

Use `git log` to track down his crime spree and answer the following questions:

1. Where is he currently hiding at? (which SHA hash does `HEAD` point to?)
2. When did he rob Alice Bakersfield? (find the date of the commit whose description details the robbery)
3. What is the criminal's real name? (find the commit whose author is not "Anonymous")
4. What did the criminal do on 13 September 2024? (what is the description of the commit on 13 September 2024?)
5. What was the very first crime he had committed? (what is the message of the very first commit?)

Answer these questions in the `answers.txt` file and run `gitmastery verify` to check if your answers are right!

Empty file added log_and_order/__init__.py
Empty file.
124 changes: 124 additions & 0 deletions log_and_order/download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import subprocess
from sys import exit
from typing import List, Optional


def run_command(command: List[str], verbose: bool) -> Optional[str]:
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
check=True,
)
if verbose:
print(result.stdout)
return result.stdout
except subprocess.CalledProcessError as e:
if verbose:
print(e.stderr)
exit(1)


def commit(
author: str,
email: str,
date: str,
message: str,
description: Optional[str],
verbose: bool,
) -> None:
if description is None:
run_command(
[
"git",
"commit",
"--allow-empty",
"--date",
date,
"--author",
f"{author} <{email}>",
"-m",
message,
],
verbose,
)
else:
run_command(
[
"git",
"commit",
"--allow-empty",
"--date",
date,
"--author",
f"{author} <{email}>",
"-m",
message,
"-m",
description,
],
verbose,
)


ANONYMOUS_AUTHOR = "Anonymous"
ANONYMOUS_EMAIL = "[email protected]"
CRIMINAL_AUTHOR = "Josh Badur"
CRIMINAL_EMAIL = "[email protected]"


def setup(verbose: bool = False):
commit(
ANONYMOUS_AUTHOR,
ANONYMOUS_EMAIL,
"2024-01-05 08:00",
"Stole bicycle from Main Street",
None,
verbose,
)

commit(
ANONYMOUS_AUTHOR,
ANONYMOUS_EMAIL,
"2024-03-12 14:45",
"Vandalized statue in city park",
None,
verbose,
)

commit(
ANONYMOUS_AUTHOR,
ANONYMOUS_EMAIL,
"2024-06-21 22:30",
"Robbed Alice Bakersfield",
None,
verbose,
)

commit(
ANONYMOUS_AUTHOR,
ANONYMOUS_EMAIL,
"2024-09-13 03:15",
"Graffiti on police station wall",
"Spray painted a giant smiley face over the precinct's main entrance.",
verbose,
)

commit(
CRIMINAL_AUTHOR,
CRIMINAL_EMAIL,
"2024-10-28 09:00",
"Oh no what have I done",
None,
verbose,
)

commit(
ANONYMOUS_AUTHOR,
ANONYMOUS_EMAIL,
"2024-11-14 07:00",
"Currently hiding at the abandoned warehouse at docks",
None,
verbose,
)
14 changes: 14 additions & 0 deletions log_and_order/res/answers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Q: Where is the criminal currently hiding at? (What is the location in commit where HEAD is pointing to?)
A:

Q: When did the criminal rob Alice Bakersfield? (Find the date of the commit whose description details the robbery)
A:

Q: What is the criminal's real name? (Find the one commit where the author is not "Anonymous")
A:

Q: What did the criminal do on 13 September 2024? (What is the description of the commit on 13 September 2024?)
A:

Q: What was the very first crime the criminal committed? (What is the message of the very first commit?)
A:
Empty file added log_and_order/tests/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions log_and_order/tests/specs/base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
initialization:
steps:
- type: commit
empty: true
message: Empty commit
id: start
12 changes: 12 additions & 0 deletions log_and_order/tests/test_verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from git_autograder import GitAutograderTestLoader

from ..verify import verify

REPOSITORY_NAME = "log-and-order"

loader = GitAutograderTestLoader(__file__, REPOSITORY_NAME, verify)


def test():
with loader.load("specs/base.yml", "start"):
pass
75 changes: 75 additions & 0 deletions log_and_order/verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from git_autograder import (
GitAutograderOutput,
GitAutograderExercise,
GitAutograderStatus,
)
from git_autograder.answers.rules import NotEmptyRule, HasExactValueRule
from git_autograder.answers.rules.answer_rule import AnswerRule
from git_autograder.answers import GitAutograderAnswersRecord

QUESTION_ONE = "Where is the criminal currently hiding at? (What is the location in commit where HEAD is pointing to?)"
QUESTION_TWO = "When did the criminal rob Alice Bakersfield? (Find the date of the commit whose description details the robbery)"
QUESTION_THREE = 'What is the criminal\'s real name? (Find the one commit where the author is not "Anonymous")'
QUESTION_FOUR = "What did the criminal do on 13 September 2024? (What is the description of the commit on 13 September 2024?)"
QUESTION_FIVE = "What was the very first crime the criminal committed? (What is the message of the very first commit?)"


class ContainsValueRule(AnswerRule):
MISSING_VALUE = "Answer for {question} missing the right values"

def __init__(self, value: str) -> None:
super().__init__()
self.value = value

def apply(self, answer: GitAutograderAnswersRecord) -> None:
if self.value not in answer.answer.strip().lower():
raise Exception(self.MISSING_VALUE.format(question=answer.question))


class ContainsOneOfValueRule(AnswerRule):
MISMATCH_VALUE = "Answer for {question} did not contain the right value"

def __init__(self, *values: str) -> None:
super().__init__()
self.values = values

def apply(self, answer: GitAutograderAnswersRecord) -> None:
for value in self.values:
if value in answer.answer.strip().lower():
break
else:
raise Exception(self.MISMATCH_VALUE.format(question=answer.question))


def verify(exercise: GitAutograderExercise) -> GitAutograderOutput:
exercise.answers.add_validation(
QUESTION_ONE,
NotEmptyRule(),
ContainsOneOfValueRule(
"abandoned warehouse at docks",
"abandoned warehouse",
"warehouse",
"warehouse at docks",
),
).add_validation(
QUESTION_TWO,
NotEmptyRule(),
ContainsOneOfValueRule(
"2024-06-21",
"2024-06-21 22:30",
"21 June 2024",
"21 Jun 2024",
"21/06/2024",
),
).add_validation(
QUESTION_THREE, NotEmptyRule(), HasExactValueRule("Josh Badur")
).add_validation(
QUESTION_FOUR,
HasExactValueRule(
"Spray painted a giant smiley face over the precinct's main entrance."
),
).add_validation(
QUESTION_FIVE, HasExactValueRule("Stole bicycle from Main Street")
).validate()

return exercise.to_output([], GitAutograderStatus.SUCCESSFUL)