git-remote-mediawiki: escape ", \, and LF in file names

A mediawiki page can contain, and even start with a " character, we have to escape it when generating the fast-export stream, as well as \ character. While we're there, also escape newlines, but I don't think we can get them from MediaWiki pages. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Nov 29, 2012 at 18:00 UTC 462d97daf69951f968f16b6271de9db34f7dd13c
2 files changed +39 -3
contrib/mw-to-git/git-remote-mediawiki
+13 -3
@@ -711,6 +711,14 @@ sub fetch_mw_revisions {
711 return ($n, @revisions);
712 }
713
714 +sub fe_escape_path {
715 + my $path = shift;
716 + $path =~ s/\\/\\\\/g;
717 + $path =~ s/"/\\"/g;
718 + $path =~ s/\n/\\n/g;
719 + return '"' . $path . '"';
720 +}
721 +
722 sub import_file_revision {
723 my $commit = shift;
724 my %commit = %{$commit};
@@ -738,15 +746,17 @@ sub import_file_revision {
746 print STDOUT "from refs/mediawiki/$remotename/master^0\n";
747 }
748 if ($content ne DELETED_CONTENT) {
741 - print STDOUT "M 644 inline $title.mw\n";
749 + print STDOUT "M 644 inline " .
750 + fe_escape_path($title . ".mw") . "\n";
751 literal_data($content);
752 if (%mediafile) {
744 - print STDOUT "M 644 inline $mediafile{title}\n";
753 + print STDOUT "M 644 inline "
754 + . fe_escape_path($mediafile{title}) . "\n";
755 literal_data_raw($mediafile{content});
756 }
757 print STDOUT "\n\n";
758 } else {
749 - print STDOUT "D $title.mw\n";
759 + print STDOUT "D " . fe_escape_path($title . ".mw") . "\n";
760 }
761
762 # mediawiki revision number in the git note
contrib/mw-to-git/t/t9362-mw-to-git-utf8.sh
+26
@@ -318,4 +318,30 @@ test_expect_success 'git push with \ in format control' '
318 '
319
320
321 +test_expect_success 'fast-import meta-characters in page name (mw -> git)' '
322 + wiki_reset &&
323 + wiki_editpage \"file\"_\\_foo "expect to be called \"file\"_\\_foo" false &&
324 + git clone mediawiki::'"$WIKI_URL"' mw_dir_21 &&
325 + test_path_is_file mw_dir_21/\"file\"_\\_foo.mw &&
326 + wiki_getallpage ref_page_21 &&
327 + test_diff_directories mw_dir_21 ref_page_21
328 +'
329 +
330 +
331 +test_expect_success 'fast-import meta-characters in page name (git -> mw) ' '
332 + wiki_reset &&
333 + git clone mediawiki::'"$WIKI_URL"' mw_dir_22 &&
334 + (
335 + cd mw_dir_22 &&
336 + echo "this file is called \"file\"_\\_foo.mw" >\"file\"_\\_foo &&
337 + git add . &&
338 + git commit -am "file \"file\"_\\_foo" &&
339 + git pull &&
340 + git push
341 + ) &&
342 + wiki_getallpage ref_page_22 &&
343 + test_diff_directories mw_dir_22 ref_page_22
344 +'
345 +
346 +
347 test_done