git-svn: refuse to dcommit non-UTF-8 messages
...without i18n.commitencoding set in the config. SVN tries to store all commit messages in UTF-8, however it is up to the job of the clients to enforce this rule. SVN servers themselves do not always enforce this; allowing clients to commit malformed UTF-8 messages and break repositories. So git-svn will enforce this and tell the user to set i18n.commitencoding when a git commit is is not in UTF-8. Signed-off-by: Eric Wong <normalperson@yhbt.net>
Eric Wong committed
May 28, 2009 at 00:56 UTC
b510df8af2c86d441de87fa4be09f786b5411527
2 files changed
+61
-3
git-svn.perl
+14
-3
@@ -1178,16 +1178,27 @@ sub get_commit_entry {
1178
}
1179
rename $commit_editmsg, $commit_msg or croak $!;
1180
{
1181
+ require Encode;
1182
# SVN requires messages to be UTF-8 when entering the repo
1183
local $/;
1184
open $log_fh, '<', $commit_msg or croak $!;
1185
binmode $log_fh;
1186
chomp($log_entry{log} = <$log_fh>);
1187
1187
- if (my $enc = Git::config('i18n.commitencoding')) {
1188
- require Encode;
1189
- Encode::from_to($log_entry{log}, $enc, 'UTF-8');
1188
+ my $enc = Git::config('i18n.commitencoding') || 'UTF-8';
1189
+ my $msg = $log_entry{log};
1190
+
1191
+ eval { $msg = Encode::decode($enc, $msg, 1) };
1192
+ if ($@) {
1193
+ die "Could not decode as $enc:\n", $msg,
1194
+ "\nPerhaps you need to set i18n.commitencoding\n";
1195
}
1196
+
1197
+ eval { $msg = Encode::encode('UTF-8', $msg, 1) };
1198
+ die "Could not encode as UTF-8:\n$msg\n" if $@;
1199
+
1200
+ $log_entry{log} = $msg;
1201
+
1202
close $log_fh or croak $!;
1203
}
1204
unlink $commit_msg;
t/t9139-git-svn-non-utf8-commitencoding.sh
new
+47
@@ -0,0 +1,47 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2009 Eric Wong
4
+
5
+test_description='git svn refuses to dcommit non-UTF8 messages'
6
+
7
+. ./lib-git-svn.sh
8
+
9
+# ISO-2022-JP can pass for valid UTF-8, so skipping that in this test
10
+
11
+for H in ISO-8859-1 EUCJP
12
+do
13
+ test_expect_success "$H setup" '
14
+ mkdir $H &&
15
+ svn_cmd import -m "$H test" $H "$svnrepo"/$H &&
16
+ git svn clone "$svnrepo"/$H $H
17
+ '
18
+done
19
+
20
+for H in ISO-8859-1 EUCJP
21
+do
22
+ test_expect_success "$H commit on git side" '
23
+ (
24
+ cd $H &&
25
+ git config i18n.commitencoding $H &&
26
+ git checkout -b t refs/remotes/git-svn &&
27
+ echo $H >F &&
28
+ git add F &&
29
+ git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt &&
30
+ E=$(git cat-file commit HEAD | sed -ne "s/^encoding //p") &&
31
+ test "z$E" = "z$H"
32
+ )
33
+ '
34
+done
35
+
36
+for H in ISO-8859-1 EUCJP
37
+do
38
+ test_expect_success "$H dcommit to svn" '
39
+ (
40
+ cd $H &&
41
+ git config --unset i18n.commitencoding &&
42
+ ! git svn dcommit
43
+ )
44
+ '
45
+done
46
+
47
+test_done