#!/usr/bin/env bash set -euo pipefail scriptloc=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) replace() { keyword=$1 string=$2 python << END with open("${scriptloc}/slides.md") as slides: content = slides.read() string = """${string}""" with open("${scriptloc}/slides.md", "w") as slides: slides.write(content.replace(r"${keyword}", string)) END } mdprint() { cmd=$* echo '```bash' echo '$' ${cmd} eval ${cmd} echo '```' } tmpdir=$(mktemp -d) pushd ${tmpdir} # delete temporary directory on script exit trap "rm -fr ${tmpdir}" 0 # Initialise repository mdprint git init git config --local user.name "Achilleas Koutsou" git config --local user.email "ak@example.com" # No objects here yet mdprint tree -F .git/objects # Add a README and commit mdprint 'echo "# Understanding Git: Presentation slides repository" > README.md' mdprint git add README.md firstcommit=$(mdprint 'git commit -m "Initial commit: Add README"') replace "::firstcommit::" "${firstcommit}" # Check the object store again firstobjectstore=$(mdprint tree -F .git/objects) replace "::firstobjectstore::" "${firstobjectstore}" filepaths1=$(find .git/objects -type f) # git cat-file on each object for path in ${filepaths1}; do prefix=${path:13:2} rest=${path:16} full=${prefix}${rest} type=$(git cat-file -t ${full}) cattype=$(mdprint git cat-file -t ${full}) catprint=$(mdprint git cat-file -p ${full}) replace "::catt${type}1::" "${cattype}" replace "::catp${type}1::" "${catprint}" done # Let's make one more commit so we can see the parent mdprint 'echo -e "# Understanding Git\n\n> Achilleas Koutsou\n\n2018-11-14" > slides.md' mdprint git add slides.md mdprint 'git commit -m "Add first presentation slide"' # Check the object store again mdprint tree -F .git/objects secondobjectstore=$(mdprint find .git/objects -type f) replace "::secondobjectstore::" "${secondobjectstore}" filepaths2=$(find .git/objects -type f) # git cat-file on each *new* object for path in ${filepaths2}; do if echo ${filepaths1} | grep ${path} &> /dev/null; then continue fi prefix=${path:13:2} rest=${path:16} full=${prefix}${rest} mdprint git cat-file -t ${full} mdprint git cat-file -p ${full} type=$(git cat-file -t ${full}) cattype=$(mdprint git cat-file -t ${full}) catprint=$(mdprint git cat-file -p ${full}) replace "::catt${type}2::" "${cattype}" replace "::catp${type}2::" "${catprint}" if git cat-file -p ${full} | grep "Understanding Git" &> /dev/null; then slidesfirstver=${full} fi done echo "--------------------------" # Add a new slide mdprint 'echo -e "\n\n# Part 1\n## Porcelain and Plumbing" >> slides.md' mdprint git add slides.md mdprint 'git commit -m "Add second presentation slide"' # Check the object store again thirdobjectstore=$(mdprint find .git/objects -type f) replace "::thirdobjectstore::" "${thirdobjectstore}" filepaths3=$(find .git/objects -type f) # git cat-file on each *new* object for path in ${filepaths3}; do if echo ${filepaths2} | grep ${path} &> /dev/null; then continue fi prefix=${path:13:2} rest=${path:16} full=${prefix}${rest} mdprint git cat-file -t ${full} mdprint git cat-file -p ${full} type=$(git cat-file -t ${full}) cattype=$(mdprint git cat-file -t ${full}) catprint=$(mdprint git cat-file -p ${full}) replace "::catt${type}3::" "${cattype}" replace "::catp${type}3::" "${catprint}" done mdprint git cat-file -p ${slidesfirstver} mdprint git cat-file -t HEAD mdprint git cat-file -t master catpmaster=$(mdprint git cat-file -p master) replace "::catpmaster::" "${catpmaster}" mdprint git cat-file -t master~1 mdprint git cat-file -t master~1: mdprint git cat-file -p master~1:slides.md mdprint git cat-file -p master~1:README.md mdprint git cat-file -p master:README.md rpheada=$(mdprint git rev-parse HEAD) replace "::rpheada::" "${rpheada}" rpmastera=$(mdprint git rev-parse master) replace "::rpmastera::" "${rpmastera}" catpheada=$(mdprint git cat-file -p HEAD) replace "::catpheada::" "${catpheada}" rpheadtld1a=$(mdprint git rev-parse HEAD~1) replace "::rpheadtld1a::" "${rpheadtld1a}" catpmastertld1clna=$(mdprint git cat-file -p master~1:) replace "::catpmastertld1clna::" "${catpmastertld1clna}" catpmastertld1rdma=$(mdprint git rev-parse master~1:README.md) replace "::catpmastertld1rdma::" "${catpmastertld1rdma}" mdprint git branch demo-code rpmasterdemohead=$(mdprint git rev-parse master demo-code HEAD) replace "::rpmasterdemohead::" "${rpmasterdemohead}" mdprint git checkout demo-code mdprint 'echo "#!/usr/bin/env bash" > demoscript' mdprint 'chmod +x demoscript' mdprint 'git add demoscript' mdprint 'git commit -m "Add demoscript"' rpmasterb=$(mdprint git rev-parse master) replace "::rpmasterb::" "${rpmasterb}" rpdemob=$(mdprint git rev-parse demo-code) replace "::rpdemob::" "${rpdemob}" rpheadb=$(mdprint git rev-parse HEAD) replace "::rpheadb::" "${rpheadb}" mdprint git checkout master rpmasterc=$(mdprint git rev-parse master) replace "::rpmasterc::" "${rpmasterc}" rpdemoc=$(mdprint git rev-parse demo-code) replace "::rpdemoc::" "${rpdemoc}" rpheadc=$(mdprint git rev-parse HEAD) replace "::rpheadc::" "${rpheadc}" mdprint ls mdprint 'echo -e "\n\n# Part 2\n## What is Git?" >> slides.md' mdprint git add slides.md mdprint 'git commit -m "Add third presentation slide"' rpmasterdemod=$(mdprint git rev-parse master~1 demo-code~1) replace "::rpmasterdemod::" "${rpmasterdemod}" gitloga=$(mdprint git log -n 2) replace "::gitloga::" "${gitloga}" gitlogb=$(mdprint 'git log --pretty=format:"Commit: %h | Parent: %p | Tree: %t %n Message: %s%n"') replace "::gitlogb::" "${gitlogb}" gitlogc=$(mdprint 'git log --graph --oneline master demo-code') replace "::gitlogc::" "${gitlogc}" git checkout demo-code echo "" >> demoscript git commit demoscript -m "Writing script" echo "" >> demoscript git commit demoscript -m "Finalising script" gitlogd=$(mdprint 'git log --graph --decorate --oneline master demo-code') replace "::gitlogd::" "${gitlogd}" export GIT_MERGE_AUTOEDIT=no mdprint git checkout master gitmergea=$(mdprint git merge demo-code) replace "::gitmergea::" "${gitmergea}" gitloge=$(mdprint 'git log --graph --decorate --oneline master demo-code') replace "::gitloge::" "${gitloge}" gitlogf=$(mdprint 'git log --oneline master') replace "::gitlogf::" "${gitlogf}" gitcatpheadtld1b=$(mdprint 'git cat-file -p HEAD~1') replace "::gitcatpheadtld1b::" "${gitcatpheadtld1b}" gitcatpheadtldcln1b=$(mdprint 'git cat-file -p HEAD~1:') replace "::gitcatpheadtld1clnb::" "${gitcatpheadtldcln1b}" gitcatpmerge=$(mdprint 'git cat-file -p HEAD') replace "::gitcatpmerge::" "${gitcatpmerge}" gitbranchf=$(mdprint git branch) replace "::gitbranchf::" "${gitbranchf}" gitlogall=$(mdprint git log --all --graph) replace "::gitlogall::" "${gitlogall}" appendreadme=$(mdprint 'echo "See slides.md for slides" >> README.md') replace "::appendreadme::" "${appendreadme}" gitstatusf=$(mdprint git status) replace "::gitstatusf::" "${gitstatusf}" gitdiff=$(mdprint git diff) replace "::gitdiff::" "${gitdiff}" gitdiffdemo=$(mdprint git diff demo-code) replace "::gitdiffdemo::" "${gitdiffdemo}" git commit README.md -m "Mention slides in README" gitcatsrdma=$(mdprint git cat-file -s HEAD:README.md) replace "::gitcatsrdma::" "${gitcatsrdma}" gitcatprdma=$(mdprint git cat-file -p HEAD:README.md) replace "::gitcatprdma::" "${gitcatprdma}" gitcatsrdmb=$(mdprint git cat-file -s HEAD~1:README.md) replace "::gitcatsrdmb::" "${gitcatsrdmb}" gitcatprdmb=$(mdprint git cat-file -p HEAD~1:README.md) replace "::gitcatprdmb::" "${gitcatprdmb}" ospregc=$(mdprint find .git/objects -type f) replace "::ospregc::" "${ospregc}" osdupre=$(mdprint du -s .git/objects) replace "::osdupre::" "${osdupre}" gitgc=$(mdprint git gc) replace "::gitgc::" "${gitgc}" ospostgc=$(mdprint find .git/objects -type f) replace "::ospostgc::" "${ospostgc}" osdupost=$(mdprint du -s .git/objects) replace "::osdupost::" "${osdupost}" apack=$(find .git/objects -type f -iname "*.pack" | head -n1) verifypack=$(mdprint git verify-pack -v ${apack}) replace "::verifypack::" "${verifypack}" exit # --------------- mdprint git log --graph master demo-code mdprint 'git log --graph --pretty=format:"Commit: %H%n Message: %s%n Parent: %P%n Tree: %T%n%n" master demo-code' git log --reverse --pretty=format:"Commit: %H%n Message: %s%n Parent: %P%n Tree: %T%n%n"