1#! /usr/bin/perl 2 3# Copyright (C) 2011 4# Jérémie Nikaes <jeremie.nikaes@ensimag.imag.fr> 5# Arnaud Lacurie <arnaud.lacurie@ensimag.imag.fr> 6# Claire Fousse <claire.fousse@ensimag.imag.fr> 7# David Amouyal <david.amouyal@ensimag.imag.fr> 8# Matthieu Moy <matthieu.moy@grenoble-inp.fr> 9# License: GPL v2 or later 10 11# Gateway between Git and MediaWiki. 12# https://github.com/Bibzball/Git-Mediawiki/wiki 13# 14# Known limitations: 15# 16# - Only wiki pages are managed, no support for [[File:...]] 17# attachments. 18# 19# - Poor performance in the best case: it takes forever to check 20# whether we're up-to-date (on fetch or push) or to fetch a few 21# revisions from a large wiki, because we use exclusively a 22# page-based synchronization. We could switch to a wiki-wide 23# synchronization when the synchronization involves few revisions 24# but the wiki is large. 25# 26# - Git renames could be turned into MediaWiki renames (see TODO 27# below) 28# 29# - login/password support requires the user to write the password 30# cleartext in a file (see TODO below). 31# 32# - No way to import "one page, and all pages included in it" 33# 34# - Multiple remote MediaWikis have not been very well tested. 35 36use strict; 37use MediaWiki::API; 38use DateTime::Format::ISO8601; 39use encoding 'utf8'; 40 41# use encoding 'utf8' doesn't change STDERROR 42# but we're going to output UTF-8 filenames to STDERR 43binmode STDERR,":utf8"; 44 45use URI::Escape; 46use warnings; 47 48# Mediawiki filenames can contain forward slashes. This variable decides by which pattern they should be replaced 49useconstant SLASH_REPLACEMENT =>"%2F"; 50 51# It's not always possible to delete pages (may require some 52# priviledges). Deleted pages are replaced with this content. 53useconstant DELETED_CONTENT =>"[[Category:Deleted]]\n"; 54 55# It's not possible to create empty pages. New empty files in Git are 56# sent with this content instead. 57useconstant EMPTY_CONTENT =>"<!-- empty page -->\n"; 58 59# used to reflect file creation or deletion in diff. 60useconstant NULL_SHA1 =>"0000000000000000000000000000000000000000"; 61 62my$remotename=$ARGV[0]; 63my$url=$ARGV[1]; 64 65# Accept both space-separated and multiple keys in config file. 66# Spaces should be written as _ anyway because we'll use chomp. 67my@tracked_pages=split(/[ \n]/, run_git("config --get-all remote.".$remotename.".pages")); 68chomp(@tracked_pages); 69 70# Just like @tracked_pages, but for MediaWiki categories. 71my@tracked_categories=split(/[ \n]/, run_git("config --get-all remote.".$remotename.".categories")); 72chomp(@tracked_categories); 73 74my$wiki_login= run_git("config --get remote.".$remotename.".mwLogin"); 75# TODO: ideally, this should be able to read from keyboard, but we're 76# inside a remote helper, so our stdin is connect to git, not to a 77# terminal. 78my$wiki_passwd= run_git("config --get remote.".$remotename.".mwPassword"); 79chomp($wiki_login); 80chomp($wiki_passwd); 81 82# Import only last revisions (both for clone and fetch) 83my$shallow_import= run_git("config --get --bool remote.".$remotename.".shallow"); 84chomp($shallow_import); 85$shallow_import= ($shallow_importeq"true"); 86 87# Dumb push: don't update notes and mediawiki ref to reflect the last push. 88# 89# Configurable with mediawiki.dumbPush, or per-remote with 90# remote.<remotename>.dumbPush. 91# 92# This means the user will have to re-import the just-pushed 93# revisions. On the other hand, this means that the Git revisions 94# corresponding to MediaWiki revisions are all imported from the wiki, 95# regardless of whether they were initially created in Git or from the 96# web interface, hence all users will get the same history (i.e. if 97# the push from Git to MediaWiki loses some information, everybody 98# will get the history with information lost). If the import is 99# deterministic, this means everybody gets the same sha1 for each 100# MediaWiki revision. 101my$dumb_push= run_git("config --get --bool remote.$remotename.dumbPush"); 102unless($dumb_push) { 103$dumb_push= run_git("config --get --bool mediawiki.dumbPush"); 104} 105chomp($dumb_push); 106$dumb_push= ($dumb_pusheq"true"); 107 108my$wiki_name=$url; 109$wiki_name=~s/[^\/]*:\/\///; 110 111# Commands parser 112my$entry; 113my@cmd; 114while(<STDIN>) { 115chomp; 116@cmd=split(/ /); 117if(defined($cmd[0])) { 118# Line not blank 119if($cmd[0]eq"capabilities") { 120die("Too many arguments for capabilities")unless(!defined($cmd[1])); 121 mw_capabilities(); 122}elsif($cmd[0]eq"list") { 123die("Too many arguments for list")unless(!defined($cmd[2])); 124 mw_list($cmd[1]); 125}elsif($cmd[0]eq"import") { 126die("Invalid arguments for import")unless($cmd[1]ne""&& !defined($cmd[2])); 127 mw_import($cmd[1]); 128}elsif($cmd[0]eq"option") { 129die("Too many arguments for option")unless($cmd[1]ne""&&$cmd[2]ne""&& !defined($cmd[3])); 130 mw_option($cmd[1],$cmd[2]); 131}elsif($cmd[0]eq"push") { 132 mw_push($cmd[1]); 133}else{ 134print STDERR "Unknown command. Aborting...\n"; 135last; 136} 137}else{ 138# blank line: we should terminate 139last; 140} 141 142BEGIN{ $| =1}# flush STDOUT, to make sure the previous 143# command is fully processed. 144} 145 146########################## Functions ############################## 147 148# MediaWiki API instance, created lazily. 149my$mediawiki; 150 151sub mw_connect_maybe { 152if($mediawiki) { 153return; 154} 155$mediawiki= MediaWiki::API->new; 156$mediawiki->{config}->{api_url} ="$url/api.php"; 157if($wiki_login) { 158if(!$mediawiki->login({ 159 lgname =>$wiki_login, 160 lgpassword =>$wiki_passwd, 161})) { 162print STDERR "Failed to log in mediawiki user\"$wiki_login\"on$url\n"; 163print STDERR "(error ". 164$mediawiki->{error}->{code} .': '. 165$mediawiki->{error}->{details} .")\n"; 166exit1; 167}else{ 168print STDERR "Logged in with user\"$wiki_login\".\n"; 169} 170} 171} 172 173sub get_mw_first_pages { 174my$some_pages=shift; 175my@some_pages= @{$some_pages}; 176 177my$pages=shift; 178 179# pattern 'page1|page2|...' required by the API 180my$titles=join('|',@some_pages); 181 182my$mw_pages=$mediawiki->api({ 183 action =>'query', 184 titles =>$titles, 185}); 186if(!defined($mw_pages)) { 187print STDERR "fatal: could not query the list of wiki pages.\n"; 188print STDERR "fatal: '$url' does not appear to be a mediawiki\n"; 189print STDERR "fatal: make sure '$url/api.php' is a valid page.\n"; 190exit1; 191} 192while(my($id,$page) =each(%{$mw_pages->{query}->{pages}})) { 193if($id<0) { 194print STDERR "Warning: page$page->{title} not found on wiki\n"; 195}else{ 196$pages->{$page->{title}} =$page; 197} 198} 199} 200 201sub get_mw_pages { 202 mw_connect_maybe(); 203 204my%pages;# hash on page titles to avoid duplicates 205my$user_defined; 206if(@tracked_pages) { 207$user_defined=1; 208# The user provided a list of pages titles, but we 209# still need to query the API to get the page IDs. 210 211my@some_pages=@tracked_pages; 212while(@some_pages) { 213my$last=50; 214if($#some_pages<$last) { 215$last=$#some_pages; 216} 217my@slice=@some_pages[0..$last]; 218 get_mw_first_pages(\@slice, \%pages); 219@some_pages=@some_pages[51..$#some_pages]; 220} 221} 222if(@tracked_categories) { 223$user_defined=1; 224foreachmy$category(@tracked_categories) { 225if(index($category,':') <0) { 226# Mediawiki requires the Category 227# prefix, but let's not force the user 228# to specify it. 229$category="Category:".$category; 230} 231my$mw_pages=$mediawiki->list( { 232 action =>'query', 233 list =>'categorymembers', 234 cmtitle =>$category, 235 cmlimit =>'max'} ) 236||die$mediawiki->{error}->{code} .': '.$mediawiki->{error}->{details}; 237foreachmy$page(@{$mw_pages}) { 238$pages{$page->{title}} =$page; 239} 240} 241} 242if(!$user_defined) { 243# No user-provided list, get the list of pages from 244# the API. 245my$mw_pages=$mediawiki->list({ 246 action =>'query', 247 list =>'allpages', 248 aplimit =>500, 249}); 250if(!defined($mw_pages)) { 251print STDERR "fatal: could not get the list of wiki pages.\n"; 252print STDERR "fatal: '$url' does not appear to be a mediawiki\n"; 253print STDERR "fatal: make sure '$url/api.php' is a valid page.\n"; 254exit1; 255} 256foreachmy$page(@{$mw_pages}) { 257$pages{$page->{title}} =$page; 258} 259} 260returnvalues(%pages); 261} 262 263sub run_git { 264open(my$git,"-|:encoding(UTF-8)","git ".$_[0]); 265my$res=do{local$/; <$git> }; 266close($git); 267 268return$res; 269} 270 271 272sub get_last_local_revision { 273# Get note regarding last mediawiki revision 274my$note= run_git("notes --ref=$remotename/mediawikishow refs/mediawiki/$remotename/master2>/dev/null"); 275my@note_info=split(/ /,$note); 276 277my$lastrevision_number; 278if(!(defined($note_info[0]) &&$note_info[0]eq"mediawiki_revision:")) { 279print STDERR "No previous mediawiki revision found"; 280$lastrevision_number=0; 281}else{ 282# Notes are formatted : mediawiki_revision: #number 283$lastrevision_number=$note_info[1]; 284chomp($lastrevision_number); 285print STDERR "Last local mediawiki revision found is$lastrevision_number"; 286} 287return$lastrevision_number; 288} 289 290sub get_last_remote_revision { 291 mw_connect_maybe(); 292 293my@pages= get_mw_pages(); 294 295my$max_rev_num=0; 296 297foreachmy$page(@pages) { 298my$id=$page->{pageid}; 299 300my$query= { 301 action =>'query', 302 prop =>'revisions', 303 rvprop =>'ids', 304 pageids =>$id, 305}; 306 307my$result=$mediawiki->api($query); 308 309my$lastrev=pop(@{$result->{query}->{pages}->{$id}->{revisions}}); 310 311$max_rev_num= ($lastrev->{revid} >$max_rev_num?$lastrev->{revid} :$max_rev_num); 312} 313 314print STDERR "Last remote revision found is$max_rev_num.\n"; 315return$max_rev_num; 316} 317 318# Clean content before sending it to MediaWiki 319sub mediawiki_clean { 320my$string=shift; 321my$page_created=shift; 322# Mediawiki does not allow blank space at the end of a page and ends with a single \n. 323# This function right trims a string and adds a \n at the end to follow this rule 324$string=~s/\s+$//; 325if($stringeq""&&$page_created) { 326# Creating empty pages is forbidden. 327$string= EMPTY_CONTENT; 328} 329return$string."\n"; 330} 331 332# Filter applied on MediaWiki data before adding them to Git 333sub mediawiki_smudge { 334my$string=shift; 335if($stringeq EMPTY_CONTENT) { 336$string=""; 337} 338# This \n is important. This is due to mediawiki's way to handle end of files. 339return$string."\n"; 340} 341 342sub mediawiki_clean_filename { 343my$filename=shift; 344$filename=~s/@{[SLASH_REPLACEMENT]}/\//g; 345# [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded. 346# Do a variant of URL-encoding, i.e. looks like URL-encoding, 347# but with _ added to prevent MediaWiki from thinking this is 348# an actual special character. 349$filename=~s/[\[\]\{\}\|]/sprintf("_%%_%x", ord($&))/ge; 350# If we use the uri escape before 351# we should unescape here, before anything 352 353return$filename; 354} 355 356sub mediawiki_smudge_filename { 357my$filename=shift; 358$filename=~s/\//@{[SLASH_REPLACEMENT]}/g; 359$filename=~s/ /_/g; 360# Decode forbidden characters encoded in mediawiki_clean_filename 361$filename=~s/_%_([0-9a-fA-F][0-9a-fA-F])/sprintf("%c", hex($1))/ge; 362return$filename; 363} 364 365sub literal_data { 366my($content) =@_; 367print STDOUT "data ", bytes::length($content),"\n",$content; 368} 369 370sub mw_capabilities { 371# Revisions are imported to the private namespace 372# refs/mediawiki/$remotename/ by the helper and fetched into 373# refs/remotes/$remotename later by fetch. 374print STDOUT "refspec refs/heads/*:refs/mediawiki/$remotename/*\n"; 375print STDOUT "import\n"; 376print STDOUT "list\n"; 377print STDOUT "push\n"; 378print STDOUT "\n"; 379} 380 381sub mw_list { 382# MediaWiki do not have branches, we consider one branch arbitrarily 383# called master, and HEAD pointing to it. 384print STDOUT "? refs/heads/master\n"; 385print STDOUT "\@refs/heads/masterHEAD\n"; 386print STDOUT "\n"; 387} 388 389sub mw_option { 390print STDERR "remote-helper command 'option$_[0]' not yet implemented\n"; 391print STDOUT "unsupported\n"; 392} 393 394sub fetch_mw_revisions_for_page { 395my$page=shift; 396my$id=shift; 397my$fetch_from=shift; 398my@page_revs= (); 399my$query= { 400 action =>'query', 401 prop =>'revisions', 402 rvprop =>'ids', 403 rvdir =>'newer', 404 rvstartid =>$fetch_from, 405 rvlimit =>500, 406 pageids =>$id, 407}; 408 409my$revnum=0; 410# Get 500 revisions at a time due to the mediawiki api limit 411while(1) { 412my$result=$mediawiki->api($query); 413 414# Parse each of those 500 revisions 415foreachmy$revision(@{$result->{query}->{pages}->{$id}->{revisions}}) { 416my$page_rev_ids; 417$page_rev_ids->{pageid} =$page->{pageid}; 418$page_rev_ids->{revid} =$revision->{revid}; 419push(@page_revs,$page_rev_ids); 420$revnum++; 421} 422last unless$result->{'query-continue'}; 423$query->{rvstartid} =$result->{'query-continue'}->{revisions}->{rvstartid}; 424} 425if($shallow_import&&@page_revs) { 426print STDERR " Found 1 revision (shallow import).\n"; 427@page_revs=sort{$b->{revid} <=>$a->{revid}} (@page_revs); 428return$page_revs[0]; 429} 430print STDERR " Found ",$revnum," revision(s).\n"; 431return@page_revs; 432} 433 434sub fetch_mw_revisions { 435my$pages=shift;my@pages= @{$pages}; 436my$fetch_from=shift; 437 438my@revisions= (); 439my$n=1; 440foreachmy$page(@pages) { 441my$id=$page->{pageid}; 442 443print STDERR "page$n/",scalar(@pages),": ".$page->{title} ."\n"; 444$n++; 445my@page_revs= fetch_mw_revisions_for_page($page,$id,$fetch_from); 446@revisions= (@page_revs,@revisions); 447} 448 449return($n,@revisions); 450} 451 452sub import_file_revision { 453my$commit=shift; 454my%commit= %{$commit}; 455my$full_import=shift; 456my$n=shift; 457 458my$title=$commit{title}; 459my$comment=$commit{comment}; 460my$content=$commit{content}; 461my$author=$commit{author}; 462my$date=$commit{date}; 463 464print STDOUT "commit refs/mediawiki/$remotename/master\n"; 465print STDOUT "mark :$n\n"; 466print STDOUT "committer$author<$author\@$wiki_name> ",$date->epoch," +0000\n"; 467 literal_data($comment); 468 469# If it's not a clone, we need to know where to start from 470if(!$full_import&&$n==1) { 471print STDOUT "from refs/mediawiki/$remotename/master^0\n"; 472} 473if($contentne DELETED_CONTENT) { 474print STDOUT "M 644 inline$title.mw\n"; 475 literal_data($content); 476print STDOUT "\n\n"; 477}else{ 478print STDOUT "D$title.mw\n"; 479} 480 481# mediawiki revision number in the git note 482if($full_import&&$n==1) { 483print STDOUT "reset refs/notes/$remotename/mediawiki\n"; 484} 485print STDOUT "commit refs/notes/$remotename/mediawiki\n"; 486print STDOUT "committer$author<$author\@$wiki_name> ",$date->epoch," +0000\n"; 487 literal_data("Note added by git-mediawiki during import"); 488if(!$full_import&&$n==1) { 489print STDOUT "from refs/notes/$remotename/mediawiki^0\n"; 490} 491print STDOUT "N inline :$n\n"; 492 literal_data("mediawiki_revision: ".$commit{mw_revision}); 493print STDOUT "\n\n"; 494} 495 496# parse a sequence of 497# <cmd> <arg1> 498# <cmd> <arg2> 499# \n 500# (like batch sequence of import and sequence of push statements) 501sub get_more_refs { 502my$cmd=shift; 503my@refs; 504while(1) { 505my$line= <STDIN>; 506if($line=~m/^$cmd (.*)$/) { 507push(@refs,$1); 508}elsif($lineeq"\n") { 509return@refs; 510}else{ 511die("Invalid command in a '$cmd' batch: ".$_); 512} 513} 514} 515 516sub mw_import { 517# multiple import commands can follow each other. 518my@refs= (shift, get_more_refs("import")); 519foreachmy$ref(@refs) { 520 mw_import_ref($ref); 521} 522print STDOUT "done\n"; 523} 524 525sub mw_import_ref { 526my$ref=shift; 527# The remote helper will call "import HEAD" and 528# "import refs/heads/master". 529# Since HEAD is a symbolic ref to master (by convention, 530# followed by the output of the command "list" that we gave), 531# we don't need to do anything in this case. 532if($refeq"HEAD") { 533return; 534} 535 536 mw_connect_maybe(); 537 538my@pages= get_mw_pages(); 539 540print STDERR "Searching revisions...\n"; 541my$last_local= get_last_local_revision(); 542my$fetch_from=$last_local+1; 543if($fetch_from==1) { 544print STDERR ", fetching from beginning.\n"; 545}else{ 546print STDERR ", fetching from here.\n"; 547} 548my($n,@revisions) = fetch_mw_revisions(\@pages,$fetch_from); 549 550# Creation of the fast-import stream 551print STDERR "Fetching & writing export data...\n"; 552 553$n=0; 554my$last_timestamp=0;# Placeholer in case $rev->timestamp is undefined 555 556foreachmy$pagerevid(sort{$a->{revid} <=>$b->{revid}}@revisions) { 557# fetch the content of the pages 558my$query= { 559 action =>'query', 560 prop =>'revisions', 561 rvprop =>'content|timestamp|comment|user|ids', 562 revids =>$pagerevid->{revid}, 563}; 564 565my$result=$mediawiki->api($query); 566 567my$rev=pop(@{$result->{query}->{pages}->{$pagerevid->{pageid}}->{revisions}}); 568 569$n++; 570 571my%commit; 572$commit{author} =$rev->{user} ||'Anonymous'; 573$commit{comment} =$rev->{comment} ||'*Empty MediaWiki Message*'; 574$commit{title} = mediawiki_smudge_filename( 575$result->{query}->{pages}->{$pagerevid->{pageid}}->{title} 576); 577$commit{mw_revision} =$pagerevid->{revid}; 578$commit{content} = mediawiki_smudge($rev->{'*'}); 579 580if(!defined($rev->{timestamp})) { 581$last_timestamp++; 582}else{ 583$last_timestamp=$rev->{timestamp}; 584} 585$commit{date} = DateTime::Format::ISO8601->parse_datetime($last_timestamp); 586 587print STDERR "$n/",scalar(@revisions),": Revision #$pagerevid->{revid} of$commit{title}\n"; 588 589 import_file_revision(\%commit, ($fetch_from==1),$n); 590} 591 592if($fetch_from==1&&$n==0) { 593print STDERR "You appear to have cloned an empty MediaWiki.\n"; 594# Something has to be done remote-helper side. If nothing is done, an error is 595# thrown saying that HEAD is refering to unknown object 0000000000000000000 596# and the clone fails. 597} 598} 599 600sub error_non_fast_forward { 601# Native git-push would show this after the summary. 602# We can't ask it to display it cleanly, so print it 603# ourselves before. 604print STDERR "To prevent you from losing history, non-fast-forward updates were rejected\n"; 605print STDERR "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"; 606print STDERR "'Note about fast-forwards' section of 'git push --help' for details.\n"; 607 608print STDOUT "error$_[0]\"non-fast-forward\"\n"; 609return0; 610} 611 612sub mw_push_file { 613my$diff_info=shift; 614# $diff_info contains a string in this format: 615# 100644 100644 <sha1_of_blob_before_commit> <sha1_of_blob_now> <status> 616my@diff_info_split=split(/[ \t]/,$diff_info); 617 618# Filename, including .mw extension 619my$complete_file_name=shift; 620# Commit message 621my$summary=shift; 622# MediaWiki revision number. Keep the previous one by default, 623# in case there's no edit to perform. 624my$newrevid=shift; 625 626my$new_sha1=$diff_info_split[3]; 627my$old_sha1=$diff_info_split[2]; 628my$page_created= ($old_sha1eq NULL_SHA1); 629my$page_deleted= ($new_sha1eq NULL_SHA1); 630$complete_file_name= mediawiki_clean_filename($complete_file_name); 631 632if(substr($complete_file_name,-3)eq".mw") { 633my$title=substr($complete_file_name,0,-3); 634 635my$file_content; 636if($page_deleted) { 637# Deleting a page usually requires 638# special priviledges. A common 639# convention is to replace the page 640# with this content instead: 641$file_content= DELETED_CONTENT; 642}else{ 643$file_content= run_git("cat-file blob$new_sha1"); 644} 645 646 mw_connect_maybe(); 647 648my$result=$mediawiki->edit( { 649 action =>'edit', 650 summary =>$summary, 651 title =>$title, 652 text => mediawiki_clean($file_content,$page_created), 653}, { 654 skip_encoding =>1# Helps with names with accentuated characters 655}) ||die'Fatal: Error '. 656$mediawiki->{error}->{code} . 657' from mediwiki: '.$mediawiki->{error}->{details}; 658$newrevid=$result->{edit}->{newrevid}; 659print STDERR "Pushed file:$new_sha1-$title\n"; 660}else{ 661print STDERR "$complete_file_namenot a mediawiki file (Not pushable on this version of git-remote-mediawiki).\n" 662} 663return$newrevid; 664} 665 666sub mw_push { 667# multiple push statements can follow each other 668my@refsspecs= (shift, get_more_refs("push")); 669my$pushed; 670formy$refspec(@refsspecs) { 671my($force,$local,$remote) =$refspec=~/^(\+)?([^:]*):([^:]*)$/ 672or die("Invalid refspec for push. Expected <src>:<dst> or +<src>:<dst>"); 673if($force) { 674print STDERR "Warning: forced push not allowed on a MediaWiki.\n"; 675} 676if($localeq"") { 677print STDERR "Cannot delete remote branch on a MediaWiki\n"; 678print STDOUT "error$remotecannot delete\n"; 679next; 680} 681if($remotene"refs/heads/master") { 682print STDERR "Only push to the branch 'master' is supported on a MediaWiki\n"; 683print STDOUT "error$remoteonly master allowed\n"; 684next; 685} 686if(mw_push_revision($local,$remote)) { 687$pushed=1; 688} 689} 690 691# Notify Git that the push is done 692print STDOUT "\n"; 693 694if($pushed&&$dumb_push) { 695print STDERR "Just pushed some revisions to MediaWiki.\n"; 696print STDERR "The pushed revisions now have to be re-imported, and your current branch\n"; 697print STDERR "needs to be updated with these re-imported commits. You can do this with\n"; 698print STDERR "\n"; 699print STDERR " git pull --rebase\n"; 700print STDERR "\n"; 701} 702} 703 704sub mw_push_revision { 705my$local=shift; 706my$remote=shift;# actually, this has to be "refs/heads/master" at this point. 707my$last_local_revid= get_last_local_revision(); 708print STDERR ".\n";# Finish sentence started by get_last_local_revision() 709my$last_remote_revid= get_last_remote_revision(); 710my$mw_revision=$last_remote_revid; 711 712# Get sha1 of commit pointed by local HEAD 713my$HEAD_sha1= run_git("rev-parse$local2>/dev/null");chomp($HEAD_sha1); 714# Get sha1 of commit pointed by remotes/$remotename/master 715my$remoteorigin_sha1= run_git("rev-parse refs/remotes/$remotename/master2>/dev/null"); 716chomp($remoteorigin_sha1); 717 718if($last_local_revid>0&& 719$last_local_revid<$last_remote_revid) { 720return error_non_fast_forward($remote); 721} 722 723if($HEAD_sha1eq$remoteorigin_sha1) { 724# nothing to push 725return0; 726} 727 728# Get every commit in between HEAD and refs/remotes/origin/master, 729# including HEAD and refs/remotes/origin/master 730my@commit_pairs= (); 731if($last_local_revid>0) { 732my$parsed_sha1=$remoteorigin_sha1; 733# Find a path from last MediaWiki commit to pushed commit 734while($parsed_sha1ne$HEAD_sha1) { 735my@commit_info=grep(/^$parsed_sha1/,split(/\n/, run_git("rev-list --children$local"))); 736if(!@commit_info) { 737return error_non_fast_forward($remote); 738} 739my@commit_info_split=split(/ |\n/,$commit_info[0]); 740# $commit_info_split[1] is the sha1 of the commit to export 741# $commit_info_split[0] is the sha1 of its direct child 742push(@commit_pairs, \@commit_info_split); 743$parsed_sha1=$commit_info_split[1]; 744} 745}else{ 746# No remote mediawiki revision. Export the whole 747# history (linearized with --first-parent) 748print STDERR "Warning: no common ancestor, pushing complete history\n"; 749my$history= run_git("rev-list --first-parent --children$local"); 750my@history=split('\n',$history); 751@history=@history[1..$#history]; 752foreachmy$line(reverse@history) { 753my@commit_info_split=split(/ |\n/,$line); 754push(@commit_pairs, \@commit_info_split); 755} 756} 757 758foreachmy$commit_info_split(@commit_pairs) { 759my$sha1_child= @{$commit_info_split}[0]; 760my$sha1_commit= @{$commit_info_split}[1]; 761my$diff_infos= run_git("diff-tree -r --raw -z$sha1_child$sha1_commit"); 762# TODO: we could detect rename, and encode them with a #redirect on the wiki. 763# TODO: for now, it's just a delete+add 764my@diff_info_list=split(/\0/,$diff_infos); 765# Keep the first line of the commit message as mediawiki comment for the revision 766my$commit_msg= (split(/\n/, run_git("show --pretty=format:\"%s\"$sha1_commit")))[0]; 767chomp($commit_msg); 768# Push every blob 769while(@diff_info_list) { 770# git diff-tree -z gives an output like 771# <metadata>\0<filename1>\0 772# <metadata>\0<filename2>\0 773# and we've split on \0. 774my$info=shift(@diff_info_list); 775my$file=shift(@diff_info_list); 776$mw_revision= mw_push_file($info,$file,$commit_msg,$mw_revision); 777} 778unless($dumb_push) { 779 run_git("notes --ref=$remotename/mediawikiadd -m\"mediawiki_revision:$mw_revision\"$sha1_commit"); 780 run_git("update-ref -m\"Git-MediaWiki push\"refs/mediawiki/$remotename/master$sha1_commit$sha1_child"); 781} 782} 783 784print STDOUT "ok$remote\n"; 785return1; 786}