Skip to content

Commit 5a25615

Browse files
committed
Merge branch 'ab/mediawiki-fixes'
Modernization and fixes to MediaWiki remote backend. * ab/mediawiki-fixes: remote-mediawiki: use "sh" to eliminate unquoted commands remote-mediawiki: annotate unquoted uses of run_git() remote-mediawiki: convert to quoted run_git() invocation remote-mediawiki: provide a list form of run_git() remote-mediawiki tests: annotate failing tests remote-mediawiki: fix duplicate revisions being imported remote-mediawiki tests: use CLI installer remote-mediawiki tests: use inline PerlIO for readability remote-mediawiki tests: replace deprecated Perl construct remote-mediawiki tests: use a more idiomatic dispatch table remote-mediawiki tests: use "$dir/" instead of "$dir." remote-mediawiki tests: change `[]` to `test` remote-mediawiki tests: use test_cmp in tests remote-mediawiki tests: use a 10 character password remote-mediawiki tests: use the login/password variables remote-mediawiki doc: don't hardcode Debian PHP versions remote-mediawiki doc: link to MediaWiki's current version remote-mediawiki doc: correct link to GitHub project
2 parents 306ee63 + 9a86064 commit 5a25615

13 files changed

+169
-401
lines changed

contrib/mw-to-git/git-mw.perl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# License: GPL v2 or later
77

88
# Set of tools for git repo with a mediawiki remote.
9-
# Documentation & bugtracker: https://github.com/moy/Git-Mediawiki/
9+
# Documentation & bugtracker: https://github.com/Git-Mediawiki/Git-Mediawiki
1010

1111
use strict;
1212
use warnings;

contrib/mw-to-git/git-remote-mediawiki.perl

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# License: GPL v2 or later
1010

1111
# Gateway between Git and MediaWiki.
12-
# Documentation & bugtracker: https://github.com/moy/Git-Mediawiki/
12+
# Documentation & bugtracker: https://github.com/Git-Mediawiki/Git-Mediawiki
1313

1414
use strict;
1515
use MediaWiki::API;
@@ -56,38 +56,38 @@
5656

5757
# Accept both space-separated and multiple keys in config file.
5858
# Spaces should be written as _ anyway because we'll use chomp.
59-
my @tracked_pages = split(/[ \n]/, run_git("config --get-all remote.${remotename}.pages"));
59+
my @tracked_pages = split(/[ \n]/, run_git_quoted(["config", "--get-all", "remote.${remotename}.pages"]));
6060
chomp(@tracked_pages);
6161

6262
# Just like @tracked_pages, but for MediaWiki categories.
63-
my @tracked_categories = split(/[ \n]/, run_git("config --get-all remote.${remotename}.categories"));
63+
my @tracked_categories = split(/[ \n]/, run_git_quoted(["config", "--get-all", "remote.${remotename}.categories"]));
6464
chomp(@tracked_categories);
6565

6666
# Just like @tracked_categories, but for MediaWiki namespaces.
67-
my @tracked_namespaces = split(/[ \n]/, run_git("config --get-all remote.${remotename}.namespaces"));
67+
my @tracked_namespaces = split(/[ \n]/, run_git_quoted(["config", "--get-all", "remote.${remotename}.namespaces"]));
6868
for (@tracked_namespaces) { s/_/ /g; }
6969
chomp(@tracked_namespaces);
7070

7171
# Import media files on pull
72-
my $import_media = run_git("config --get --bool remote.${remotename}.mediaimport");
72+
my $import_media = run_git_quoted(["config", "--get", "--bool", "remote.${remotename}.mediaimport"]);
7373
chomp($import_media);
7474
$import_media = ($import_media eq 'true');
7575

7676
# Export media files on push
77-
my $export_media = run_git("config --get --bool remote.${remotename}.mediaexport");
77+
my $export_media = run_git_quoted(["config", "--get", "--bool", "remote.${remotename}.mediaexport"]);
7878
chomp($export_media);
7979
$export_media = !($export_media eq 'false');
8080

