Skip to content

Commit 3c1ed90

Browse files
moygitster
authored andcommitted
git-remote-mediawiki: set 'basetimestamp' to let the wiki handle conflicts
We already have a check that no new revisions are on the wiki at the beginning of the push, but this didn't handle concurrent accesses to the wiki. Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac86ec0 commit 3c1ed90

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ sub get_last_local_revision {
287287
return $lastrevision_number;
288288
}
289289

290+
# Remember the timestamp corresponding to a revision id.
291+
my %basetimestamps;
292+
290293
sub get_last_remote_revision {
291294
mw_connect_maybe();
292295

@@ -300,14 +303,16 @@ sub get_last_remote_revision {
300303
my $query = {
301304
action => 'query',
302305
prop => 'revisions',
303-
rvprop => 'ids',
306+
rvprop => 'ids|timestamp',
304307
pageids => $id,
305308
};
306309

307310
my $result = $mediawiki->api($query);
308311

309312
my $lastrev = pop(@{$result->{query}->{pages}->{$id}->{revisions}});
310313

314+
$basetimestamps{$lastrev->{revid}} = $lastrev->{timestamp};
315+
311316
$max_rev_num = ($lastrev->{revid} > $max_rev_num ? $lastrev->{revid} : $max_rev_num);
312317
}
313318

@@ -649,18 +654,32 @@ sub mw_push_file {
649654
action => 'edit',
650655
summary => $summary,
651656
title => $title,
657+
basetimestamp => $basetimestamps{$newrevid},
652658
text => mediawiki_clean($file_content, $page_created),
653659
}, {
654660
skip_encoding => 1 # Helps with names with accentuated characters
655-
}) || die 'Fatal: Error ' .
656-
$mediawiki->{error}->{code} .
657-
' from mediwiki: ' . $mediawiki->{error}->{details};
661+
});
662+
if (!$result) {
663+
if ($mediawiki->{error}->{code} == 3) {
664+
# edit conflicts, considered as non-fast-forward
665+
print STDERR 'Warning: Error ' .
666+
$mediawiki->{error}->{code} .
667+
' from mediwiki: ' . $mediawiki->{error}->{details} .
668+
".\n";
669+
return ($newrevid, "non-fast-forward");
670+
} else {
671+
# Other errors. Shouldn't happen => just die()
672+
die 'Fatal: Error ' .
673+
$mediawiki->{error}->{code} .
674+
' from mediwiki: ' . $mediawiki->{error}->{details};
675+
}
676+
}
658677
$newrevid = $result->{edit}->{newrevid};
659678
print STDERR "Pushed file: $new_sha1 - $title\n";
660679
} else {
661680
print STDERR "$complete_file_name not a mediawiki file (Not pushable on this version of git-remote-mediawiki).\n"
662681
}
663-
return $newrevid;
682+
return ($newrevid, "ok");
664683
}
665684

666685
sub mw_push {
@@ -767,13 +786,25 @@ sub mw_push_revision {
767786
chomp($commit_msg);
768787
# Push every blob
769788
while (@diff_info_list) {
789+
my $status;
770790
# git diff-tree -z gives an output like
771791
# <metadata>\0<filename1>\0
772792
# <metadata>\0<filename2>\0
773793
# and we've split on \0.
774794
my $info = shift(@diff_info_list);
775795
my $file = shift(@diff_info_list);
776-
$mw_revision = mw_push_file($info, $file, $commit_msg, $mw_revision);
796+
($mw_revision, $status) = mw_push_file($info, $file, $commit_msg, $mw_revision);
797+
if ($status eq "non-fast-forward") {
798+
# we may already have sent part of the
799+
# commit to MediaWiki, but it's too
800+
# late to cancel it. Stop the push in
801+
# the middle, but still give an
802+
# accurate error message.
803+
return error_non_fast_forward($remote);
804+
}
805+
if ($status ne "ok") {
806+
die("Unknown error from mw_push_file()");
807+
}
777808
}
778809
unless ($dumb_push) {
779810
run_git("notes --ref=$remotename/mediawiki add -m \"mediawiki_revision: $mw_revision\" $sha1_commit");

0 commit comments

Comments
 (0)