php操作SVN类

使用PHP完成SVN的操作,包括复制,查看列表,删除,移动,创建目录,查看diff,更新,合并,提交,获取状态,获取commit log,获取当前版本号操作。在svn 1.6.11版本中测试通过。

 

<?php
/**
*
* This class for execute the external program of svn
*
* @auth Seven Yang <qineer@gmail.com>
*
*/
class SvnPeer
{

/**
* List directory entries in the repository
*
* @param string a specific project repository path
* @return bool true, if validated successfully, otherwise false
*/
static public function ls($repository)
{
$command = "svn ls " . $repository;
$output = SvnPeer::runCmd($command);
$output = implode("<br>", $output);
if (strpos($output, 'non-existent in that revision')) {
return false;
}

return "<br>" . $command . "<br>" . $output;
}

/**
* Duplicate something in working copy or repository, remembering history
*
* @param $src
* @param $dst
* @param $comment string specify log message
* @return bool true, if copy successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function copy($src, $dst, $comment)
{
$command = "svn cp $src $dst -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode("<br>", $output);

if (strpos($output, 'Committed revision')) {
return true;
}

return "<br>" . $command . "<br>" . $output;
}

/**
* Remove files and directories from version control
*
* @param $url
* @return bool true, if delete successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function delete($url, $comment)
{
$command = "svn del $url -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strpos($output, 'Committed revision')) {
return true;
}

return "<br>" . $command . "<br>" . $output;
}

/**
* Move and/or rename something in working copy or repository
*
* @param $src string trunk path
* @param $dst string new branch path
* @param $comment string specify log message
* @return bool true, if move successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function move($src, $dst, $comment)
{
$command = "svn mv $src $dst -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);

if (strpos($output, 'Committed revision')) {
return true;
}

return "<br>" . $command . "<br>" . $output;
}

/**
* Create a new directory under version control
*
* @param $url string
* @param $comment string the svn message
* @return bool true, if create successfully, otherwise return the error message
*
* @todo comment need addslashes for svn commit
*/
static public function mkdir($url, $comment)
{
$command = "svn mkdir $url -m '$comment'";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);

if (strpos($output, 'Committed revision')) {
return true;
}

return "<br>" . $command . "<br>" . $output;
}

static public function diff($pathA, $pathB)
{
$output = SvnPeer::runCmd("svn diff $pathA $pathB");
return implode('<br>', $output);
}

static public function checkout($url, $dir)
{
$command = "cd $dir && svn co $url";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);
if (strstr($output, 'Checked out revision')) {
return true;
}

return "<br>" . $command . "<br>" . $output;
}

static public function update($path)
{
$command = "cd $path && svn up";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);

preg_match_all("/[0-9]+/", $output, $ret);
if (!$ret[0][0]){
return "<br>" . $command . "<br>" . $output;
}

return $ret[0][0];
}

static public function merge($revision, $url, $dir)
{
$command = "cd $dir && svn merge -r1:$revision $url";
$output = implode('<br>', SvnPeer::runCmd($command));
if (strstr($output, 'Text conflicts')) {
return 'Command: ' . $command .'<br>'. $output;
}

return true;
}

static public function commit($dir, $comment)
{
$command = "cd $dir && svn commit -m'$comment'";
$output = implode('<br>', SvnPeer::runCmd($command));

if (strpos($output, 'Committed revision') empty($output)) {
return true;
}

return $output;
}

static public function getStatus($dir)
{
$command = "cd $dir && svn st";
return SvnPeer::runCmd($command);
}

static public function hasConflict($dir)
{
$output = SvnPeer::getStatus($dir);
foreach ($output as $line){
if ('C' == substr(trim($line), 0, 1) ('!' == substr(trim($line), 0, 1))){
return true;
}
}

return false;
}

/**
* Show the log messages for a set of path with XML
*
* @param path string
* @return log message string
*/
static public function getLog($path)
{
$command = "svn log $path --xml";
$output = SvnPeer::runCmd($command);
return implode('', $output);
}

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

