Skip to content

Instantly share code, notes, and snippets.

@iley
Created October 24, 2011 19:09
Show Gist options
  • Save iley/1309859 to your computer and use it in GitHub Desktop.
Save iley/1309859 to your computer and use it in GitHub Desktop.
pre-receive hook to deny pushing big files
#!/usr/bin/perl -wl
use strict;
my $limit = 1024 * 1024;
my $hasBadFiles;
while (<>) {
chomp;
my ($old, $new, $ref) = split / /, $_;
my $log = ($old =~ /^0+$/ ? `git log --pretty=%H` : `git log --pretty=%H $old..$new`);
for my $commit (split /\n/, $log) {
for my $blob (split /\n/, `git ls-tree -l -r $commit`) {
my (undef, $type, undef, $size, $name) = split /\s+/, $blob;
next if $type ne 'blob';
if ($size > $limit) {
print "Size of file '$name' in commit $commit is over limit ($limit bytes)";
$hasBadFiles = 1;
}
}
}
}
$hasBadFiles and exit 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment