本文提供一個功能完整的 PHP SVN 封裝類 SvnUtils,封裝常用 svn 命令(ls、copy、delete、move、mkdir、diff、checkout、update、merge、commit、status、log、info 等),支援認證、配置目錄自定義、管道過濾輸出,適合自動化部署、持續集成場景。
runCmd(),支援管道預篩選true,失敗返回帶命令與輸出的錯誤字符串| 方法 | 功能 | 關鍵參數 |
|---|---|---|
| ls($repo) | 列出版本庫內容 | $repository |
| copy($src, $dst, $comment) | 複製(分支/標籤) | 源路徑、目標路御、註釋 |
| delete($url, $comment) | 刪除路徑 | $url, $comment |
| move($src, $dst, $comment) | 移動/重命名 | $src, $dst, $comment |
| mkdir($url, $comment) | 創建目錄 | $url, $comment |
| diff($pathA, $pathB) | 差異對比 | 兩路徑 |
| checkout($url, $dir) | 棧出 | 倉庫 URL、本地目錄 |
| update($path) | 更新工作副本 | $path,返回版本號 |
| merge($rev, $url, $dir) | 合併變更 | 版本範圍、URL、目錄 |
| commit($dir, $comment) | 提交變更 | 工作副本目錄、註釋 |
| getStatus($dir) | 獲取狀態列表 | $dir |
| hasConflict($dir) | 檢測衝突 | $dir,返回 bool |
| getLog($path) | 獲取日誌(XML) | $path |
| getPathRevision($path) | 獲取路徑版本號 | $path,解析 XML |
| getHeadRevision($path) | 獲取最新版本號 | $path,執行 update |
| getFileFirstVersion($file) | 獲取文件首個版本 | $filePath,管道過濾 |
| getChangedFiles($path, $from, $head) | 版本間變更文件列表 | 路徑、起止版本 |
| getChangedInfo($file, $from, $head) | 文件具體變更內容 | 文件路徑、版本範圍 |
| getFileContent($file, $version) | 獲取指定版本文件內容 | 文件路徑、版本號 |
const SVN_USERNAME = "robot"; const SVN_PASSWORD = "robot2013"; const SVN_CONFIG_DIR = "/var/tmp/"; // 避免 /root/.subversion 權限問題
protected static function runCmd($command, $pipe = "") {
$auth = ' --username ' . self::SVN_USERNAME
. ' --password ' . self::SVN_PASSWORD
. ' --no-auth-cache --non-interactive'
. ' --config-dir ' . self::SVN_CONFIG_DIR . '.subversion';
exec($command . $auth . " 2>&1" . $pipe, $output);
return $output;
}
2>&1 合併 stderr 到 stdout,確保錯誤信息可捕獲$pipe 支援如 |grep ... 進一步過濾<?php
require_once 'SvnUtils.php';
// 檢出項目
SvnUtils::checkout('svn://repo/project/trunk', '/var/www/project');
// 更新並獲取版本號
$rev = SvnUtils::update('/var/www/project');
// 提交變更
SvnUtils::commit('/var/www/project', 'feat: 新增用戶模塊');
// 獲取兩版本間變更文件
$files = SvnUtils::getChangedFiles('svn://repo/project', 100, 200);
svn 客戶端且 PHP exec() 未被禁用sudo 提權(類中已內置 sudo)sudo 需適配<?php
class SvnUtils {
const SVN_USERNAME = "robot";
const SVN_PASSWORD = "robot2013";
const SVN_CONFIG_DIR = "/var/tmp/";
public static function ls($repository) { /* ... */ }
public static function copy($src, $dst, $comment) { /* ... */ }
public static function delete($url, $comment) { /* ... */ }
public static function move($src, $dst, $comment) { /* ... */ }
public static function mkdir($url, $comment) { /* ... */ }
public static function diff($pathA, $pathB) { /* ... */ }
public static function checkout($url, $dir) { /* ... */ }
public static function update($path) { /* ... */ }
public static function merge($revision, $url, $dir) { /* ... */ }
public static function commit($dir, $comment) { /* ... */ }
public static function getStatus($dir) { /* ... */ }
public static function hasConflict($dir) { /* ... */ }
public static function getLog($path) { /* ... */ }
public static function getPathRevision($path) { /* ... */ }
public static function getHeadRevision($path) { /* ... */ }
public static function getFileFirstVersion($filePath) { /* ... */ }
public static function getChangedFiles($path, $fromVersion, $headRevision) { /* ... */ }
public static function getChangedInfo($filePath, $fromVersion, $headRevision) { /* ... */ }
public static function getFileContent($filePath, $version) { /* ... */ }
protected static function runCmd($command, $pipe = "") { /* ... */ }
}
此類已在生產環境驗證,可直接集成至部署腳本或 CI/CD 流水線,顯著簡化 SVN 自動化操作代碼。