没想到啊

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

http://www.oschina.net/code/snippet_162844_7312

 

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

  1 <?php
2 /**
3 *
4 * This class for execute the external program of svn
5 *
6 * @auth Seven Yang <qineer@gmail.com>
7 *
8 */
9 class SvnPeer
10 {
11
12 /**
13 * List directory entries in the repository
14 *
15 * @param string a specific project repository path
16 * @return bool true, if validated successfully, otherwise false
17 */
18 static public function ls($repository)
19 {
20 $command = "svn ls " . $repository;
21 $output = SvnPeer::runCmd($command);
22 $output = implode("<br>", $output);
23 if (strpos($output, 'non-existent in that revision')) {
24 return false;
25 }
26
27 return "<br>" . $command . "<br>" . $output;
28 }
29
30 /**
31 * Duplicate something in working copy or repository, remembering history
32 *
33 * @param $src
34 * @param $dst
35 * @param $comment string specify log message
36 * @return bool true, if copy successfully, otherwise return the error message
37 *
38 * @todo comment need addslashes for svn commit
39 */
40 static public function copy($src, $dst, $comment)
41 {
42 $command = "svn cp $src $dst -m '$comment'";
43 $output = SvnPeer::runCmd($command);
44 $output = implode("<br>", $output);
45
46 if (strpos($output, 'Committed revision')) {
47 return true;
48 }
49
50 return "<br>" . $command . "<br>" . $output;
51 }
52
53 /**
54 * Remove files and directories from version control
55 *
56 * @param $url
57 * @return bool true, if delete successfully, otherwise return the error message
58 *
59 * @todo comment need addslashes for svn commit
60 */
61 static public function delete($url, $comment)
62 {
63 $command = "svn del $url -m '$comment'";
64 $output = SvnPeer::runCmd($command);
65 $output = implode('<br>', $output);
66 if (strpos($output, 'Committed revision')) {
67 return true;
68 }
69
70 return "<br>" . $command . "<br>" . $output;
71 }
72
73 /**
74 * Move and/or rename something in working copy or repository
75 *
76 * @param $src string trunk path
77 * @param $dst string new branch path
78 * @param $comment string specify log message
79 * @return bool true, if move successfully, otherwise return the error message
80 *
81 * @todo comment need addslashes for svn commit
82 */
83 static public function move($src, $dst, $comment)
84 {
85 $command = "svn mv $src $dst -m '$comment'";
86 $output = SvnPeer::runCmd($command);
87 $output = implode('<br>', $output);
88
89 if (strpos($output, 'Committed revision')) {
90 return true;
91 }
92
93 return "<br>" . $command . "<br>" . $output;
94 }
95
96 /**
97 * Create a new directory under version control
98 *
99 * @param $url string
100 * @param $comment string the svn message
101 * @return bool true, if create successfully, otherwise return the error message
102 *
103 * @todo comment need addslashes for svn commit
104 */
105 static public function mkdir($url, $comment)
106 {
107 $command = "svn mkdir $url -m '$comment'";
108 $output = SvnPeer::runCmd($command);
109 $output = implode('<br>', $output);
110
111 if (strpos($output, 'Committed revision')) {
112 return true;
113 }
114
115 return "<br>" . $command . "<br>" . $output;
116 }
117
118 static public function diff($pathA, $pathB)
119 {
120 $output = SvnPeer::runCmd("svn diff $pathA $pathB");
121 return implode('<br>', $output);
122 }
123
124 static public function checkout($url, $dir)
125 {
126 $command = "cd $dir && svn co $url";
127 $output = SvnPeer::runCmd($command);
128 $output = implode('<br>', $output);
129 if (strstr($output, 'Checked out revision')) {
130 return true;
131 }
132
133 return "<br>" . $command . "<br>" . $output;
134 }
135
136
137 static public function update($path)
138 {
139 $command = "cd $path && svn up";
140 $output = SvnPeer::runCmd($command);
141 $output = implode('<br>', $output);
142
143 preg_match_all("/[0-9]+/", $output, $ret);
144 if (!$ret[0][0]){
145 return "<br>" . $command . "<br>" . $output;
146 }
147
148 return $ret[0][0];
149 }
150
151 static public function merge($revision, $url, $dir)
152 {
153 $command = "cd $dir && svn merge -r1:$revision $url";
154 $output = implode('<br>', SvnPeer::runCmd($command));
155 if (strstr($output, 'Text conflicts')) {
156 return 'Command: ' . $command .'<br>'. $output;
157 }
158
159 return true;
160 }
161
162 static public function commit($dir, $comment)
163 {
164 $command = "cd $dir && svn commit -m'$comment'";
165 $output = implode('<br>', SvnPeer::runCmd($command));
166
167 if (strpos($output, 'Committed revision') || empty($output)) {
168 return true;
169 }
170
171 return $output;
172 }
173
174 static public function getStatus($dir)
175 {
176 $command = "cd $dir && svn st";
177 return SvnPeer::runCmd($command);
178 }
179
180 static public function hasConflict($dir)
181 {
182 $output = SvnPeer::getStatus($dir);
183 foreach ($output as $line){
184 if ('C' == substr(trim($line), 0, 1) || ('!' == substr(trim($line), 0, 1))){
185 return true;
186 }
187 }
188
189 return false;
190 }
191
192 /**
193 * Show the log messages for a set of path with XML
194 *
195 * @param path string
196 * @return log message string
197 */
198 static public function getLog($path)
199 {
200 $command = "svn log $path --xml";
201 $output = SvnPeer::runCmd($command);
202 return implode('', $output);
203 }
204
205 static public function getPathRevision($path)
206 {
207 $command = "svn info $path --xml";
208 $output = SvnPeer::runCmd($command);
209 $string = implode('', $output);
210 $xml = new SimpleXMLElement($string);
211 foreach ($xml->entry[0]->attributes() as $key=>$value){
212 if ('revision' == $key) {
213 return $value;
214 }
215 }
216 }
217
218 static public function getHeadRevision($path)
219 {
220 $command = "cd $path && svn up";
221 $output = SvnPeer::runCmd($command);
222 $output = implode('<br>', $output);
223
224 preg_match_all("/[0-9]+/", $output, $ret);
225 if (!$ret[0][0]){
226 return "<br>" . $command . "<br>" . $output;
227 }
228
229 return $ret[0][0];
230 }
231
232 /**
233 * Run a cmd and return result
234 *
235 * @param string command line
236 * @param boolen true need add the svn authentication
237 * @return array the contents of the output that svn execute
238 */
239 static protected function runCmd($command)
240 {
241 $authCommand = ' --username ' . SVN_USERNAME . ' --password ' . SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir '.SVN_CONFIG_DIR.'.subversion';
242 exec($command . $authCommand . " 2>&1", $output);
243
244 return $output;
245 }
246 }



posted on 2011-11-24 19:05  没想到啊  阅读(543)  评论(0)    收藏  举报