Advertisement
Guest User

pre-commit

a guest
Oct 11th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. if [ `git rev-parse --abbrev-ref HEAD` == "master" ] || [ `git rev-parse --abbrev-ref HEAD` == "review" ] ; then
  4. echo "[pre-commit HOOK activated]"
  5. debug_rex='/console.assert\|console.clear\|console.count\|console.countReset\|console.debug\|console.dir\|console.dirxml\|console.error\|console.exception\|console.group\|console.groupCollapsed\|console.groupEnd\|console.info\|console.log\|console.profile\|console.profileEnd\|console.table\|console.time\|console.timeEnd\|console.timeLog\|console.timeStamp\|console.trace\|console.warn/'
  6. debug_print_rex=$debug_rex'p'
  7. debug_del_rex=$debug_rex' d'
  8. add_debug_patch='.add-debug.patch'
  9. del_debug_patch='.del-debug.patch'
  10. git_work_dir="$(git rev-parse --show-toplevel)"
  11. fake_file="$git_work_dir/.fake_file"
  12. cur_dir="$(pwd)"
  13. staged_files="$(git diff --name-only --cached | grep '.js$\|.vue$')"
  14. debug_lines="$(sed -n $debug_print_rex $staged_files)"
  15.  
  16. function add_fake_file {
  17. touch $fake_file
  18. git add $fake_file
  19. }
  20.  
  21. function clear_fake_file {
  22. rm $fake_file
  23. git rm -q $fake_file
  24. }
  25.  
  26. if [ ! "$debug_lines" ]; then
  27. exit 0
  28. fi
  29.  
  30. cd $git_work_dir
  31.  
  32. #echo "save unstaged to stash"
  33. git stash save --keep-index -q
  34.  
  35. #echo "clone staged change to stash"
  36. add_fake_file
  37. git stash save -q
  38. git stash apply -q
  39.  
  40. if [ ! "$staged_files" ]; then
  41. exit 0
  42. fi
  43.  
  44. #echo "show console.log"
  45. echo "[remove console.log]"
  46. echo "$debug_lines"
  47. #echo "delete console.log"
  48. sed -i "$debug_del_rex" $staged_files
  49.  
  50. #echo "generate debug patch file"
  51. git diff -R stash@{0} > $add_debug_patch
  52.  
  53. clear_fake_file
  54. git add -u # for deleted file
  55. git add $staged_files # for file new added
  56.  
  57. git stash drop -q
  58. cd - > /dev/null 2>&1
  59. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement