Created
April 11, 2012 18:22
-
-
Save yaegashi/2361136 to your computer and use it in GitHub Desktop.
Git pre-commit hook script to verify file encodings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Git pre-commit hook script to verify file encodings. | |
# Works with Perl 5.8. The bundled perl in msysgit is also supported. | |
# Copyright (C) 2012 Takeshi Yaegashi | |
# Encodings allowed to be committed. | |
my @allowed = qw/ascii utf8/; | |
# Encodings possible to be seen - optional. | |
my @possible = qw/euc-jp shiftjis 7bit-jis/; | |
use strict; | |
use warnings; | |
use Encode; | |
use Encode::Guess; | |
Encode::Guess->set_suspects(@possible); | |
my $failed = 0; | |
open my $files, "-|", qw/git diff --cached --numstat --diff-filter=ACM/; | |
while (<$files>) { | |
chomp; | |
(my $add, my $del, my $file) = split(/\s+/, $_, 3); | |
next if $add eq "-"; # Skip binary files. | |
my $fh; | |
unless (open $fh, "<", $file) { | |
print "$file: $!\n"; | |
$failed++; | |
next; | |
} | |
binmode $fh; | |
read $fh, my $data, (-s $file); | |
close $fh; | |
next unless length($data) > 0; | |
my $decoder = Encode::Guess->guess($data); | |
my $encoding = ref($decoder) ? $decoder->name : $decoder; | |
unless (grep { $_ eq $encoding } @allowed) { | |
print "$file: $encoding\n"; | |
$failed++; | |
} | |
} | |
close $files; | |
if ($failed > 0) { | |
print "Commit aborted! (allowed encodings: @allowed)\n"; | |
exit 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now replaced by https://github.com/yaegashi/git-companion-scripts/blob/master/hooks/pre-commit-encoding