81-
my $wiki_login = run_git("config --get remote.${remotename}.mwLogin");
81+
my $wiki_login = run_git_quoted(["config", "--get", "remote.${remotename}.mwLogin"]);
8282
# Note: mwPassword is discouraged. Use the credential system instead.
83-
my $wiki_passwd = run_git("config --get remote.${remotename}.mwPassword");
84-
my $wiki_domain = run_git("config --get remote.${remotename}.mwDomain");
83+
my $wiki_passwd = run_git_quoted(["config", "--get", "remote.${remotename}.mwPassword"]);
84+
my $wiki_domain = run_git_quoted(["config", "--get", "remote.${remotename}.mwDomain"]);
8585
chomp($wiki_login);
8686
chomp($wiki_passwd);
8787
chomp($wiki_domain);
8888

8989
# Import only last revisions (both for clone and fetch)
90-
my $shallow_import = run_git("config --get --bool remote.${remotename}.shallow");
90+
my $shallow_import = run_git_quoted(["config", "--get", "--bool", "remote.${remotename}.shallow"]);
9191
chomp($shallow_import);
9292
$shallow_import = ($shallow_import eq 'true');
9393

@@ -97,9 +97,9 @@
9797
# Possible values:
9898
# - by_rev: perform one query per new revision on the remote wiki
9999
# - by_page: query each tracked page for new revision
100-
my $fetch_strategy = run_git("config --get remote.${remotename}.fetchStrategy");
100+
my $fetch_strategy = run_git_quoted(["config", "--get", "remote.${remotename}.fetchStrategy"]);
101101
if (!$fetch_strategy) {
102-
$fetch_strategy = run_git('config --get mediawiki.fetchStrategy');
102+
$fetch_strategy = run_git_quoted(["config", "--get", "mediawiki.fetchStrategy"]);
103103
}
104104
chomp($fetch_strategy);
105105
if (!$fetch_strategy) {
@@ -123,9 +123,9 @@
123123
# will get the history with information lost). If the import is
124124
# deterministic, this means everybody gets the same sha1 for each
125125
# MediaWiki revision.
126-
my $dumb_push = run_git("config --get --bool remote.${remotename}.dumbPush");
126+
my $dumb_push = run_git_quoted(["config", "--get", "--bool", "remote.${remotename}.dumbPush"]);
127127
if (!$dumb_push) {
128-
$dumb_push = run_git('config --get --bool mediawiki.dumbPush');
128+
$dumb_push = run_git_quoted(["config", "--get", "--bool", "mediawiki.dumbPush"]);
129129
}
130130
chomp($dumb_push);
131131
$dumb_push = ($dumb_push eq 'true');
@@ -369,12 +369,14 @@ sub get_mw_pages {
369369
return %pages;
370370
}
371371

372-
# usage: $out = run_git("command args");
373-
# $out = run_git("command args", "raw"); # don't interpret output as UTF-8.
374-
sub run_git {
372+
# usage: $out = run_git_quoted(["command", "args", ...]);
373+
# $out = run_git_quoted(["command", "args", ...], "raw"); # don't interpret output as UTF-8.
374+
# $out = run_git_quoted_nostderr(["command", "args", ...]); # discard stderr
375+
# $out = run_git_quoted_nostderr(["command", "args", ...], "raw"); # ditto but raw instead of UTF-8 as above
376+
sub _run_git {
375377
my $args = shift;
376378
my $encoding = (shift || 'encoding(UTF-8)');
377-
open(my $git, "-|:${encoding}", "git ${args}")
379+
open(my $git, "-|:${encoding}", @$args)
378380
or die "Unable to fork: $!\n";
379381
my $res = do {
380382
local $/ = undef;
@@ -385,6 +387,13 @@ sub run_git {
385387
return $res;
386388
}
387389

390+
sub run_git_quoted {
391+
_run_git(["git", @{$_[0]}], $_[1]);
392+
}
393+
394+
sub run_git_quoted_nostderr {
395+
_run_git(['sh', '-c', 'git "$@" 2>/dev/null', '--', @{$_[0]}], $_[1]);
396+
}
388397

389398
sub get_all_mediafiles {
390399
my $pages = shift;
@@ -511,8 +520,9 @@ sub download_mw_mediafile {
511520
}
512521

513522
sub get_last_local_revision {
514-
# Get note regarding last mediawiki revision
515-
my $note = run_git("notes --ref=${remotename}/mediawiki show refs/mediawiki/${remotename}/master 2>/dev/null");
523+
# Get note regarding last mediawiki revision.
524+
my $note = run_git_quoted_nostderr(["notes", "--ref=${remotename}/mediawiki",
525+
"show", "refs/mediawiki/${remotename}/master"]);
516526
my @note_info = split(/ /, $note);
517527

518528
my $lastrevision_number;
@@ -807,7 +817,10 @@ sub get_more_refs {
807817
sub mw_import {
808818
# multiple import commands can follow each other.
809819
my @refs = (shift, get_more_refs('import'));
820+
my $processedRefs;
810821
foreach my $ref (@refs) {
822+
next if $processedRefs->{$ref}; # skip duplicates: "import refs/heads/master" being issued twice; TODO: why?
823+
$processedRefs->{$ref} = 1;
811824
mw_import_ref($ref);
812825
}
813826
print {*STDOUT} "done\n";
@@ -970,7 +983,7 @@ sub mw_import_revids {
970983
}
971984

972985
sub error_non_fast_forward {
973-
my $advice = run_git('config --bool advice.pushNonFastForward');
986+
my $advice = run_git_quoted(["config", "--bool", "advice.pushNonFastForward"]);
974987
chomp($advice);
975988
if ($advice ne 'false') {
976989
# Native git-push would show this after the summary.
@@ -1014,7 +1027,7 @@ sub mw_upload_file {
10141027
}
10151028
} else {
10161029
# Don't let perl try to interpret file content as UTF-8 => use "raw"
1017-
my $content = run_git("cat-file blob ${new_sha1}", 'raw');
1030+
my $content = run_git_quoted(["cat-file", "blob", $new_sha1], 'raw');
10181031
if ($content ne EMPTY) {
10191032
$mediawiki = connect_maybe($mediawiki, $remotename, $url);
10201033
$mediawiki->{config}->{upload_url} =
@@ -1084,7 +1097,7 @@ sub mw_push_file {
10841097
# with this content instead:
10851098
$file_content = DELETED_CONTENT;
10861099
} else {
1087-
$file_content = run_git("cat-file blob ${new_sha1}");
1100+
$file_content = run_git_quoted(["cat-file", "blob", $new_sha1]);
10881101
}
10891102

10901103
$mediawiki = connect_maybe($mediawiki, $remotename, $url);
@@ -1174,10 +1187,10 @@ sub mw_push_revision {
11741187
my $mw_revision = $last_remote_revid;
11751188

11761189
# Get sha1 of commit pointed by local HEAD
1177-
my $HEAD_sha1 = run_git("rev-parse ${local} 2>/dev/null");
1190+
my $HEAD_sha1 = run_git_quoted_nostderr(["rev-parse", $local]);
11781191
chomp($HEAD_sha1);
11791192
# Get sha1 of commit pointed by remotes/$remotename/master
1180-
my $remoteorigin_sha1 = run_git("rev-parse refs/remotes/${remotename}/master 2>/dev/null");
1193+
my $remoteorigin_sha1 = run_git_quoted_nostderr(["rev-parse", "refs/remotes/${remotename}/master"]);
11811194
chomp($remoteorigin_sha1);
11821195

11831196
if ($last_local_revid > 0 &&
@@ -1197,7 +1210,7 @@ sub mw_push_revision {
11971210
my $parsed_sha1 = $remoteorigin_sha1;
11981211
# Find a path from last MediaWiki commit to pushed commit
11991212
print {*STDERR} "Computing path from local to remote ...\n";
1200-
my @local_ancestry = split(/\n/, run_git("rev-list --boundary --parents ${local} ^${parsed_sha1}"));
1213+
my @local_ancestry = split(/\n/, run_git_quoted(["rev-list", "--boundary", "--parents", $local, "^${parsed_sha1}"]));
12011214
my %local_ancestry;
12021215
foreach my $line (@local_ancestry) {
12031216
if (my ($child, $parents) = $line =~ /^-?([a-f0-9]+) ([a-f0-9 ]+)/) {
@@ -1221,7 +1234,7 @@ sub mw_push_revision {
12211234
# No remote mediawiki revision. Export the whole
12221235
# history (linearized with --first-parent)
12231236
print {*STDERR} "Warning: no common ancestor, pushing complete history\n";
1224-
my $history = run_git("rev-list --first-parent --children ${local}");
1237+
my $history = run_git_quoted(["rev-list", "--first-parent", "--children", $local]);
12251238
my @history = split(/\n/, $history);
12261239
@history = @history[1..$#history];
12271240
foreach my $line (reverse @history) {
@@ -1233,12 +1246,12 @@ sub mw_push_revision {
12331246
foreach my $commit_info_split (@commit_pairs) {
12341247
my $sha1_child = @{$commit_info_split}[0];
12351248
my $sha1_commit = @{$commit_info_split}[1];
1236-
my $diff_infos = run_git("diff-tree -r --raw -z ${sha1_child} ${sha1_commit}");
1249+
my $diff_infos = run_git_quoted(["diff-tree", "-r", "--raw", "-z", $sha1_child, $sha1_commit]);
12371250
# TODO: we could detect rename, and encode them with a #redirect on the wiki.
12381251
# TODO: for now, it's just a delete+add
12391252
my @diff_info_list = split(/\0/, $diff_infos);
12401253
# Keep the subject line of the commit message as mediawiki comment for the revision
1241-
my $commit_msg = run_git(qq(log --no-walk --format="%s" ${sha1_commit}));
1254+
my $commit_msg = run_git_quoted(["log", "--no-walk", '--format="%s"', $sha1_commit]);
12421255
chomp($commit_msg);
12431256
# Push every blob
12441257
while (@diff_info_list) {
@@ -1263,7 +1276,10 @@ sub mw_push_revision {
12631276
}
12641277
}
12651278
if (!$dumb_push) {
1266-
run_git(qq(notes --ref=${remotename}/mediawiki add -f -m "mediawiki_revision: ${mw_revision}" ${sha1_commit}));
1279+
run_git_quoted(["notes", "--ref=${remotename}/mediawiki",
1280+
"add", "-f", "-m",
1281+
"mediawiki_revision: ${mw_revision}",
1282+
$sha1_commit]);
12671283
}
12681284
}
12691285

@@ -1304,7 +1320,7 @@ sub get_mw_namespace_id {
13041320
# already cached. Namespaces are stored in form:
13051321
# "Name_of_namespace:Id_namespace", ex.: "File:6".
13061322
my @temp = split(/\n/,
1307-
run_git("config --get-all remote.${remotename}.namespaceCache"));
1323+
run_git_quoted(["config", "--get-all", "remote.${remotename}.namespaceCache"]));
13081324
chomp(@temp);
13091325
foreach my $ns (@temp) {
13101326
my ($n, $id) = split(/:/, $ns);
@@ -1358,7 +1374,7 @@ sub get_mw_namespace_id {
13581374

13591375
# Store explicitly requested namespaces on disk
13601376
if (!exists $cached_mw_namespace_id{$name}) {
1361-
run_git(qq(config --add remote.${remotename}.namespaceCache "${name}:${store_id}"));
1377+
run_git_quoted(["config", "--add", "remote.${remotename}.namespaceCache", "${name}:${store_id}"]);
13621378
$cached_mw_namespace_id{$name} = 1;
13631379
}
13641380
return $id;

contrib/mw-to-git/git-remote-mediawiki.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ objects from mediawiki just as one would do with a classic git
44
repository thanks to remote-helpers.
55

66
For more information, visit the wiki at
7-
https://github.com/moy/Git-Mediawiki/wiki
7+
https://github.com/Git-Mediawiki/Git-Mediawiki

contrib/mw-to-git/t/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
WEB/
2-
wiki/
2+
mediawiki/
33
trash directory.t*/
44
test-results/

contrib/mw-to-git/t/README

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ install the following packages (Debian/Ubuntu names, may need to be
1414
adapted for another distribution):
1515

1616
* lighttpd
17-
* php5
18-
* php5-cgi
19-
* php5-cli
20-
* php5-curl
21-
* php5-sqlite
17+
* php
18+
* php-cgi
19+
* php-cli
20+
* php-curl
21+
* php-sqlite
2222

2323
Principles and Technical Choices
2424
--------------------------------

contrib/mw-to-git/t/install-wiki/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)