File: check-valid-utf8.pl

package info (click to toggle)
libsvn-hooks-perl 1.19-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 372 kB
  • sloc: perl: 1,443; makefile: 7
file content (18 lines) | stat: -rwxr-xr-x 630 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Check if every changed text file contains valid UTF-8 data.

PRE_COMMIT {
    my ($svnlook) = @_;
    foreach my $file ($svnlook->added(), $svnlook->updated()) {
	next if $file =~ m:/$:; # skip directories
	my $props = $svnlook->proplist($file);
	next unless exists $props->{'svn:mime-type'}; # skip files without a mime-type
	next unless $props->{'svn:mime-type'} =~ m:^text/:; # skip non-text files

	# Try to decode file contents as UTF-8 and dies if not
	require Encode;
	eval {Encode::decode_utf8($svnlook->cat($file), Encode::FB_CROAK)};
	die "New file '$file' does not contain valid UTF-8 data: $@\n" if $@;
    }
};

1;