r113665 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113664‎ | r113665 | r113666 >
Date:20:55, 12 March 2012
Author:reedy
Status:ok
Tags:
Comment:
Bug 27812 - Make ExtensionDistributor work with Git

Abstract out VCS base stuff, add code to handle based on version control system

Used Status objects in refactored code


svn-invoker.php isn't quite finished, so will be coming in a followup commit
Modified paths:
  • /trunk/extensions/ExtensionDistributor/ExtensionDistributor.i18n.php (modified) (history)
  • /trunk/extensions/ExtensionDistributor/ExtensionDistributor.php (modified) (history)
  • /trunk/extensions/ExtensionDistributor/ExtensionDistributorGit.php (added) (history)
  • /trunk/extensions/ExtensionDistributor/ExtensionDistributorSVN.php (added) (history)
  • /trunk/extensions/ExtensionDistributor/ExtensionDistributorVCS.php (added) (history)
  • /trunk/extensions/ExtensionDistributor/ExtensionDistributor_body.php (modified) (history)
  • /trunk/extensions/ExtensionDistributor/cron.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ExtensionDistributor/ExtensionDistributorGit.php
@@ -0,0 +1,42 @@
 2+<?php
 3+
 4+class ExtensionDistributorGit extends ExtensionDistributorVCS {
 5+
 6+ /**
 7+ * @param $dir string
 8+ * @return Status
 9+ */
 10+ public function updateAndGetVersion( $dir ) {
 11+ if ( !chdir( $dir ) ) {
 12+ return Status::newFatal( 'extdist-git-invalid-dir' );
 13+ }
 14+ // Is -q (quiet) enough?
 15+ $cmd = "git pull -q 2>&1";
 16+ $retval = -1;
 17+
 18+ $result = wfShellExec( $cmd, $retval );
 19+
 20+ if ( $retval ) {
 21+ return Status::newFatal( 'extdist-git-error', $result );
 22+ }
 23+
 24+ // Determine last changed revision
 25+ $cmd = "git rev-parse HEAD";
 26+ $retval = -1;
 27+ $result = wfShellExec( $cmd, $retval );
 28+
 29+ if ( $retval ) {
 30+ return Status::newFatal( 'extdist-git-error', $result );
 31+ }
 32+
 33+ // Trim trailing whitespace
 34+ $result = rtrim( $result );
 35+
 36+ // Check it looks like a SHA1 hash
 37+ if ( !preg_match( '/^[0-9a-f]{40}$/i', $result ) ) {
 38+ return Status::newFatal( 'extdist-git-invalidsha1', $result );
 39+ }
 40+
 41+ return Status::newGood( $result );
 42+ }
 43+}
Property changes on: trunk/extensions/ExtensionDistributor/ExtensionDistributorGit.php
___________________________________________________________________
Added: svn:eol-style
144 + native
Index: trunk/extensions/ExtensionDistributor/ExtensionDistributorSVN.php
@@ -0,0 +1,37 @@
 2+<?php
 3+
 4+class ExtensionDistributorSVN extends ExtensionDistributorVCS {
 5+
 6+ /**
 7+ * @param $dir string
 8+ * @return Status
 9+ */
 10+ public function updateAndGetVersion( $dir ) {
 11+ $cmd = "svn up --non-interactive " . wfEscapeShellArg( $dir ) . " 2>&1";
 12+ $retval = -1;
 13+
 14+ $result = wfShellExec( $cmd, $retval );
 15+
 16+ if ( $retval ) {
 17+ return Status::newFatal( 'extdist-svn-error', $result );
 18+ }
 19+
 20+ // Determine last changed revision
 21+ $cmd = "svn info --non-interactive --xml " . wfEscapeShellArg( $dir );
 22+ $retval = -1;
 23+ $result = wfShellExec( $cmd, $retval );
 24+
 25+ if ( $retval ) {
 26+ return Status::newFatal( 'extdist-svn-error', $result );
 27+ }
 28+
 29+ $sx = new SimpleXMLElement( $result );
 30+ $rev = $sx->entry->commit['revision'];
 31+
 32+ if ( !$rev || strpos( $rev, '/' ) !== false || strpos( $rev, "\000" ) !== false ) {
 33+ return Status::newFatal( 'extdist-svn-parse-error', $result );
 34+ }
 35+
 36+ return Status::newGood( $rev );
 37+ }
 38+}
Property changes on: trunk/extensions/ExtensionDistributor/ExtensionDistributorSVN.php
___________________________________________________________________
Added: svn:eol-style
139 + native
Index: trunk/extensions/ExtensionDistributor/cron.php
@@ -35,18 +35,65 @@
3636 }
3737
3838 // Update the files
39 - svnUpdate( "$wgExtDistWorkingCopy/trunk/extensions" );
 39+ vcsUpdate( "$wgExtDistWorkingCopy/trunk/extensions" );
 40+
4041 foreach ( glob( "$wgExtDistWorkingCopy/branches/*", GLOB_ONLYDIR ) as $branch ) {
41 - svnUpdate( "$branch/extensions" );
 42+ vcsUpdate( "$branch/extensions" );
4243 }
4344 }
4445
 46+/**
 47+ * @param $dir string
 48+ * @return bool
 49+ */
 50+function isSVNDir( $dir ) {
 51+ return is_dir( "$dir/.svn");
 52+}
 53+
 54+/**
 55+ * @param $dir string
 56+ * @return bool
 57+ */
 58+function isGitDir( $dir ) {
 59+ return is_dir( "$dir/.git");
 60+}
 61+
 62+/**
 63+ * @param $dir string
 64+ */
 65+function vcsUpdate( $dir ) {
 66+ if ( isSVNDir( $dir ) ) {
 67+ svnUpdate( $dir );
 68+ } elseif ( isGitDir( $dir ) ) {
 69+ gitUpdate( $dir );
 70+ } else {
 71+ // Meh?
 72+ }
 73+}
 74+
 75+/**
 76+ * @param $path string
 77+ */
4578 function svnUpdate( $path ) {
4679 $cmd = "svn up --non-interactive " . escapeshellarg( $path );
47 - $retval = 1;
 80+ $retval = -1;
4881 system( $cmd, $retval );
4982 if ( $retval ) {
5083 echo "Error executing command: $cmd\n";
5184 exit( 1 );
5285 }
5386 }
 87+
 88+/**
 89+ * @param $path string
 90+ */
 91+function gitUpdate( $path ) {
 92+ chdir( $path );
 93+ $cmd = "git pull -q";
 94+ $retval = -1;
 95+ system( $cmd, $retval );
 96+ if ( $retval ) {
 97+ echo "Error executing command: $cmd\n";
 98+ exit( 1 );
 99+ }
 100+}
Index: trunk/extensions/ExtensionDistributor/ExtensionDistributorVCS.php
@@ -0,0 +1,24 @@
 2+<?php
 3+
 4+abstract class ExtensionDistributorVCS {
 5+
 6+ /**
 7+ * @param $vcs string
 8+ * @return ExtensionDistributorGIT|ExtensionDistributorSVN|null
 9+ */
 10+ public static function factory( $vcs ) {
 11+ if ( $vcs === 'svn' ) {
 12+ return new ExtensionDistributorSVN;
 13+ } elseif ( $vcs === 'git' ) {
 14+ return new ExtensionDistributorGit;
 15+ }
 16+ return null;
 17+ }
 18+
 19+ /**
 20+ * @abstract
 21+ * @param $dir string
 22+ * @return Status
 23+ */
 24+ abstract function updateAndGetVersion( $dir );
 25+}
Property changes on: trunk/extensions/ExtensionDistributor/ExtensionDistributorVCS.php
___________________________________________________________________
Added: svn:eol-style
126 + native
Index: trunk/extensions/ExtensionDistributor/ExtensionDistributor_body.php
@@ -223,10 +223,11 @@
224224 global $wgExtDistWorkingCopy, $wgExtDistTarDir, $wgExtDistBranches,
225225 $wgOut, $wgExtDistTarUrl, $wgExtDistRemoteClient;
226226
 227+ $vcs = isset( $wgExtDistBranches[$version]['vcs'] ) ? $wgExtDistBranches[$version]['vcs'] : 'svn';
227228 if ( $wgExtDistRemoteClient ) {
228 - $rev = $this->updateAndGetRevisionRemote( $extension, $version );
 229+ $rev = $this->updateAndGetRevisionRemote( $extension, $version, $vcs );
229230 } else {
230 - $rev = $this->updateAndGetRevisionLocal( $extension, $version );
 231+ $rev = $this->updateAndGetRevisionLocal( $extension, $version, $vcs );
231232 }
232233
233234 if ( $rev === false ) {
@@ -286,54 +287,45 @@
287288 /**
288289 * @param $extension string
289290 * @param $version string
 291+ * @param $vcs string Version control system to use for branch
290292 * @return bool|string
291293 */
292 - protected function updateAndGetRevisionLocal( $extension, $version ) {
 294+ protected function updateAndGetRevisionLocal( $extension, $version, $vcs ) {
293295 global $wgExtDistWorkingCopy, $wgOut;
294296
295297 // svn up
296298 $dir = "$wgExtDistWorkingCopy/$version/extensions/$extension";
297299
298 - $cmd = "svn up --non-interactive " . wfEscapeShellArg( $dir ) . " 2>&1";
299 - $retval = - 1;
300 -
301 - $result = wfShellExec( $cmd, $retval );
302 -
303 - if ( $retval ) {
304 - $wgOut->addWikiMsg( 'extdist-svn-error', $result );
 300+ $ed = ExtensionDistributorVCS::factory( $vcs );
 301+ if ( $ed === null ) {
 302+ $wgOut->addWikiMsg( 'extdist-cvs-unsupported', $vcs );
305303 return false;
306304 }
307305
308 - // Determine last changed revision
309 - $cmd = "svn info --non-interactive --xml " . wfEscapeShellArg( $dir );
310 - $retval = - 1;
311 - $result = wfShellExec( $cmd, $retval );
312 -
313 - if ( $retval ) {
314 - $wgOut->addWikiMsg( 'extdist-svn-error', $result );
 306+ $result = $ed->updateAndGetVersion( $dir );
 307+ if ( !$result->isGood() ) {
 308+ $this->getOutput()->addWikiText( $result->getWikiText() );
315309 return false;
316310 }
317 -
318 - $sx = new SimpleXMLElement( $result );
319 - $rev = $sx->entry->commit['revision'];
320 -
321 - if ( !$rev || strpos( $rev, '/' ) !== false || strpos( $rev, "\000" ) !== false ) {
322 - $wgOut->addWikiMsg( 'extdist-svn-parse-error', $result );
323 - return false;
324 - }
325 -
326 - return $rev;
 311+ return $result->value;
327312 }
328313
329314 /**
330315 * @param $extension string
331316 * @param $version string
 317+ * @param $vcs string Version control system to use for branch
332318 * @return bool|string
333319 */
334 - protected function updateAndGetRevisionRemote( $extension, $version ) {
 320+ protected function updateAndGetRevisionRemote( $extension, $version, $vcs ) {
335321 global $wgExtDistRemoteClient, $wgOut;
336322
337 - $cmd = json_encode( array( 'extension' => $extension, 'version' => $version ) );
 323+ $cmd = json_encode(
 324+ array(
 325+ 'extension' => $extension,
 326+ 'version' => $version,
 327+ 'vcs' => $vcs
 328+ )
 329+ );
338330 $cmd = str_replace( "\000", '', $cmd );
339331
340332 list( $host, $port ) = explode( ':', $wgExtDistRemoteClient, 2 );
Index: trunk/extensions/ExtensionDistributor/ExtensionDistributor.i18n.php
@@ -26,6 +26,8 @@
2727 'extdist-remote-invalid-response' => 'Invalid response from remote subversion client.',
2828 'extdist-svn-error' => 'Subversion encountered an error: <pre>$1</pre>',
2929 'extdist-svn-parse-error' => 'Unable to process the XML from "svn info": <pre>$1</pre>',
 30+ 'extdist-git-error' => 'Git encountered an error: <pre>$1</pre>',
 31+ 'extdist-git-invalidsha1' => 'Git returned an invalid SHA1 hash for the current revision: <pre>$1<pre>',
3032 'extdist-tar-error' => 'Tar returned exit code $1:',
3133 'extdist-created' => "A snapshot of version <b>$2</b> of the <b>$1</b> extension for MediaWiki <b>$3</b> has been created. Your download should start automatically in 5 seconds.
3234
@@ -47,6 +49,8 @@
4850
4951 If you have any questions about this extension distribution system, please go to [[Extension talk:ExtensionDistributor]].",
5052 'extdist-want-more' => 'Get another extension',
 53+ 'extdist-cvs-unsupported' => 'Extension Distributor doesn\'t support the "$1" version control system.',
 54+ 'extdist-git-invalid-dir' => 'Unable to enter directory for git checkout',
5155 );
5256
5357 /** Message documentation (Message documentation)
Index: trunk/extensions/ExtensionDistributor/ExtensionDistributor.php
@@ -49,3 +49,6 @@
5050 $wgSpecialPages['ExtensionDistributor'] = 'ExtensionDistributorPage';
5151 $wgSpecialPageGroups['ExtensionDistributor'] = 'developer';
5252 $wgAutoloadClasses['ExtensionDistributorPage'] = $dir . 'ExtensionDistributor_body.php';
 53+$wgAutoloadClasses['ExtensionDistributorSVN'] = $dir . 'ExtensionDistributorSVN.php';
 54+$wgAutoloadClasses['ExtensionDistributorVCS'] = $dir . 'ExtensionDistributorVCS.php';
 55+$wgAutoloadClasses['ExtensionDistributorGit'] = $dir . 'ExtensionDistributorGit.php';

Follow-up revisions

RevisionCommit summaryAuthorDate
r113813Bug 27812 - Make ExtensionDistributor work with Git...reedy15:37, 14 March 2012
r113815Bug 27812 - Make ExtensionDistributor work with Git...reedy16:00, 14 March 2012

Status & tagging log