-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit.pl
152 lines (120 loc) · 2.7 KB
/
pre-commit.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/perl
# Should be put into <repository>/
# TODO: masks to ignore
# TODO: ignore files that were indexed but deleted
# TODO: learn to /bin/bash correctly and put it back, lol
EL();
my @diffFiles = GetFiles();
my $filecount = @diffFiles;
if ( $filecount == 0 )
{
print "* Nothing to check\n";
EL();
exit 0;
}
print "* $filecount files to check\n";
ES();
my @unicodedFiles = ();
my $unicodedCount = 0;
my $i = 0;
foreach $file (@diffFiles)
{
$i += 1;
print "[$i/$filecount] --- Checking: $file";
ConvertFile($file);
}
ES();
print "Unicode check finished: $i files checked\n";
if ( $i != $filecount )
{
print " ! ERROR: couldn't parse all $filecount files!\n";
EL();
exit 1;
}
if ( $unicodedCount == 0 )
{
print " * Everything OK\n";
EL();
exit 0;
}
print " * Of them $unicodedCount were encoded to UTF-8:\n";
for $f (@unicodedFiles)
{
print " - $f\n";
}
ES();
print " * Check these files and then 'git add' them again\n";
EL();
exit 1;
###############################################################################
# get the files to encode
sub GetFiles
{
# turn off escaping non-ascii characters so тест.файл won't turn to "\321\202\320\265\321\201\321\202.\321\204\320\260\320\271\320\273"
my $QP = `git config --get core.quotepath`;
`git config core.quotepath off`;
# get files to process
my @diffResult = `git diff --stat --cached --diff-filter=ACMR`;
`git config core.quotepath $QP`;
# skip last line "3 files changed, 1 insertion"
pop @diffResult;
my @diffFiles = ();
for $d (@diffResult)
{
# split each string by " | " in center
my @parts = split / \| /, $d;
# skip binary files
if (not BeginsWith(Trim($parts[1]), 'Bin'))
{
push @diffFiles, Trim($parts[0]);
}
}
return @diffFiles;
}
# encode the file if needed
sub ConvertFile
{
my ($f) = @_;
# since git diff returns one file per line ending with \n
$f =~ s/\n//;
my $utf = '~'.$f.'.utf8';
# if we can successfully convert from Utf8 to Utf8 - it's already encoded correctly, won't mess around
`iconv -s -f utf-8 -t utf-8 $f > /dev/null`;
if ( not $? )
{
print " * Conversion not needed\n";
return;
}
# otherwise, let's try to convert it
`touch $utf`;
`iconv -s -f cp1251 -t utf-8 $f > $utf`;
if ( not $? )
{
`rm $f`;
`mv $utf $f`;
$unicodedCount += 1;
push @unicodedFiles, $f;
print " + Converted\n";
return;
}
print " - Couldn't convert! Probably a binary file\n";
`rm $utf`;
}
sub EL
{
print "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
}
sub ES
{
print "- - - - - - - - - - - - - - - - - - - - - - - - - - -\n";
}
sub BeginsWith
{
return substr($_[0], 0, length($_[1])) eq $_[1];
}
sub Trim
{
my $s = shift;
$s =~ s/^\s+|\s+$//g;
return $s
};