Advertisement
Guest User

Untitled

a guest
Jul 7th, 2013
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. How to use Git Hooks to save some time
  2.  
  3. Who am I?
  4.  
  5. Marian HackMan Marinov
  6.  
  7. @OpenFest
  8. @турнето
  9. Chairman of Open Projects Foundation
  10. CEO & Co-founder of 1H Ltd.
  11. Part time lecturer at FMI and Telerik
  12.  
  13.  
  14. What is git?
  15.  
  16. [ my repo ] <-----> [ central ]
  17. | \
  18. | \ <----> [ stefan ]
  19. [ github ] <---> /
  20.  
  21. What is a git hook?
  22.  
  23. A simple script
  24.  
  25. And what types of hooks do we have?
  26. pre-commit
  27. prepare-commit-msg
  28. commit-msg [--no-verify]
  29. post-commit
  30. post-checkout
  31. post-merge
  32. pre-receive
  33. post-receive
  34.  
  35.  
  36. My pre-commit
  37. #!/bin/bash
  38.  
  39. change=($(git diff --name-status --cached))
  40. st=${change[0]}
  41. name=${change[1]}
  42.  
  43. function err() {
  44. echo -n "\nThere is an issue with the committed"
  45. echo -e "file: \e[31m$1\e[0m\n"
  46. echo "Please fix it before trying to commit again."
  47. exit 1
  48. }
  49.  
  50. ######
  51. # Handle only files that are changed or newly added
  52. ######
  53. if [ "$st" != 'M' ] && [ "$st" != 'A' ]; then
  54. exit 0
  55. fi
  56.  
  57. ######
  58. # Bash syntax checks
  59. ######
  60. if [[ "$name" =~ \.sh$ ]] && ( ! bash -n $name ); then
  61. err $name
  62. fi
  63.  
  64. ######
  65. # Perl handling
  66. ######
  67. if [[ "$name" =~ \.pl$ ]]; then
  68. # Syntax checks
  69. if ( ! perl -c $name ); then
  70. err $name
  71. fi
  72. # Indentify the code
  73. # -syn - syntax checks
  74. # -dws - remove trailing white spaces
  75. # -et=4 - replace every 4 spaces with one tab
  76. # -bbs - blank lines before subs and packages
  77. # -ce - cuddled else; } else {
  78. if ( ! perltidy -syn -dws -et=4 -bbs -ce $name ); then
  79. err $name
  80. fi
  81. if [ -f ${name}.tdy ] && ( ! diff $name ${name}.tdy > /dev/null ); then
  82. /bin/mv -f ${name}.tdy $name
  83. fi
  84. # Produce some major critics about the code
  85. if ( ! perlcritic -2 $name ); then
  86. err $name
  87. fi
  88. fi
  89.  
  90. ######
  91. # PHP handling
  92. ######
  93. if [[ "$name" =~ \.pl$ ]]; then
  94. # Syntax checks
  95. if ( ! php -l $name ); then
  96. err $name
  97. fi
  98. fi
  99.  
  100. exit 0
  101.  
  102. What else can I do with hooks:
  103. * prepare-commit-msg / commit-msg
  104. - Auto format your commit messages
  105. - Add additional information into the commit,
  106. like signatures
  107. - Add third party CC for the patch(to be used
  108. by post-receive-email)
  109.  
  110. * post-commit
  111. - auto generate patches or patch sets
  112. - auto push to default and non default remotes
  113.  
  114. * post-checkout
  115. - fast deployment
  116. deploy everything in a separate branch and
  117. switch to it later
  118. - reconfigure your local copy
  119.  
  120. * pre-receive
  121. - check the incoming data
  122. verify that there is no malicious code
  123. syntax checks
  124.  
  125. * post-receive
  126. - Run code tests
  127. have a separate directory and make a pull in that
  128. dir and start test
  129. - Upload your project to the live environment
  130. pull the uploaded data directly into the live
  131. environment
  132. - Very fast deploy to live
  133. pull the new changes in a separate branch and
  134. switch to it
  135. - Update changes to your database
  136. - Use post-receive-email to send e-mail notifications
  137. - Auto push to other remotes
  138.  
  139. /usr/share/git-core/contrib/hooks/post-receive-email
  140. [hooks]
  141. mailinglist = mm@1h.com
  142. emailprefix = [GIT]
  143. project/description
  144.  
  145. How can you make use of the hooks for different languages:
  146. * Do syntax checks
  147. - bash -n
  148. - perl -c
  149. - ruby -c
  150. - php -l
  151. - python -m py_compile
  152.  
  153. * Do Indentation
  154. - beautify_bash
  155. - Perl::Tidy
  156. - PHP Tidy
  157. - PythonTidy, untabify.py, reindent.py
  158.  
  159. * Do sanity checks
  160. - Bash - checkbashisms
  161. - Perl::Critic
  162. - Python - pep8, pyflakes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement