maroph

Git pre-commit hook sample

Jan 23rd, 2024 (edited)
2,582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.72 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Based on a script shown in the following discussion
  4. # https://stackoverflow.com/questions/72588572/can-i-configure-github-to-block-large-files
  5. #
  6. max_file_size=10000000
  7. #
  8. # redirect output to stderr.
  9. # exec 1>&2
  10. #
  11. # enable user input
  12. exec < /dev/tty
  13. #
  14. for filename in $(git diff --cached --name-only --diff-filter=A HEAD); do
  15.     filesize=$(stat -c%s "$filename")
  16.     if  (( filesize > max_file_size ))
  17.     then
  18.         echo "The file "$filename" is more than $max_file_size bytes."
  19.         read -p "Commit anyway ? (y/[n]) : " answer
  20.         if [ "${answer}" = "y" ]
  21.         then
  22.             exit 0
  23.         else
  24.             echo "Git commit cancled."
  25.             exit -1
  26.         fi
  27.     fi
  28. done
  29.  
Advertisement
Add Comment
Please, Sign In to add comment