svnexport.php

<?php
/**
Script to export only updated files between 2 revisions svn


@verison 1.0
@author  A.P.Palaniraja <A.P.Palaniraja@gmail.com>
@copyright 2008
@license GNU GPL, http://www.gnu.org/licenses/gpl.html
*/

//var_dump($_POST);

//Inputs for the scripts
    
if(!empty($_POST['svnlog']) && !empty($_POST['wc'])){
        
$svnlog $_POST['svnlog'];
        
$sourcePath $_POST['wc'];
        
$outputPath '2bUploaded/'//from current folder
        
if(isset($_POST['skipexport'])){
            
$skipExport true;
        }
        else
            
$skipExport false;
    }
    else{
    
//Sample svnlog to test
    
$svnlog '------------------------------------------------------------------------
r2 | palanirajaap | 2008-09-18 16:08:12 +0530 (Thu, 18 Sep 2008) | 3 lines
Changed paths:
   M /lib/templates/admin/login.html
   A /www/admin/assets/images/supremeads.gif

logo changed
title
and login changed
------------------------------------------------------------------------
r3 | palanirajaap | 2008-09-18 18:26:52 +0530 (Thu, 18 Sep 2008) | 1 line
Changed paths:
   M /lib/templates/admin/login.html
   M /var/localhost.conf.php
   A /www/admin/assets/images/loginn.jpg
   D /www/admin/assets/images/loginn2.jpg
   A /www/admin/assets/images/logo.gif

changes made in login, local config
------------------------------------------------------------------------'
;
      
$sourcePath 'www/htdocs/';  //from current folder
      
$outputPath '2bUploaded/'//from current folder
      
$skipExport false;
    }

/*Patterns for svn log  conversion*/
    
$svnRevStart "/\-{72,}/"//72 dashes(-)
    
$noOfChanges "/\|\ (\d)+\ line(s)*/";  //get 3 lines from the text "r2 | palanirajaap | 2008-09-18 16:08:12 +0530 (Thu, 18 Sep 2008) | 3 lines" //but never used
    
$fileStatus "/(\ ){3}[A-Z]{1}/";  //"   M /lib/templates/admin/login.html"
    
$skipStatusChars 5;  //get the file, by skipping "...M.";

//The task
    
$lines explode("\n",$svnlog);
    echo 
"<pre><h1>SvnExport</h1>";
    
//print_r($lines);
    
    
$files = array();
    
$delFiles = array();
    
$filesWithStatus = array();
    
$comments false;
    
  
$totLines count($lines)-1//skip the last line of 72 dashes(-);
    
for($i=0;$i<$totLines;$i++){
      
//echo "\nInside FOR i = $i";
      
$curLine $lines[$i];
         
//remove \r and \n
        
$curLine str_replace("\r"""$curLine);
        
$curLine str_replace("\n"""$curLine);
     
     
//echo "\nLine $i has length : ".strlen($curLine);//get the empty line between files and comment
       
if(!strlen($curLine)){
           
$comments true;
         continue;
//skip the empty line
        
}
    
//check if it is begining of the revision
        
preg_match($svnRevStart$curLine$matches);
        if(
count($matches)){
          
$comments false;
          echo 
"\nVersion start at $i. ";//var_dump($matches);
          
$i++; //get the meta tags
      
$metaTags $lines[$i];
      echo 
"\n\tMetaTags at $i: ".$metaTags;
      
//var_dump($curLine);
      //skip the "changed paths"
      
$i++;
    }
    else{
    
//get the list
      
if($comments){
        
//skip the comments 
        
continue;
      }
      else {
        
//$files[] = $curLine;
        //split the Status and File
        
preg_match($fileStatus$curLine$status); //will return array("   M"," ")
        
$sts trim($status[0]);
       
//skip the file if it is deleted
        
$file trim(substr($curLine$skipStatusChars)); //get the file, by skipping "...M."; 
        
$fileWithStatus $sts."@@".$file;
        
//print_r($fileWithStatus);
        
if($sts == 'D'){
          
$delFiles[] = $file;
        }
        else{
          
$files[] = $file;
        }
      }
    }
    
    }
    echo 
"\n<h3>Files updated(".count($files).")</h3>";
    
print_r(array_unique($files)); 
    echo 
"<h3>Files Deleted(".count($delFiles).")</h3>";
    
print_r($delFiles);

    if(
$skipExport){   //skip the files export
       
echo "<h3>Dry Run mode, files are not exported.</h3>";
       exit;
    }
    
    
//build the files to be exported
    
foreach($files as $file){  //if there is any file updated more than once then it will be available only once, so no array_unique reqd.
        
$newFile $outputPath.$file;
        
$sourceFile $sourcePath.$file;
        
MkDirRec(dirname($newFile));
        
copy($sourceFile,$newFile);
  }
  echo 
"<h3>Files are exported to $outputPath</h3>";
  
  
  function 
MkDirRec($dir$mode 0777)
  {
       if (
is_dir($dir) || @mkdir($dir,$mode)) return TRUE;
       if (!
MkDirRec(dirname($dir),$mode)) return FALSE;
       return 
mkdir($dir,$mode);
  }