Commit 2ac3bd54 authored by Matt Belhorn's avatar Matt Belhorn
Browse files

Adds terminal demo of staging operations.

parent fc3e0ab6
#!/usr/bin/env bash
###############################################################################
#
# demo-magic.sh
#
# Copyright (c) 2015 Paxton Hare
#
# This script lets you script demos in bash. It runs through your demo script when you press
# a key. It simulates typing and runs commands.
#
###############################################################################
# the speed to "type" the text
TYPE_SPEED=20
# handy color vars for pretty prompts
BLACK="\033[0;30m"
BLUE="\033[0;34m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
RED="\033[0;31m"
PURPLE="\033[0;35m"
BROWN="\033[0;33m"
WHITE="\033[1;37m"
COLOR_RESET="\033[0m"
##
# prints the script usage
##
function usage() {
echo -e ""
echo -e "Usage: $0 [options]"
echo -e ""
echo -e "\tWhere options is one or more of:"
echo -e "\t-h\tPrints Help text"
echo -e "\t-d\tDebug mode. Disables simulated typing"
echo -e ""
}
##
# wait for user to press any key
##
function wait() {
read -rsn 1;
}
##
# print command only. Useful for when you want to pretend to run a command
#
# takes 1 param - the string command to print
#
# usage: p "ls -l"
#
##
function p() {
cmd=$1
# render the prompt
x=$(PS1="$DEMO_PROMPT" "$BASH" --norc -i </dev/null 2>&1 | sed -n '${s/^\(.*\)exit$/\1/p;}')
printf "$x"
# wait for the user to press a key before typing the command
wait
if [[ -z $TYPE_SPEED ]]; then
echo -en "\033[0m$cmd"
else
echo -en "\033[0m$cmd" | pv -qL $[$TYPE_SPEED+(-2 + RANDOM%5)];
fi
# wait for the user to press a key before moving on
wait
echo ""
}
##
# Prints and executes a command
#
# takes 1 parameter - the string command to run
#
# usage: pe "ls -l"
#
##
function pe() {
# print the command
p "$@"
# execute the command
eval "$@"
}
function check_pv() {
command -v pv >/dev/null 2>&1 || {
echo ""
echo -e "${RED}##############################################################"
echo "# HOLD IT!! I require pv but it's not installed. Aborting." >&2;
echo -e "${RED}##############################################################"
echo ""
echo -e "${COLOR_RESET}Installing pv:"
echo ""
echo -e "${BLUE}Mac:${COLOR_RESET} $ brew install pv"
echo ""
echo -e "${BLUE}Other:${COLOR_RESET} http://www.ivarch.com/programs/pv.shtml"
echo -e "${COLOR_RESET}"
exit 1;
}
}
check_pv
#
# handle some default params
# -h for help
# -d for disabling simulated typing
#
while getopts ":dh" opt; do
case $opt in
h)
usage
exit 1
;;
d)
unset TYPE_SPEED
;;
esac
done
#!/bin/bash
# Setup demo repo
# ===============
. ./demo-magic.sh
DEMO_PROMPT="\n$ "
DEMO_REPO=/tmp/demp
TYPE_SPEED=40
rm -fr $DEMO_REPO
mkdir -p $DEMO_REPO
cd $DEMO_REPO
# Pay no attention to the man behind the curtain
clear
pe "git init ."
# Introduce the index
# ===================
p "# Introduction to the index."
pe "git rev-parse --show-toplevel"
pe "ls -l .git"
pe "git status"
pe "echo 'Hello, Index' > hello.txt"
pe "git status"
pe "file .git/index"
pe "git add hello.txt"
pe "file .git/index"
pe "git status"
pe "git commit -m 'Initial Commit'"
pe "git log -15 --pretty=format:'%h | %s [%cn]%d' --decorate"
pe "file .git/index"
# Additions at commit-time
# ========================
p "# Additions at commit-time."
pe "echo 'We meet again.' >> hello.txt"
pe "echo '#!/usr/bin/env python3' > patched.py"
pe "git status"
pe "git commit -am 'Only the previously tracked file is added!'"
pe "git status"
pe "file .git/index"
pe "git add patched.py"
pe "git commit -m 'Adds initial code for patched addition demo."
pe "git status"
pe "file .git/index"
# Moving, renaming, deleting, untracking
# ======================================
## Moving files in tree, but not the index
p "# Moving renaming, deleting, and untracking files."
pe "mv 'hello.txt' 'hello_index.txt'"
pe "ls -1"
pe "git status"
pe "git add 'hello_index.txt'"
pe "git status"
pe "git rm 'hello.txt'"
pe "git status"
pe "git commit -m 'Renamed hello.txt in index manually'"
## Reset the situation for another go.
pe "cp 'hello_index.txt' 'hello.txt'"
pe "ls -1"
pe "git status"
pe "git add hello.txt"
pe "git commit -m 'Reintroduces hello.txt'"
## Hit two birds with one mv
pe "git mv hello.txt hello_copy.txt"
pe "ls -1"
pe "git status"
pe "git commit -m 'Renamed hello.txt in index automatically.'"
## Removing files from tree and index
pe "git rm hello_index.txt"
pe "ls -1"
pe "git status"
## Don't really do it...
pe "git reset HEAD hello_index.txt"
pe "ls -1"
pe "git status"
pe "git checkout -- hello_index.txt"
pe "ls -1"
pe "git status"
## OK, now really do it...
pe "git rm hello_index.txt"
pe "git commit -m 'Removed hello_index.txt from the index and the tree.'"
pe "ls -1"
pe "git ls-files"
## Now undo it.
p "# remember, we can always checkout the file again from a previous commit."
pe "git checkout 'HEAD~1' -- hello_index.txt"
pe "ls -1"
pe "git status"
pe "git commit -m 'Resurrects hello_index.txt'"
pe "ls -1"
pe "git ls-files"
## Remove files from just the index.
pe "git rm --cached hello_index.txt"
pe "ls -l"
pe "git status"
pe "git commit -m 'Untracks hello_index.txt'"
pe "git ls-files"
# Ignoring files
# ==============
p "# Ignoring files."
pe "mkdir -p nested/dir"
p "# Lets fill that tree with some content."
echo 'ew' > nested/garbage.log
echo 'no thanks' > nested/refuse.log
echo 'Only for Finn and Jake' > nested/prismo_pickle_recipe.md
echo 'not track worthy' > nested/dir/junk.log
echo 'whoa, do not commit this!' > nested/dir/secret.key
pe "git status"
pe "git ls-files --others --exclude-standard"
pe "echo '*.log' > .gitignore"
pe "git add .gitignore"
pe "git status"
pe "git ls-files --others --exclude-standard"
pe "git add nested"
pe "git status"
pe "echo '*.key' >> .gitignore"
pe "git add .gitignore"
pe "git status"
pe "git rm --cached nested/dir/secret.key"
pe "git status"
pe "git commit -m 'Ignores logs and keys.'"
# Patched Additions
# =================
p "# Patched additions."
cat << EOF > patched.py
#!/usr/env python3
def main():
pass
if __name__ == '__main__':
main()
EOF
pe "vimcat patched.py"
pe "git add patched.py"
pe "git commit -m 'Adds a simply python script to demo patched additions.'"
p "# Let's update that file..."
cat << EOF > patched.py
#!/usr/env python3
'''
This is a demo of logically separate changes
made all at once, but committed separately.
'''
def square(number):
'''Returns square of number'''
return number**2
def half(number):
'''Returns integer half of number'''
return number//2
def main():
'''What runs when the script is called.'''
output = square(5)
output += half(34)
print(output)
if __name__ == '__main__':
main()
EOF
pe "vimcat patched.py"
p "# Note to self: use '?,s,e,e'"
pe "git add --patch"
pe "git diff --cached"
pe "git status"
pe "git commit -m 'Adds docstrings and a square function.'"
pe "git status"
pe "git diff"
pe "git commit -am 'Adds half function to get the ultimate answer.'"
pe "git log -15 --pretty=format:'%h | %s [%cn]%d' --decorate"
p ""
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment