ホームページ  >  記事  >  バックエンド開発  >  PHP_PHP で書かれた SVN クラスのチュートリアル

PHP_PHP で書かれた SVN クラスのチュートリアル

WBOY
WBOYオリジナル
2016-07-20 11:13:00771ブラウズ

<?<span php
</span><span /*</span><span *
 * SVN 外部命令 类
 *
 * @author rubekid
 *
 * @todo comment need addslashes for svn commit
 *
 </span><span */</span>

<span class</span><span  SvnUtils {

    </span><span /*</span><span *
     *
     * svn 账号
     </span><span */</span>
    <span const</span> SVN_USERNAME = "robot"<span ;

    </span><span /*</span><span *
     * svn 密码
     </span><span */</span>
    <span const</span> SVN_PASSWORD = "robot2013"<span ;

    </span><span /*</span><span *
     * 配置文件目录   (任意指定一个临时目录,解决svn: warning: Can't open file '/root/.subversion/servers': Permission denied)
     </span><span */</span>
    <span const</span> SVN_CONFIG_DIR = "/var/tmp/"<span ;


    </span><span /*</span><span *
     * svn list
     *
     * @param $repository string
     * @return boolean
     *
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> ls(<span $repository</span><span ) {
        </span><span $command</span> = "sudo svn ls " . <span $repository</span><span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $output</span> = <span implode</span> ( "<br />", <span $output</span><span  );
        </span><span if</span> (<span strpos</span> ( <span $output</span>, 'non-existent in that revision'<span  )) {
            </span><span return</span> <span false</span><span ;
        }
        </span><span return</span> "<br />" . <span $command</span> . "<br />" . <span $output</span><span ;
    }

    </span><span /*</span><span *
     * svn copy
     *
     * @param $src string
     * @param $dst string
     * @param $comment string
     * @return boolean
     *
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> <span copy</span>(<span $src</span>, <span $dst</span>, <span $comment</span><span ) {
        </span><span $command</span> = "sudo svn cp <span $src</span> <span $dst</span> -m '<span $comment</span>'"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $output</span> = <span implode</span> ( "<br />", <span $output</span><span  );
        </span><span if</span> (<span strpos</span> ( <span $output</span>, 'Committed revision'<span  )) {
            </span><span return</span> <span true</span><span ;
        }
        </span><span return</span> "<br />" . <span $command</span> . "<br />" . <span $output</span><span ;
    }

    </span><span /*</span><span *
     * svn delete
     *
     * @param $url string
     * @param $comment string
     * @return boolean
     *
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> delete(<span $url</span>, <span $comment</span><span ) {
        </span><span $command</span> = "sudo svn del <span $url</span> -m '<span $comment</span>'"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $output</span> = <span implode</span> ( '<br />', <span $output</span><span  );
        </span><span if</span> (<span strpos</span> ( <span $output</span>, 'Committed revision'<span  )) {
            </span><span return</span> <span true</span><span ;
        }
        </span><span return</span> "<br />" . <span $command</span> . "<br />" . <span $output</span><span ;
    }

    </span><span /*</span><span *
     * svn move
     *
     * @param $src string
     * @param $dst string
     * @param $comment string
     * @return boolean
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> move(<span $src</span>, <span $dst</span>, <span $comment</span><span ) {
        </span><span $command</span> = "sudo svn mv <span $src</span> <span $dst</span> -m '<span $comment</span>'"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $output</span> = <span implode</span> ( '<br />', <span $output</span><span  );
        </span><span if</span> (<span strpos</span> ( <span $output</span>, 'Committed revision'<span  )) {
            </span><span return</span> <span true</span><span ;
        }
        </span><span return</span> "<br />" . <span $command</span> . "<br />" . <span $output</span><span ;
    }

    </span><span /*</span><span *
     * svn mkdir
     *
     * @param $url string
     * @param $comment string
     * @return boolean
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> <span mkdir</span>(<span $url</span>, <span $comment</span><span ) {
        </span><span $command</span> = "sudo svn mkdir <span $url</span> -m '<span $comment</span>'"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $output</span> = <span implode</span> ( '<br />', <span $output</span><span  );
        </span><span if</span> (<span strpos</span> ( <span $output</span>, 'Committed revision'<span  )) {
            </span><span return</span> <span true</span><span ;
        }
        </span><span return</span> "<br />" . <span $command</span> . "<br />" . <span $output</span><span ;
    }

    </span><span /*</span><span *
     * svn diff
     * @param $pathA string
     * @param $pathB string
     * @return string
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> diff(<span $pathA</span>, <span $pathB</span><span ) {
        </span><span $output</span> = self::runCmd ( "sudo svn diff <span $pathA</span> <span $pathB</span>"<span  );
        </span><span return</span> <span implode</span> ( '<br />', <span $output</span><span  );
    }

    </span><span /*</span><span *
     * svn checkout
     * @param $url string
     * @param $dir string
     * @return boolean
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> checkout(<span $url</span>, <span $dir</span><span ) {
        </span><span $command</span> = "cd <span $dir</span> && sudo svn co <span $url</span>"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $output</span> = <span implode</span> ( '<br />', <span $output</span><span  );
        </span><span if</span> (<span strstr</span> ( <span $output</span>, 'Checked out revision'<span  )) {
            </span><span return</span> <span true</span><span ;
        }
        </span><span return</span> "<br />" . <span $command</span> . "<br />" . <span $output</span><span ;
    }

    </span><span /*</span><span *
     * svn update
     * @param $path string
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> update(<span $path</span><span ) {
        </span><span $command</span> = "cd <span $path</span> && sudo svn up"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $output</span> = <span implode</span> ( '<br />', <span $output</span><span  );
        </span><span preg_match_all</span> ( "/[0-9]+/", <span $output</span>, <span $ret</span><span  );
        </span><span if</span> (! <span $ret</span> [0] [0<span ]) {
            </span><span return</span> "<br />" . <span $command</span> . "<br />" . <span $output</span><span ;
        }
        </span><span return</span> <span $ret</span> [0] [0<span ];
    }

    </span><span /*</span><span *
     * svn merge
     *
     * @param $revision string
     * @param $url string
     * @param $dir string
     *
     * @return boolean
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> merge(<span $revision</span>, <span $url</span>, <span $dir</span><span ) {
        </span><span $command</span> = "cd <span $dir</span> && sudo svn merge -r1:<span $revision</span> <span $url</span>"<span ;
        </span><span $output</span> = <span implode</span> ( '<br />', self::runCmd ( <span $command</span><span  ) );
        </span><span if</span> (<span strstr</span> ( <span $output</span>, 'Text conflicts'<span  )) {
            </span><span return</span> 'Command: ' . <span $command</span> . '<br />' . <span $output</span><span ;
        }
        </span><span return</span> <span true</span><span ;
    }

    </span><span /*</span><span *
     * svn commit
     *
     * @param $dir string
     * @param $comment string
     *
     * @return boolean
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> commit(<span $dir</span>, <span $comment</span><span ) {
        </span><span $command</span> = "cd <span $dir</span> && sudo svn commit -m'<span $comment</span>'"<span ;
        </span><span $output</span> = <span implode</span> ( '<br />', self::runCmd ( <span $command</span><span  ) );
        </span><span if</span> (<span strpos</span> ( <span $output</span>, 'Committed revision' ) || <span empty</span> ( <span $output</span><span  )) {
            </span><span return</span> <span true</span><span ;
        }
        </span><span return</span> <span $output</span><span ;
    }

    </span><span /*</span><span *
     * svn status (输出WC中文件和目录的状态)
     *
     * @param $dir string
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> getStatus(<span $dir</span><span ) {
        </span><span $command</span> = "cd <span $dir</span> && sudo svn st"<span ;
        </span><span return</span> self::runCmd ( <span $command</span><span  );
    }

    </span><span /*</span><span *
     * svn 冲突
     *
     * @param $dir string
     * @return boolean
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> hasConflict(<span $dir</span><span ) {
        </span><span $output</span> = self::getStatus ( <span $dir</span><span  );
        </span><span foreach</span> ( <span $output</span> <span as</span> <span $line</span><span  ) {
            </span><span if</span> ( <span substr</span> ( <span trim</span> ( <span $line</span> ), 0, 1 ) == 'C' || (<span substr</span> ( <span trim</span> ( <span $line</span> ), 0, 1 ) == '!'<span )) {
                </span><span return</span> <span true</span><span ;
            }
        }
        </span><span return</span> <span false</span><span ;
    }

    </span><span /*</span><span *
     * svn log
     *
     * @param $path string
     * @return string
     *
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> getLog(<span $path</span><span ) {
        </span><span $command</span> = "sudo svn log <span $path</span> --xml"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span return</span> <span implode</span> ( '', <span $output</span><span  );
    }

    </span><span /*</span><span *
     * svn info
     * @param $path string
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> getPathRevision(<span $path</span><span ) {
        </span><span $command</span> = "sudo svn info <span $path</span> --xml"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $string</span> = <span implode</span> ( '', <span $output</span><span  );
        </span><span $xml</span> = <span new</span> SimpleXMLElement ( <span $string</span><span  );
        </span><span foreach</span> ( <span $xml</span>->entry [0]->attributes () <span as</span> <span $key</span> => <span $value</span><span  ) {
            </span><span if</span> ( <span $key</span> == 'revision'<span  ) {
                </span><span return</span> <span $value</span><span ;
            }
        }
    }

    </span><span /*</span><span *
     * 获取最新版本号
     * @param $path string
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> getHeadRevision(<span $path</span><span ) {
        </span><span $command</span> = "cd <span $path</span> && sudo svn up"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span $output</span> = <span implode</span> ( '<br />', <span $output</span><span  );
        </span><span preg_match_all</span> ( "/[0-9]+/", <span $output</span>, <span $ret</span><span  );
        </span><span if</span> (! <span $ret</span> [0] [0<span ]) {
            </span><span return</span> "<br />" . <span $command</span> . "<br />" . <span $output</span><span ;
        }
        </span><span return</span> <span $ret</span> [0] [0<span ];
    }

    </span><span /*</span><span *
     * 获取某文件最早版本号
     *
     * @param $filePath string
     *
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> getFileFirstVersion(<span $filePath</span><span ){
        </span><span $command</span> = "sudo svn log {<span $filePath</span>}"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span> , "|grep -i ^r[0-9]* |awk  '{print $1}'"<span );
        </span><span if</span>(<span empty</span>(<span $output</span><span )){
            </span><span return</span> <span false</span><span ;
        }
        </span><span return</span> <span str_replace</span>("r", '', <span $output</span>[<span count</span>(<span $output</span>)-1<span ]);
    }

    </span><span /*</span><span *
     * 获取两个版本间修改的文件信息列表
     *
     * @param $fromVersion int
     * @param $headRevision int
     * @param $$path string
     *
     * @return array
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> getChangedFiles(<span $path</span>, <span $fromVersion</span>, <span $headRevision</span><span  ){
        </span><span $files</span> = <span array</span><span ();
        </span><span $pipe</span> = "|grep -i ^Index:|awk -F : '{print $2}'"<span ;
        </span><span $command</span> = "svn diff -r {<span $fromVersion</span>}:{<span $headRevision</span>} <span $path</span>"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span> ,<span $pipe</span><span );
        </span><span $files</span> = <span array_merge</span>(<span $files</span>, <span $output</span><span );
        </span><span $command</span> = "svn diff -r {<span $headRevision</span>}:{<span $fromVersion</span>} <span $path</span>"; <span //</span><span 文件删除可用逆向对比</span>
        <span $output</span> = self::runCmd ( <span $command</span> ,<span $pipe</span><span );
        </span><span $files</span> = <span array_merge</span>(<span $files</span>, <span $output</span><span );
        </span><span return</span> <span array_unique</span>(<span $files</span><span );
    }

    </span><span /*</span><span *
     * 获取两个版本间某文件修改 的内容
     *
     * @param $filePath string
     * @param $fromVersion int
     * @param $headRevision int
     *
     * @return array
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> getChangedInfo( <span $filePath</span>, <span $fromVersion</span>, <span $headRevision</span><span  ){
        </span><span $command</span> = "sudo svn diff -r {<span $fromVersion</span>}:{<span $headRevision</span>} <span $filePath</span>"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span return</span> <span $output</span><span ;
    }

    </span><span /*</span><span *
     * 查看文件内容
     *
     * @param $filePath string
     * @param $version int
     *
     * @return array
     </span><span */</span>
    <span public</span> <span static</span> <span function</span> getFileContent(<span $filePath</span>, <span $version</span><span ){
        </span><span $command</span> = "sudo svn cat -r {<span $version</span>} <span $filePath</span>"<span ;
        </span><span $output</span> = self::runCmd ( <span $command</span><span  );
        </span><span return</span> <span $output</span><span ;
    }

    </span><span /*</span><span *
     * Run a cmd and return result
     * @param $command string
     * @param $pipe string (可以增加管道对返回数据进行预筛选)
     * @return array
     </span><span */</span>
    <span protected</span> <span static</span> <span function</span> runCmd(<span $command</span> , <span $pipe</span> =""<span ) {
        </span><span $authCommand</span> = ' --username ' . self::SVN_USERNAME . ' --password ' . self::SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir ' . self::SVN_CONFIG_DIR . '.subversion'<span ;
        </span><span exec</span> ( <span $command</span> . <span $authCommand</span> . " 2>&1" . <span $pipe</span>, <span $output</span><span  );
        </span><span return</span> <span $output</span><span ;
    }
}</span>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/440398.htmlTechArticle? SVN_USERNAME = "robot" SVN_PASSWORD = "robot2013" SVN_CONFIG_DIR = "/var/tmp/" ls( = "sudo svn ls " . = self::runCmd ( = ( "br /", ( ( , 'non-existent in that revision' "br /" ....
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。