Skip to content

Commit

Permalink
add alias skip-to-nuggit-chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
miallo committed Jan 15, 2024
1 parent 6d51a95 commit 9e49bac
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ cp "$DOCDIR/origin_hooks/"* ".git/my-origin/hooks"
mkdir ".git/another-downstream/docdir"
cp "$DOCDIR/another-downstream/"* ".git/another-downstream/docdir/"

# Scripts for aliases
replace CHAPTER_INTERACTIVE_REBASE_FOLLOW "$DOCDIR/skip_to_chapter.sh" > .git/skip_to_chapter.sh
chmod +x .git/skip_to_chapter.sh

# # debug origin hooks
# while read -r hook; do
# echo '#/bin/sh
Expand Down
1 change: 1 addition & 0 deletions lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ remove_build_setup_from_config() {

add_player_config() {
git config --local --add alias.redeem-nuggit '!$(git rev-parse --show-toplevel)/.git/redeem.nuggit'
git config --local --add alias.skip-to-nuggit-chapter '!$(git rev-parse --show-toplevel)/.git/skip_to_chapter.sh'
}

# Useage:
Expand Down
2 changes: 2 additions & 0 deletions src/01_init/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ In some places you might need to use a command that you previously learned, but

If you encounter a situation, where you don't know what to do: [open an issue](https://github.com/miallo/nuggit/issues/new).

If you already know about certain git features, you can run `git skip-to-nuggit-chapter` to be presented with a list of places of the tutorial to jump to. Note: you will not collect the nuggits you skipped ;)

# First steps with `git`

`git` takes snapshots of your code, so that you can e.g. go back in the history of a file.
29 changes: 29 additions & 0 deletions src/skip_to_chapter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

# Format: <chapter name><SPACE><command to get to that chapter>
chapters=(
"branches git switch -f branches-explained"
"push/pull git switch -f working-with-others"
"log git reset --hard @ && git fetch -q && git switch -f history"
"cherry-pick git switch -f --detach CHAPTER_INTERACTIVE_REBASE_FOLLOW"
)

while true; do
printf "Chapters you can jump to (you won't be collecting nuggits ;)):\n"
for i in "${!chapters[@]}"; do
chapter_name="$(echo "${chapters["$i"]}" | cut -d " " -f 1)"
printf "%s)\t%s\n" "$i" "$chapter_name"
done
printf "q)\tquit\n\n"
read -r -p "Enter chapter to jump to: " c
if [ "$c" = q ]; then
exit
fi
if [ "$c" -lt "${#chapters[@]}" ] && [ "$c" -ge 0 ]; then
goto_chapter="$(echo "${chapters["$c"]}" | cut -d " " -f 2-)"
break
fi
printf "\n\nInvalid input!\n"
done

eval "$goto_chapter"
15 changes: 15 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ expect "! git redeem-nuggit LocalCodeExecution 2>&1" to contain "Unfortunately t
check_redeem_without_local_code_execution
'

extract_chapter_number() {
git skip-to-nuggit-chapter <<< q | sed -r "s/([0-9]+)\\)\t$1/\\1/gp;d"
}

it 'git skip-to-nuggit-chapter should work' <<EOF
expect 'git skip-to-nuggit-chapter <<< "\$(extract_chapter_number branches)" 2>&1' to succeed
expect '[ -f branch.md ]' to succeed
expect 'git skip-to-nuggit-chapter <<< "\$(extract_chapter_number "push\\/pull")" 2>&1' to succeed
expect "git push 2>&1" to succeed
expect 'git skip-to-nuggit-chapter <<< "\$(extract_chapter_number log)" 2>&1' to succeed
expect '[ -f log.md ]' to succeed
expect 'git skip-to-nuggit-chapter <<< "\$(extract_chapter_number cherry-pick)" 2>&1' to succeed
expect '[ -f cherry-pick.md ]' to succeed
EOF

cd ..
build_challenge
echo "Running tests..."
Expand Down

0 comments on commit 9e49bac

Please sign in to comment.