当前位置: 首页 > 编程文章 >

PHP SVN封装类实现代码管理功能

PHP SVN封装类实现代码管理功能

作者:苏景时间:2025-12-22 11:57:02浏览:139

该PHP类通过执行系统Shell命令实现对Subversion版本控制系统的全方位操作。核心方法涵盖列表查看、文件复制删除移动、目录创建、差异对比、检出更新合并及提交等日常维护任务。此外,还提供获取日志、信息、版本号及冲突检测等功能,支持通过管道符预处理输出数据,适用于自动化部署与代码管理场景。

本文目录展开

PHP SVN封装类实现代码管理功能

复制代码 代码如下:

/** * svn list * * @param $repository string * @return boolean * */ public static function ls($repository) { $command = "sudo svn ls " . $repository; $output = self::runCmd ( $command ); $output = implode ( "\n", $output ); if (strpos ( $output, 'non-existent in that revision' )) { return false; } return "\n" . $command . "\n" . $output; } /** * svn copy * * @param $src string * @param $dst string * @param $comment string * @return boolean * */ public static function copy($src, $dst, $comment) { $command = "sudo svn cp $src $dst -m '$comment'"; $output = self::runCmd ( $command ); $output = implode ( "\n", $output ); if (strpos ( $output, 'Committed revision' )) { return true; } return "\n" . $command . "\n" . $output; } /** * svn delete * * @param $url string * @param $comment string * @return boolean */ public static function delete($url, $comment) { $command = "sudo svn del $url -m '$comment'"; $output = self::runCmd ( $command ); $output = implode ( '\n', $output ); if (strpos ( $output, 'Committed revision' )) { return true; } return "\n" . $command . "\n" . $output; } /** * svn move * * @param $src string * @param $dst string * @param $comment string * @return boolean */ public static function move($src, $dst, $comment) { $command = "sudo svn mv $src $dst -m '$comment'"; $output = self::runCmd ( $command ); $output = implode ( '\n', $output ); if (strpos ( $output, 'Committed revision' )) { return true; } return "\n" . $command . "\n" . $output; } /** * svn mkdir * * @param $url string * @param $comment string * @return boolean */ public static function mkdir($url, $comment) { $command = "sudo svn mkdir $url -m '$comment'"; $output = self::runCmd ( $command ); $output = implode ( '\n', $output ); if (strpos ( $output, 'Committed revision' )) { return true; } return "\n" . $command . "\n" . $output; } /** * svn diff * @param $pathA string * @param $pathB string * @return string */ public static function diff($pathA, $pathB) { $output = self::runCmd ( "sudo svn diff $pathA $pathB" ); return implode ( '\n', $output ); } /** * svn checkout * @param $url string * @param $dir string * @return boolean */ public static function checkout($url, $dir) { $command = "cd $dir && sudo svn co $url"; $output = self::runCmd ( $command ); $output = implode ( '\n', $output ); if (strstr ( $output, 'Checked out revision' )) { return true; } return "\n" . $command . "\n" . $output; } /** * svn update * @param $path string */ public static function update($path) { $command = "cd $path && sudo svn up"; $output = self::runCmd ( $command ); $output = implode ( '\n', $output ); preg_match_all ( "/[0-9]+/", $output, $ret ); if (! $ret [0] [0]) { return "\n" . $command . "\n" . $output; } return $ret [0] [0]; } /** * svn merge * * @param $revision string * @param $url string * @param $dir string * * @return boolean */ public static function merge($revision, $url, $dir) { $command = "cd $dir && sudo svn merge -r1:$revision $url"; $output = implode ( '\n', self::runCmd ( $command ) ); if (strstr ( $output, 'Text conflicts' )) { return 'Command: ' . $command . '\n' . $output; } return true; } /** * svn commit * * @param $dir string * @param $comment string * * @return boolean */ public static function commit($dir, $comment) { $command = "cd $dir && sudo svn commit -m'$comment'"; $output = implode ( '\n', self::runCmd ( $command ) ); if (strpos ( $output, 'Committed revision' ) || empty ( $output )) { return true; } return $output; } /** * svn status (输出WC中文件和目录的状态) * * @param $dir string */ public static function getStatus($dir) { $command = "cd $dir && sudo svn st"; return self::runCmd ( $command ); } /** * svn 冲突 * * @param $dir string * @return boolean */ public static function hasConflict($dir) { $output = self::getStatus ( $dir ); foreach ( $output as $line ) { if ( substr ( trim ( $line ), 0, 1 ) == 'C' || (substr ( trim ( $line ), 0, 1 ) == '!')) { return true; } } return false; } /** * svn log * * @param $path string * @return string * */ public static function getLog($path) { $command = "sudo svn log $path --xml"; $output = self::runCmd ( $command ); return implode ( '', $output ); } /** * svn info * @param $path string */ public static function getPathRevision($path) { $command = "sudo svn info $path --xml"; $output = self::runCmd ( $command ); $string = implode ( '', $output ); $xml = new SimpleXMLElement ( $string ); foreach ( $xml->entry [0]->attributes () as $key => $value ) { if ( $key == 'revision' ) { return $value; } } } /** * 获取最新版本号 * @param $path string */ public static function getHeadRevision($path) { $command = "cd $path && sudo svn up"; $output = self::runCmd ( $command ); $output = implode ( '\n', $output ); preg_match_all ( "/[0-9]+/", $output, $ret ); if (! $ret [0] [0]) { return "\n" . $command . "\n" . $output; } return $ret [0] [0]; } /** * 获取某文件最早版本号 * * @param $filePath string * */ public static function getFileFirstVersion($filePath){ $command = "sudo svn log {$filePath}"; $output = self::runCmd ( $command , "|grep -i ^r[0-9]* |awk '{print $1}'"); if(empty($output)){ return false; } return str_replace("r", '', $output[count($output)-1]); } /** * 获取两个版本间修改的文件信息列表 * * @param $fromVersion int * @param $headRevision int * @param $$path string * * @return array */ public static function getChangedFiles($path, $fromVersion, $headRevision ){ $files = array(); $pipe = "|grep -i ^Index:|awk -F : '{print $2}'"; $command = "svn diff -r {$fromVersion}:{$headRevision} $path"; $output = self::runCmd ( $command ,$pipe); $files = array_merge($files, $output); $command = "svn diff -r {$headRevision}:{$fromVersion} $path"; //文件删除可用逆向对比 $output = self::runCmd ( $command ,$pipe); $files = array_merge($files, $output); return array_unique($files); } /** * 获取两个版本间某文件修改 的内容 * * @param $filePath string * @param $fromVersion int * @param $headRevision int * * @return array */ public static function getChangedInfo( $filePath, $fromVersion, $headRevision ){ $command = "sudo svn diff -r {$fromVersion}:{$headRevision} $filePath"; $output = self::runCmd ( $command ); return $output; } /** * 查看文件内容 * * @param $filePath string * @param $version int * * @return array */ public static function getFileContent($filePath, $version){ $command = "sudo svn cat -r {$version} $filePath"; $output = self::runCmd ( $command ); return $output; } /** * Run a cmd and return result * @param $command string * @param $pipe string (可以增加管道对返回数据进行预筛选) * @return array */ protected static function runCmd($command , $pipe ="") { $authCommand = ' --username ' . self::SVN_USERNAME . ' --password ' . self::SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir ' . self::SVN_CONFIG_DIR . '.subversion'; exec ( $command . $authCommand . " 2>&1" . $pipe, $output ); return $output; } }

上一篇
PHP字符串截取函数用法分析

PHP内置的字符串截取函数在处理英文和数字时表现良好,但在面对中英文混排场景时往往无法正确识别边界,容易导致乱码或截断错误。本文通过具体代码示例,分析了这一局限性,并推荐了一种更适用于复杂字符处理的截取方案,帮助开发者避免常见陷阱。

下一篇
PHP实现最大公约数与最小公倍数计算

本文介绍了在PHP中通过自定义函数计算两个整数最大公约数与最小公倍数的方法。提供了基于循环遍历的求解逻辑,详细展示了如何区分echo与return的使用场景,并附带了在线计算工具推荐及注意事项。

推荐文章

更多
PHP时间戳与日期字符串相互转换方法详解

本文介绍PHP中时间数值(Unix时间戳)与日期字符串之间的相互转换方法。通过strtotime和date函数,实现将当前时间、指定日期转换为时间戳,以及将时间戳格式化为可读的日期时间字符串,并提供相关在线计算工具推荐。

2025.11.30 1,033
PHP日期转时间戳时区偏差怎么解决

在 PHP 中将日期字符串转换为时间戳时,若未显式指定时区,默认使用脚本配置的 date.timezone 或系统时区。当服务器时区与业务逻辑预期(如 UTC )不一致时,会产生偏差。解决核心是确保输入字符串携带明确时区信息,或在转换前统一设置时区上下文。

2026.10.24 1,031
PHP跨域配置:怎么安全地允许指定域名而非使用通配符

在 PHP 中安全地允许指定域名跨域,核心是解析请求头中的 Origin ,验证其是否在白名单内,然后动态设置 Access-Control-Allow-Origin 响应头。不要使用通配符 * ,也不要硬编码单一域名。

2025.03.29 1,018
JavaScript中利用replace结合正则实现replaceAll效果

JavaScript原生仅支持replace()进行单次或全局替换,缺乏类似Java的replaceAll()便捷方法。本文总结四种在JS中实现全量替换的方案:使用带g标志的正则、带gm标志的正则、扩展String原型以及split后join。通过对比代码示例,展示各方法的实现细节与适用场景,帮助开发者根据需求选择最优解。

2025.04.29 1,016
OpenAI的Jalapeno芯片未落地,教你分辨300兆瓦算力消息

OpenAI计划在SB Energy租赁的数据中心部署约300兆瓦自研Jalapeno推理芯片,但目前双方仍处于洽谈阶段,交易细节与上线时间表均未公布。本文带你按报道原文核实项目所处阶段,厘清芯片发布、电力功率与实际推理算力的边界,并将融资意向与签约、开工、投运节点分开追踪,避免误将规划容量计入现有算力。

2026.12.02 1,012
PHP实现基础文件上传:HTML表单与$_FILES处理流程

在 HTML 表单中设置 enctype="multipart/form-data" ,并在 PHP 后端通过 $_FILES 数组接收文件,调用 move_uploaded_file() 将临时文件移动到目标目录即可完成上传。

2026.09.07 1,012

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。