Skip to content

Instantly share code, notes, and snippets.

@namit
Last active August 29, 2015 14:18
Show Gist options
  • Save namit/f74376e5c80d22b7aa0c to your computer and use it in GitHub Desktop.
Save namit/f74376e5c80d22b7aa0c to your computer and use it in GitHub Desktop.
Git pre-commit hook to deny commits in production branches and for files larger than a specified size
#!/usr/bin/env ruby
# (1) Prevent any commit to production branches
PRODUCTION_BRANCHES = ["production"]
branch = `git rev-parse --abbrev-ref=strict HEAD`.chomp
if (PRODUCTION_BRANCHES.include?(branch))
puts "\nDo you really want to commit on '#{branch}' branch?\nIf yes, add --no-verify to the commit command to force it."
exit 1
end
# (2) Prevent any commit of big files
MAX_FILE_SIZE = 10 * 1000 * 1024 # 10MB
files = `git status --porcelain`.split("\n")
files.each do |file|
realfile = file.split(/\s+/)[-1]
realfile_size = File.size(realfile)
if realfile_size > MAX_FILE_SIZE
puts "\nFile '#{realfile}' has size #{realfile_size / 1024}KB which is bigger than max size #{MAX_FILE_SIZE / 1024}KB.\nAdd --no-verify to the commit command if you want to force the commit."
exit 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment