From 9e49bac61d8113b078e39f60769ad5662c731c18 Mon Sep 17 00:00:00 2001 From: Michael Lohmann Date: Mon, 15 Jan 2024 15:22:18 +0100 Subject: [PATCH] add alias skip-to-nuggit-chapter --- build.sh | 4 ++++ lib.sh | 1 + src/01_init/README.md | 2 ++ src/skip_to_chapter.sh | 29 +++++++++++++++++++++++++++++ test.sh | 15 +++++++++++++++ 5 files changed, 51 insertions(+) create mode 100755 src/skip_to_chapter.sh diff --git a/build.sh b/build.sh index 78e1a94..ffb902d 100755 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/lib.sh b/lib.sh index d0300e8..5ca715f 100644 --- a/lib.sh +++ b/lib.sh @@ -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: diff --git a/src/01_init/README.md b/src/01_init/README.md index e9ca423..e0627f3 100644 --- a/src/01_init/README.md +++ b/src/01_init/README.md @@ -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. diff --git a/src/skip_to_chapter.sh b/src/skip_to_chapter.sh new file mode 100755 index 0000000..61637ba --- /dev/null +++ b/src/skip_to_chapter.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +# Format: +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" diff --git a/test.sh b/test.sh index e7b77cd..dd4bf18 100755 --- a/test.sh +++ b/test.sh @@ -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' <&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..."