Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.