demoscript 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. scriptloc=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
  4. replace() {
  5. keyword=$1
  6. string=$2
  7. python << END
  8. with open("${scriptloc}/slides.md") as slides:
  9. content = slides.read()
  10. string = """${string}"""
  11. with open("${scriptloc}/slides.md", "w") as slides:
  12. slides.write(content.replace(r"${keyword}", string))
  13. END
  14. }
  15. mdprint() {
  16. cmd=$*
  17. echo '```bash'
  18. echo '$' ${cmd}
  19. eval ${cmd}
  20. echo '```'
  21. }
  22. tmpdir=$(mktemp -d)
  23. pushd ${tmpdir}
  24. # delete temporary directory on script exit
  25. trap "rm -fr ${tmpdir}" 0
  26. # Initialise repository
  27. mdprint git init
  28. git config --local user.name "Achilleas Koutsou"
  29. git config --local user.email "ak@example.com"
  30. # No objects here yet
  31. mdprint tree -F .git/objects
  32. # Add a README and commit
  33. mdprint 'echo "# Understanding Git: Presentation slides repository" > README.md'
  34. mdprint git add README.md
  35. firstcommit=$(mdprint 'git commit -m "Initial commit: Add README"')
  36. replace "::firstcommit::" "${firstcommit}"
  37. # Check the object store again
  38. firstobjectstore=$(mdprint tree -F .git/objects)
  39. replace "::firstobjectstore::" "${firstobjectstore}"
  40. filepaths1=$(find .git/objects -type f)
  41. # git cat-file on each object
  42. for path in ${filepaths1}; do
  43. prefix=${path:13:2}
  44. rest=${path:16}
  45. full=${prefix}${rest}
  46. type=$(git cat-file -t ${full})
  47. cattype=$(mdprint git cat-file -t ${full})
  48. catprint=$(mdprint git cat-file -p ${full})
  49. replace "::catt${type}1::" "${cattype}"
  50. replace "::catp${type}1::" "${catprint}"
  51. done
  52. # Let's make one more commit so we can see the parent
  53. mdprint 'echo -e "# Understanding Git\n\n> Achilleas Koutsou\n\n2018-11-14" > slides.md'
  54. mdprint git add slides.md
  55. mdprint 'git commit -m "Add first presentation slide"'
  56. # Check the object store again
  57. mdprint tree -F .git/objects
  58. secondobjectstore=$(mdprint find .git/objects -type f)
  59. replace "::secondobjectstore::" "${secondobjectstore}"
  60. filepaths2=$(find .git/objects -type f)
  61. # git cat-file on each *new* object
  62. for path in ${filepaths2}; do
  63. if echo ${filepaths1} | grep ${path} &> /dev/null; then
  64. continue
  65. fi
  66. prefix=${path:13:2}
  67. rest=${path:16}
  68. full=${prefix}${rest}
  69. mdprint git cat-file -t ${full}
  70. mdprint git cat-file -p ${full}
  71. type=$(git cat-file -t ${full})
  72. cattype=$(mdprint git cat-file -t ${full})
  73. catprint=$(mdprint git cat-file -p ${full})
  74. replace "::catt${type}2::" "${cattype}"
  75. replace "::catp${type}2::" "${catprint}"
  76. if git cat-file -p ${full} | grep "Understanding Git" &> /dev/null; then
  77. slidesfirstver=${full}
  78. fi
  79. done
  80. echo "--------------------------"
  81. # Add a new slide
  82. mdprint 'echo -e "\n\n# Part 1\n## Porcelain and Plumbing" >> slides.md'
  83. mdprint git add slides.md
  84. mdprint 'git commit -m "Add second presentation slide"'
  85. # Check the object store again
  86. thirdobjectstore=$(mdprint find .git/objects -type f)
  87. replace "::thirdobjectstore::" "${thirdobjectstore}"
  88. filepaths3=$(find .git/objects -type f)
  89. # git cat-file on each *new* object
  90. for path in ${filepaths3}; do
  91. if echo ${filepaths2} | grep ${path} &> /dev/null; then
  92. continue
  93. fi
  94. prefix=${path:13:2}
  95. rest=${path:16}
  96. full=${prefix}${rest}
  97. mdprint git cat-file -t ${full}
  98. mdprint git cat-file -p ${full}
  99. type=$(git cat-file -t ${full})
  100. cattype=$(mdprint git cat-file -t ${full})
  101. catprint=$(mdprint git cat-file -p ${full})
  102. replace "::catt${type}3::" "${cattype}"
  103. replace "::catp${type}3::" "${catprint}"
  104. done
  105. mdprint git cat-file -p ${slidesfirstver}
  106. mdprint git cat-file -t HEAD
  107. mdprint git cat-file -t master
  108. catpmaster=$(mdprint git cat-file -p master)
  109. replace "::catpmaster::" "${catpmaster}"
  110. mdprint git cat-file -t master~1
  111. mdprint git cat-file -t master~1:
  112. mdprint git cat-file -p master~1:slides.md
  113. mdprint git cat-file -p master~1:README.md
  114. mdprint git cat-file -p master:README.md
  115. rpheada=$(mdprint git rev-parse HEAD)
  116. replace "::rpheada::" "${rpheada}"
  117. rpmastera=$(mdprint git rev-parse master)
  118. replace "::rpmastera::" "${rpmastera}"
  119. catpheada=$(mdprint git cat-file -p HEAD)
  120. replace "::catpheada::" "${catpheada}"
  121. rpheadtld1a=$(mdprint git rev-parse HEAD~1)
  122. replace "::rpheadtld1a::" "${rpheadtld1a}"
  123. catpmastertld1clna=$(mdprint git cat-file -p master~1:)
  124. replace "::catpmastertld1clna::" "${catpmastertld1clna}"
  125. catpmastertld1rdma=$(mdprint git rev-parse master~1:README.md)
  126. replace "::catpmastertld1rdma::" "${catpmastertld1rdma}"
  127. mdprint git branch demo-code
  128. rpmasterdemohead=$(mdprint git rev-parse master demo-code HEAD)
  129. replace "::rpmasterdemohead::" "${rpmasterdemohead}"
  130. mdprint git checkout demo-code
  131. mdprint 'echo "#!/usr/bin/env bash" > demoscript'
  132. mdprint 'chmod +x demoscript'
  133. mdprint 'git add demoscript'
  134. mdprint 'git commit -m "Add demoscript"'
  135. rpmasterb=$(mdprint git rev-parse master)
  136. replace "::rpmasterb::" "${rpmasterb}"
  137. rpdemob=$(mdprint git rev-parse demo-code)
  138. replace "::rpdemob::" "${rpdemob}"
  139. rpheadb=$(mdprint git rev-parse HEAD)
  140. replace "::rpheadb::" "${rpheadb}"
  141. mdprint git checkout master
  142. rpmasterc=$(mdprint git rev-parse master)
  143. replace "::rpmasterc::" "${rpmasterc}"
  144. rpdemoc=$(mdprint git rev-parse demo-code)
  145. replace "::rpdemoc::" "${rpdemoc}"
  146. rpheadc=$(mdprint git rev-parse HEAD)
  147. replace "::rpheadc::" "${rpheadc}"
  148. mdprint ls
  149. mdprint 'echo -e "\n\n# Part 2\n## What is Git?" >> slides.md'
  150. mdprint git add slides.md
  151. mdprint 'git commit -m "Add third presentation slide"'
  152. rpmasterdemod=$(mdprint git rev-parse master~1 demo-code~1)
  153. replace "::rpmasterdemod::" "${rpmasterdemod}"
  154. gitloga=$(mdprint git log -n 2)
  155. replace "::gitloga::" "${gitloga}"
  156. gitlogb=$(mdprint 'git log --pretty=format:"Commit: %h | Parent: %p | Tree: %t %n Message: %s%n"')
  157. replace "::gitlogb::" "${gitlogb}"
  158. gitlogc=$(mdprint 'git log --graph --oneline master demo-code')
  159. replace "::gitlogc::" "${gitlogc}"
  160. exit
  161. # ---------------
  162. mdprint git log --graph master demo-code
  163. mdprint 'git log --graph --pretty=format:"Commit: %H%n Message: %s%n Parent: %P%n Tree: %T%n%n" master demo-code'
  164. git log --reverse --pretty=format:"Commit: %H%n Message: %s%n Parent: %P%n Tree: %T%n%n"