Skip to content
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

Cleanup #33

Merged
merged 3 commits into from
Jan 14, 2024
Merged
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
3 changes: 3 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Don't warn about expressions not being expanded in single quotes
# We use faaaar to many strings which are `eval`d and should only then capture the variables
disable=SC2016

# Don't warn about unused variables, since we have the helper `replace` that only takes in the variable name and dereferences it, but shellcheck does not understand that
disable=SC2034
16 changes: 8 additions & 8 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ALMOST_CREDITS_HASH="$(git hash-object -w "$DOCDIR/almost_credits.txt")"
CREDITS_HASH="$(tr 'A-Za-z' 'N-ZA-Mn-za-m' < "$DOCDIR/credits.txt" | git hash-object -w --stdin)"
NUMBER_OF_NUGGITS="$(wc -l <"$DOCDIR/nuggits")"

replace_placeholders "$DOCDIR/redeem-nuggit.sh" > ./.git/redeem.nuggit
replace NUMBER_OF_NUGGITS ALMOST_CREDITS_HASH CREDITS_HASH "$DOCDIR/redeem-nuggit.sh" > ./.git/redeem.nuggit
chmod a=rx ./.git/redeem.nuggit

# ------------------------------------------------------------------------------------------- #
Expand All @@ -76,7 +76,7 @@ commit -m "Initial Commit"

# ------------------------------------------------------------------------------------------- #
create_chapter branches
git switch -c branches-explained
git switch main -c branches-explained
cp "$DOCDIR/04_branch/branch.md" .
git add branch.md
commit -m "WIP: add description on branches
Expand All @@ -103,7 +103,7 @@ commit -m "WIP branch: add explanation on how to list local branches"
# ------------------------------------------------------------------------------------------- #
create_chapter commit
git switch --detach main
replace_placeholders "$DOCDIR/03_commit/commit.md" > commit.md
replace BRANCH_COMMIT "$DOCDIR/03_commit/commit.md" > commit.md
git add commit.md
commit -m "Add description on commit"
CHAPTER_COMMIT_COMMIT="$(git rev-parse --short @)"
Expand All @@ -123,7 +123,7 @@ INTERACTIVE_REBASE_EXAMPLE_PICKS="$(git log --oneline main..@ | sed 's/^/pick /'
[...]"
# FIXME
CHAPTER_AMEND_COMMIT="$END_COMMIT"
replace_placeholders "$DOCDIR/07_rebase_merge/interactive-rebase-continued.md" >> interactive-rebase.md
replace CHAPTER_AMEND_COMMIT INTERACTIVE_REBASE_EXAMPLE_PICKS "$DOCDIR/07_rebase_merge/interactive-rebase-continued.md" >> interactive-rebase.md
git add interactive-rebase.md
commit -m "Finish describing interactive rebases

Expand All @@ -133,7 +133,7 @@ INTERACTIVE_REBASE_COMMIT="$(git rev-parse --short @)"
# ------------------------------------------------------------------------------------------- #
create_chapter rebase/merge
git switch --detach main
replace_placeholders "$DOCDIR/07_rebase_merge/combine_history.md" > combine_history.md
replace INTERACTIVE_REBASE_COMMIT "$DOCDIR/07_rebase_merge/combine_history.md" > combine_history.md
git add combine_history.md
commit -m "Add description on how to combine branches"

Expand All @@ -151,7 +151,7 @@ create_chapter push
git switch main -c working-with-others # found via "git branch --list"
git push origin main
git push --set-upstream origin @
replace_placeholders "$DOCDIR/09_push_pull/push.md" > working-with-others.md
cp "$DOCDIR/09_push_pull/push.md" working-with-others.md
git add working-with-others.md
commit -m "Explain 'git push'"

Expand Down Expand Up @@ -234,11 +234,11 @@ rm .git/hooks/* # remove all the .sample files, since they are just noise
LOCAL_CODE_EXECUTION_HASH="$(echo "LocalCodeExecution" | git hash-object --stdin | git hash-object --stdin)"
for filep in "$DOCDIR/hooks/"*; do
file="$(basename "$filep")"
replace_placeholders "$DOCDIR/hooks/$file" > ".git/hooks/$file.orig"
cp "$DOCDIR/hooks/$file" ".git/hooks/$file.orig"
chmod +x ".git/hooks/$file.orig"
done
while read -r hook; do
replace_placeholders "$DOCDIR/hook_preamble.sh" > ".git/hooks/$hook"
replace LOCAL_CODE_EXECUTION_HASH "$DOCDIR/hook_preamble.sh" > ".git/hooks/$hook"
chmod +x ".git/hooks/$hook"
done < "$DOCDIR/all-git-hooks"
# Did you read the comment above?
Expand Down
17 changes: 8 additions & 9 deletions lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ add_player_config() {
git config --local --add alias.redeem-nuggit '!$(git rev-parse --show-toplevel)/.git/redeem.nuggit'
}

replace_placeholders() {
sed -e "s/CHAPTER_AMEND_COMMIT/$CHAPTER_AMEND_COMMIT/" \
-e "s/BRANCH_COMMIT/$BRANCH_COMMIT/" \
-e "s/NUMBER_OF_NUGGITS/$NUMBER_OF_NUGGITS"/ \
-e "s/ALMOST_CREDITS_HASH/$ALMOST_CREDITS_HASH"/ \
-e "s/CREDITS_HASH/$CREDITS_HASH"/ \
-e "s/LOCAL_CODE_EXECUTION_HASH/$LOCAL_CODE_EXECUTION_HASH/" \
-e "s/INTERACTIVE_REBASE_EXAMPLE_PICKS/$INTERACTIVE_REBASE_EXAMPLE_PICKS/" \
-e "s/INTERACTIVE_REBASE_COMMIT/$INTERACTIVE_REBASE_COMMIT/" "$1"
# Useage:
# replace MV_VAR1 MY_VAR2 [...] input_file > output_file
replace () {
local input_file="${!#}" # last argument
local variable_names=("${@:1:$#-1}") # all but the last argument
for var in "${variable_names[@]}"; do
printf "s/%s/%s/g\n" "$var" "${!var}" # search variable name and replace with variable content
done | sed -f- "$input_file" # read sed instructions from std-in
}

create_chapter() {
Expand Down
2 changes: 2 additions & 0 deletions src/credits/the-end.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Success! 🥳

nuggit: ThisWasATriumph

You played through all the available chapters that are available right now. Did you also collect all the nuggits?
1 change: 1 addition & 0 deletions src/nuggits
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ CuriosityKilledTheCat
ItsAllAboutTheRebase
PushItToTheLimits
PullMeUnder
ThisWasATriumph
5 changes: 5 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ expect "cat .git/redeem.nuggit" to contain CuriosityKilledTheCat
redeem_nuggit CuriosityKilledTheCat
'

it 'ThisWasATripmph shown in end chapter' '
expect "cat success.md" to contain "nuggit: ThisWasATriumph"
redeem_nuggit ThisWasATriumph
'

check_redeem() {
while read -r nuggit; do
expect "git redeem-nuggit '$nuggit'" to contain "You have found all the little nuggits?! Very impressive!"
Expand Down