Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Git pre-commit hook script to verify file encodings.
#!/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;
my $failed = 0;
open my $files, "-|", qw/git diff --cached --numstat --diff-filter=ACM/;
while (<$files>) {
(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";
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";
close $files;
if ($failed > 0) {
print "Commit aborted! (allowed encodings: @allowed)\n";
exit 1;

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment