Terrrydactyl has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/51074


Change subject: Added sparse checkout functionality.
......................................................................

Added sparse checkout functionality.

Created two new functions: SparseCheckoutNewRepo and AddToSparseCheckout.
SparseCheckoutNewRepo will clone the repo and keep only the file given by the
tag #snippet. Sparse checkout will only take the file from the given branch.
The md5 hash has been updated to include the branch name so that each branch
from the same repo will have its own folder.

AddToSparseCheckout will add files to the sparse checkout if the git repo/branch
combination already exists. It will update the tree with `git read-tree -mu 
HEAD`
whenever the file is edited.

Change-Id: I3cb8ef0f7aa38e8620e2d5580d2810ac6af661c8
---
M Git2Pages.body.php
M GitRepository.php
2 files changed, 61 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Git2Pages 
refs/changes/74/51074/1

diff --git a/Git2Pages.body.php b/Git2Pages.body.php
index 28e0021..3d8bd17 100644
--- a/Git2Pages.body.php
+++ b/Git2Pages.body.php
@@ -1,6 +1,6 @@
 <?php
 /**
- * Execution code
+ * Execution code 
  */
 include 'GitRepository.php';
 
@@ -51,20 +51,19 @@
                for ( $i = 1; $i < func_num_args(); $i++ ) {
                        $opts[] = func_get_arg( $i );
                }
-               $options = Git2PagesHooks::extractOptions( $opts );
+               $options = self::extractOptions( $opts );
                $url = $options['repository'];
-               $gitFolder =  $wgGit2PagesDataDir . DIRECTORY_SEPARATOR . md5( 
$url );
+               if( isset( $options['branch'] ) ) {
+                       $branch = wfEscapeShellArg( $options['branch'] );
+               } else {
+                       $branch = 'master';
+               }
+               $gitFolder =  $wgGit2PagesDataDir . DIRECTORY_SEPARATOR . md5( 
$url . $branch );
                if( !isset( $options['repository'] ) || !isset( 
$options['filename'] ) ) {
                        return 'repository and/or filename not defined.';
                }
                $gitRepo = new GitRepository( $url );
-               $gitRepo->CloneGitRepo( $url, $gitFolder );
-               if( isset( $options['branch'] ) ) {
-                       $gitRepo->GitCheckoutBranch( 
wfEscapeShellArg($options['branch'] ), $gitFolder );
-               }
-               else {
-                       $gitRepo->GitCheckoutBranch( 'master', $gitFolder );
-               }
+               $filename = $options['filename'];
                $startLine = isset( $options['startline'] ) ? 
$options['startline'] : 1;
                $endLine = isset( $options['endline'] ) ? $options['endline'] : 
-1;
                if( !self::isint( $startLine ) ) {
@@ -74,11 +73,12 @@
                        return '<strong class="error">endline is not an 
integer.</strong>';
                }
                try {
-                       $fileContents = $gitRepo->FindAndReadFile( 
$options['filename'], $gitFolder, $startLine, $endLine );
+                       $gitRepo->SparseCheckoutNewRepo( $url, $gitFolder, 
$filename, $branch );
+                       $fileContents = $gitRepo->FindAndReadFile( $filename, 
$gitFolder, $startLine, $endLine );
                        $output = '<pre>' . htmlspecialchars( $fileContents ) . 
'</pre>';
                } catch( Exception $ex ) {
                        $output = '<strong class="error">' . $ex->getMessage() 
. '</strong>';
                }
-               return array( $output, 'nowiki' => true, 'noparse' => true, 
'isHTML' => true );
+               return array( $gitFolder . $output, 'nowiki' => true, 'noparse' 
=> true, 'isHTML' => true );
        }
 }
diff --git a/GitRepository.php b/GitRepository.php
index eef3a8a..3ac4abd 100644
--- a/GitRepository.php
+++ b/GitRepository.php
@@ -17,8 +17,8 @@
        /**
         * Clones Git repo in unique folder.
         *
-        * @param string $dataDir contains directory where repo is cloned.
-        * @param string $gitFolder path to local git repo
+        * @param string $url contains the git url
+        * @param string $gitFolder path to local git repo where repo is cloned
         */
        static function CloneGitRepo( $url, $gitFolder ) {
                if( !file_exists( $gitFolder ) ) {
@@ -31,6 +31,53 @@
        }
 
        /**
+        * Adds a file or directory to sparse-checkout and updates the working 
tree
+        *
+        * @param string $gitFolder contains path to local Git repository
+        * @param string $checkoutItem contains the file or directory to 
checkout
+        */
+       static function AddToSparseCheckout( $gitFolder, $checkoutItem ) {
+               $oldDir = getcwd();
+               chdir( $gitFolder );
+               $sparseCheckoutFile = '.git/info/sparse-checkout';
+               if( $file = file_get_contents( $gitFolder . DIRECTORY_SEPARATOR 
. $sparseCheckoutFile ) ) {
+                       if( strpos( $file, $checkoutItem ) === false ) {
+                               wfShellExec( 'echo ' . $checkoutItem . ' >> ' . 
$sparseCheckoutFile );
+                       }
+               } else {
+                       wfShellExec( 'touch ' . $sparseCheckoutFile );
+                       wfShellExec( 'echo ' . $checkoutItem . ' >> ' . 
$sparseCheckoutFile );
+               }
+               wfShellExec( 'git read-tree -mu HEAD' );
+               chdir( $oldDir );
+       }
+       /**
+        * Clones just the .git folder
+        *
+        * @param string $url
+        * @param string $gitFolder
+        */
+       static function SparseCheckoutNewRepo( $url, $gitFolder, $checkoutItem, 
$branch ) {
+               $oldDir = getcwd();
+               if( !file_exists( $gitFolder ) ) {
+                       mkdir( $gitFolder );
+                       chdir( $gitFolder );
+                       $sparseCheckoutFile = '.git/info/sparse-checkout';
+                       wfShellExec( 'git init' );
+                       wfShellExec( 'git remote add -f origin ' . $url );
+                       wfShellExec( 'git config core.sparsecheckout true' );
+                       wfShellExec( 'touch ' . $sparseCheckoutFile );
+                       wfShellExec( 'echo ' . $checkoutItem . ' >> ' . 
$sparseCheckoutFile );
+                       wfShellExec( 'git pull ' . $url . ' ' . $branch );
+                       wfDebug( 'GitRepository: Sparse checkout subdirectory' 
);
+                       chdir( $oldDir );
+               } else {
+                       wfDebug( 'GitRepository: Git folder already exists, 
will add to sparse checkout' );
+                       self::AddToSparseCheckout( $gitFolder, $checkoutItem );
+               }
+       }
+
+       /**
         * Checkouts out Git branch
         *
         * @param string $branch is the branch to be checked

-- 
To view, visit https://gerrit.wikimedia.org/r/51074
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3cb8ef0f7aa38e8620e2d5580d2810ac6af661c8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Git2Pages
Gerrit-Branch: master
Gerrit-Owner: Terrrydactyl <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to