import os
import subprocess
import sys
"""
This is a git commit-hook which can be used to check if huge files
where accidentally added to the staging area and are about to be
committed.
If there is a file which is bigger then the given "max_file_size"-
variable, the script will exit non-zero and abort the commit.
This script is meant to be added as a "pre-commit"-hook. See this
page for further information:
http://progit.org/book/ch7-3.html#installing_a_hook
In order to make the script work probably, you'll need to set the
above path to the python interpreter (first line of the file)
according to your system (under *NIX do "which python" to find out).
Also, the "git_binary_path"-variable should contain the absolute
path to your "git"-executable (you can use "which" here, too).
See the included README-file for further information.
The script was developed and has been confirmed to work under
python 3.2.2 and git 1.7.7.1 (might also work with earlier versions!)
"""
max_file_size = 64 * 1024
git_binary_path = "/usr/bin/git"
def sizeof_fmt(num):
for x in ["bytes", "KB", "MB", "GB", "TB"]:
if num < 1024.0:
return "%3.1f %s" % (num, x)
num /= 1024.0
try:
print("Checking for files bigger then " + sizeof_fmt(max_file_size * 1024))
text = subprocess.check_output(
[git_binary_path, "status", "--porcelain", "-uno"], stderr=subprocess.STDOUT
).decode("utf-8")
file_list = text.splitlines()
for file_s in file_list:
if os.path.isfile(file_s[3:]):
stat = os.stat(file_s[3:])
if stat.st_size > (max_file_size * 1024):
print(
"'" + file_s[3:] + "' is too huge to be commited!",
"(" + sizeof_fmt(stat.st_size) + ")",
)
sys.exit(1)
print("No huge files found.")
sys.exit(0)
except Exception:
e = sys.exc_info()[1]
print(e.args)
print("Oops...")
sys.exit(12)