灵魂腐蚀 发表于 2015-2-16 00:22:37

PHP编程:php操作SVN类

熟悉了PHP和MYSQL开发的要领之后,再回头看你写的那个留言本,你也许会怀疑那真的是你写的吗?当然,如果屋里还有鬼的话,也许是它写的-_-    利用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("/+/", $output, $ret);
if (!$ret){
return "<br>" . $command . "<br>" . $output;
}
return $ret;
}
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->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("/+/", $output, $ret);
if (!$ret){
return "<br>" . $command . "<br>" . $output;
}
return $ret;
}
/**
* 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;
}
}
学习数据库了,MYSQL可算是PHP的黄金搭档了,不过,虽然话是这么说,你也可能恨不得把MYSQL给生吞活剥了,因为这一行一列的东东简直让自己头晕目眩。

再现理想 发表于 2015-2-16 00:31:28

Ps:以上纯属原创,如有雷同,纯属巧合

莫相离 发表于 2015-2-25 22:45:27

当留言板完成的时候,下步可以把做1个单人的blog程序,做为目标,

分手快乐 发表于 2015-3-8 08:46:15

对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。

活着的死人 发表于 2015-3-16 21:29:52

首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。

第二个灵魂 发表于 2015-3-23 02:41:09

我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。

飘灵儿 发表于 2015-3-27 01:08:09

说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年具体的记不清啦,囧。

只想知道 发表于 2015-3-30 23:43:14

再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。

小妖女 发表于 2015-4-3 23:24:17

实践是检验自己会不会的真理。

精灵巫婆 发表于 2015-4-9 17:34:53

建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。

蒙在股里 发表于 2015-5-1 02:13:36

说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。

若相依 发表于 2015-5-1 04:11:35

Ps:以上纯属原创,如有雷同,纯属巧合

金色的骷髅 发表于 2015-5-1 09:11:11

我要在声明一下:我是个菜鸟!!我对php这门优秀的语言也是知之甚少。但是我要在这里说一下php在网站开发中最常用的几个功能:

飘飘悠悠 发表于 2015-6-14 22:50:31

本文当是我的笔记啦,遇到的问题随时填充

愤怒的大鸟 发表于 2015-6-28 10:12:06

对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。

老尸 发表于 2015-6-28 21:25:37

先学习php和mysql,还有css(html语言很简单)我认为现在的效果比以前的方法好。

透明 发表于 2015-7-11 05:52:05

实践是检验自己会不会的真理。

灵魂腐蚀 发表于 2015-7-18 20:30:12

建数据库表的时候,int型要输入长度的,其实是个摆设的输入几位都没影响的,只要大于4就行,囧。

若天明 发表于 2015-7-18 23:11:09

使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的

admin 发表于 2015-7-25 15:11:55

曾经犯过一个很低级的错误,我在文件命名的时候用了一个横线\\\\\\\'-\\\\\\\' 号,结果找了好几个小时的错误,事实是命名的时候 是不能用横线 \\\\\\\'-\\\\\\\' 的,应该用的是下划线\\\\\\\'_\\\\\\\' ;
页: [1]
查看完整版本: PHP编程:php操作SVN类