Skip to content

Commit

Permalink
redeem-nuggit: add many comments on what we are doing
Browse files Browse the repository at this point in the history
This is now not a "threat for reverse-engineering" any more, since the
comments are stripped when "building"
  • Loading branch information
miallo committed Jan 16, 2024
1 parent b6d8d7c commit 3179d46
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/redeem-nuggit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,40 @@ already_redeemed="'$nuggit' already redeemed"
tried_before="You tried '$nuggit' before. It still isn't a valid answer... 🙄"

redeemed=0
# check if this nuggit was already_redeemed
git cat-file -e "$(echo "$already_redeemed" | git hash-object --stdin)" 2>/dev/null && redeemed=1
# The total number of nuggits is one bigger than the already committed ones
# because of the root commit, so we have to subtract 1 if this one was already
# redeemed
redeemed_nuggits="$(($(git rev-list --count nuggits) - redeemed))"

# For the second last nuggit we want to show a hint that one is still missing
# to give a hint that LocalCodeExecution is self-deleting.
# CREDITS_TREE is a tree-object and CREDITS_TREE:almost means the blob with the
# name "almost" inside of it
[ "$redeemed_nuggits" -ne $((NUMBER_OF_NUGGITS - 1)) ] || git cat-file -p CREDITS_TREE:almost;

# shellcheck disable=2170
# shellcheck disable=2170 # NUMBER_OF_NUGGITS will be replaced by an integer, once we "build" it.
[ "$redeemed_nuggits" -ne NUMBER_OF_NUGGITS ] || {
git cat-file -e "$(git hash-object --stdin <<< "$((NUMBER_OF_NUGGITS - 1))" | git hash-object --stdin)" 2>/dev/null || { echo Noughty boy!; exit 1; }
# check if the player did not just add a commit to our "nuggits"
# pseudobranch by looking up if we wrote the last number of redeemed
# nuggits to the objects. This is just a very basic "cheat-detection"...
git cat-file -e "$(git hash-object --stdin <<< "$((NUMBER_OF_NUGGITS - 1))" | git hash-object --stdin)" 2>/dev/null || { echo Naughty boy!; exit 1; }
# print the final credits. See "almost" above for syntax description
# Also do rot13 on the result in order to make it just a bit harder to just
# find the blob and read it
git cat-file -p CREDITS_TREE:final | tr 'A-Za-z' 'N-ZA-Mn-za-m';
}

# if we already have redeemed this nuggit, print that it was already redeemed and exit
git cat-file -p "$(echo "$already_redeemed" | git hash-object --stdin)" 2>/dev/null && exit
# if the user already tried to submit this wrong string, print the error and exit
git cat-file -p "$(echo "$tried_before" | git hash-object --stdin)" 2>/dev/null && exit 1

# Print the success message for this nuggit if it exists
git cat-file -p "NUGGIT_DESCRIPTION_TREE:$(echo "$nuggit" | git hash-object --stdin)/success" 2>/dev/null || {
# if it does not exist, then this is not a valid nuggit (or it was
# LocalCodeExecution and that was deleted)
echo "Unfortunately that is not a valid nuggit :/ Try again!" >&2
echo "$tried_before" | git hash-object --stdin -w >/dev/null 2>&1
exit 1
Expand All @@ -48,16 +67,20 @@ commit_nuggit() { # Manage our own little "branch" manually
local tree
# get the tree object from the last commit in nuggits
tree="$(git rev-parse "nuggits^{tree}")"
# get the description from our "nuggit tree object" from the folder with
# the hash of the nuggit and inside of that the description file
description="$(git cat-file -p "NUGGIT_DESCRIPTION_TREE:$(echo "$1" | git hash-object --stdin)/description")"
# add an empty commit with the parent being nuggits and "reset nuggits to that new commit"
git commit-tree "$tree" -p "$(cat .git/nuggits)" -m "$1
$description" > .git/nuggits.bak
git commit-tree "$tree" -p "$(cat .git/nuggits)" -m "$(printf "%s\n\n" "$nuggit" "$description")" > .git/nuggits.bak
# We can't directly pipe it into the file, because it will empty it before we read it...
# Therefore write it into a backup file and then replace it
mv .git/nuggits.bak .git/nuggits
}
commit_nuggit "$nuggit"

# Print some stats for the player, so they know if they still need to look for other nuggits
echo "Number of redeemed nuggits: $redeemed_nuggits of NUMBER_OF_NUGGITS"
# Write to our database, that this nuggit is now redeemed
echo "$already_redeemed" | git hash-object --stdin -w >/dev/null 2>&1
# Write as a "cheat-detection" for the final credits how many we have redeemed
git hash-object --stdin <<< "$redeemed_nuggits"| git hash-object --stdin -w >/dev/null 2>&1

0 comments on commit 3179d46

Please sign in to comment.