static public function getHeadRevision($path)
{
$command = "cd $path && svn up";
$output = SvnPeer::runCmd($command);
$output = implode('<br>', $output);

preg_match_all("/[0-9]+/", $output, $ret);
if (!$ret[0][0]){
return "<br>" . $command . "<br>" . $output;
}

return $ret[0][0];
}

/**
* Run a cmd and return result
*
* @param string command line
* @param boolen true need add the svn authentication
* @return array the contents of the output that svn execute
*/
static protected function runCmd($command)
{
$authCommand = ' --username ' . SVN_USERNAME . ' --password ' . SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir '.SVN_CONFIG_DIR.'.subversion';
exec($command . $authCommand . " 2>&1", $output);

return $output;
}
}

时间: 2024-08-03 17:42:38

php操作SVN类的相关文章

ASP实例代码:asp操作Excel类

asp操作Excel类: <%'*******************************************************************'使用说明'Dim a'Set a=new CreateExcel'a.SavePath="x" '保存路径'a.SheetName="工作簿名称"       '多个工作表 a.SheetName=array("工作簿名称一","工作簿名称二")'a.Sh

PHP跳转函数和一个通用的操作提示类的编写

PHP 跳转,即重定向浏览器到指定的 URL,是一个很常见的功能.这种功能也有一些细节性的要求,比如等待多少秒以后跳转,用不用JavaScript实现跳转,等等.下面的跳转方法考虑到很多,并参数化,可以用到具体的项目当中. <?php /** * 重定向浏览器到指定的 URL * * @param string $url 要重定向的 url * @param int $delay 等待多少秒以后跳转 * @param bool $js 指示是否返回用于跳转的 JavaScript 代码 * @p

php操作MongoDB类实例

 本文实例讲述了php操作MongoDB类的方法.分享给大家供大家参考.具体如下: 1. MyMongo.php文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

使用PHP编写的SVN类

以下是使用PHP编写的一个SVN类.需要的朋友可以参考下   复制代码 代码如下: <?php /**  * SVN 外部命令 类  *  * @author rubekid  *  * @todo comment need addslashes for svn commit  *  */ class SvnUtils {     /**      *      * svn 账号      */     const SVN_USERNAME = "robot";     /**

php数据库操作model类(使用__call方法)_php技巧

本文实例讲述了php数据库操作model类.分享给大家供大家参考,具体如下: 该数据库操作类使用__call()方法实现了数据的查找功能. 代码如下: <?php /* 作者 : shyhero */ define("HOSTNAME","127.0.0.1"); define("USERNAME","root"); define("PASSWORD",""); define(&q

php实现的简单数据库操作Model类_php技巧

本文实例讲述了php实现的简单数据库操作Model类.分享给大家供大家参考,具体如下: 该数据库模型类可实现数据库的增删改查,简化数据库操作. 1. config.php代码: <?php define("HOSTNAME","127.0.0.1"); define("USERNAME","root"); define("PASSWORD",""); define("DA

C/S与B/S一些公共方法,如:操作word类可以封装成动态库吗

问题描述 C/S与B/S一些公共方法,如:操作word类可以封装成动态库吗 解决方案 解决方案二:应该可以,像操作数据库的类一样解决方案三:如果你是借助vba接口(也就是进程外启动一个word或者excel)操作的office文档,这种程序还是不太适合服务器端.解决方案四:可以啊..而且人家早就有封装完的啦比如myxls或者npoi什么的....aspose等等...解决方案五:引用3楼diaodiaop的回复: 可以啊..而且人家早就有封装完的啦比如myxls或者npoi什么的....aspo

PHP基于单例模式实现的数据库操作基类_php技巧

本文实例讲述了PHP基于单例模式实现的数据库操作基类.分享给大家供大家参考,具体如下: 配置文件: <?php $db = array( 'host'=>'localhost', 'user'=>'root', 'password'=>'', 'database'=>'test', ) ?> php 数据库基类: <?php class db { public $conn; public static $sql; public static $instance=n

Asp.Net+XML操作基类(修改,删除,新增,创建)第1/2页_实用技巧

/**********************************************************************************  *   * 功能说明:XML处理基类  * 作者: 刘功勋;  * 版本:V0.1(C#2.0);时间:2006-12-13  *   * *******************************************************************************/ using System;