Home  >  Article  >  Backend Development  >  Example analysis of comparing files in two different branch directories of svn in php

Example analysis of comparing files in two different branch directories of svn in php

黄舟
黄舟Original
2017-09-07 09:20:222156browse

php中对比svn两个不同分支目录的文件的实例分析

<?php
/**
 * 对比svn两个不同分支目录的文件
 */

class DiffDir
{/*{{{*/

    public function run($dir1,$dir2,$dirResult = &#39;/tmp/&#39;)
    {/*{{{*/
        $hash1 = $this->getFileInfo($dir1);        
        $hash2 = $this->getFileInfo($dir2);        
        $this->diff($dir1,$hash1,$dir2,$hash2,$dirResult);
    }/*}}}*/

    //查找文件信息
    public function getFileInfo($dir)
    {/*{{{*/
        if(false == is_dir($dir))    
        {
            return array();    
        }
        $cmd = "find {$dir} -type f -name &#39;*.php&#39; | sort | xargs md5sum | awk &#39;{print $2,$1}&#39;";
        $ret = exec($cmd, $out, $status);
        if(is_array($out) && count($out) > 0)
        {
            return $this->formart($dir,$out);    
        }
        return array();
    }/*}}}*/

    //格式化文件
    public function formart($dir,array $data)
    {/*{{{*/
        $hash = array();
        foreach($data as $line)
        {
            $line = str_replace($dir,&#39;&#39;,$line);
            $arr = explode(" ",$line); 
            $hash[$arr[0]] = $arr[1];
        }
        return $hash;
    }/*}}}*/

    //对比文件
    public function diff($dir1,array $hash1,$dir2,array $hash2,$dirResult)
    {/*{{{*/
        foreach($hash1 as $key1 => $va11)
        {
            $fileName1 = $this->getRetFileName($dir1,$key1);
            if(false == isset($hash2[$key1])) 
            {
                $cmd =  "cp -f {$dir1}{$key1} {$dirResult}/{$fileName1}\n";
                exec($cmd, $out, $status);
                continue;
            }

            $fileName2 = $this->getRetFileName($dir2,$key1);
            if($va11 != $hash2[$key1]) 
            {
                $cmd =  "cp -f {$dir1}{$key1} {$dirResult}/{$fileName1}\n";
                exec($cmd, $out, $status);
                $cmd =  "cp -f {$dir2}{$key1} {$dirResult}/{$fileName2}\n";
                exec($cmd, $out, $status);
                continue;
            }
            
        }

        foreach($hash2 as $key2 => $va12)
        {
            $fileName2 = $this->getRetFileName($dir2,$key2);
            if(false == isset($hash1[$key2])) 
            {
                $cmd =  "cp -f {$dir2}{$key2} {$dirResult}/{$fileName2}\n";
                exec($cmd, $out, $status);
                continue;
            }
        }
    }/*}}}*/

    public function getRetFileName($dir,$file)
    {
        return basename($file).str_replace("/",&#39;_&#39;,dirname($dir.$file)); 
    }
}/*}}}*/

$diffDir = new DiffDir();
$diffDir->run(&#39;/tmp/diffDir/v6.1.0&#39;,&#39;/tmp/diffDir/v6.2.0&#39;,&#39;/tmp/diffDir/result&#39;);
?>

The above is the detailed content of Example analysis of comparing files in two different branch directories of svn in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn