pre-commit 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bash
  2. YELLOW="\e[93m"
  3. GREEN="\e[32m"
  4. RED="\e[31m"
  5. ENDCOLOR="\e[0m"
  6. printMessage() {
  7. printf "${YELLOW}AppFlowy : $1${ENDCOLOR}\n"
  8. }
  9. printSuccess() {
  10. printf "${GREEN}AppFlowy : $1${ENDCOLOR}\n"
  11. }
  12. printError() {
  13. printf "${RED}AppFlowy : $1${ENDCOLOR}\n"
  14. }
  15. printMessage "Running local AppFlowy pre-commit hook."
  16. #flutter format .
  17. ##https://gist.github.com/benmccallum/28e4f216d9d72f5965133e6c43aaff6e
  18. limit=$(( 1 * 2**20 )) # 1MB
  19. function file_too_large(){
  20. filename=$0
  21. filesize=$(( $1 / 2**20 ))
  22. cat <<HEREDOC
  23. File $filename is $filesize MB, which is larger than github's maximum
  24. file size (1 MB). We will not be able to push this file to GitHub.
  25. Commit aborted
  26. HEREDOC
  27. }
  28. empty_tree=$( git hash-object -t tree /dev/null )
  29. if git rev-parse --verify HEAD > /dev/null 2>&1
  30. then
  31. against=HEAD
  32. else
  33. against=empty_tree
  34. fi
  35. for file in $( git diff-index --cached --name-only $against ); do
  36. file_size=$( ls -la $file | awk '{ print $5 }')
  37. if [ "$file_size" -gt "$limit" ]; then
  38. file_too_large $filename $file_size
  39. exit 1;
  40. fi
  41. done