In short:
[viewsvn.git] / includes / svnlib.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Command line interface with the SVN commands
24 *
25 * @package ViewSVN
26 */
27
28 /**
29 * Interacts with the command line subsystem to
30 * access SVN information
31 *
32 * @package ViewSVN
33 * @version $Id$
34 */
35 class SVNLib
36 {
37 /**
38 * Path to the SVN binary
39 * @var string
40 */
41 var $svnpath;
42
43 /**
44 * Controller
45 * @var object
46 * @access private
47 */
48 var $controller;
49
50 /**
51 * Constructor: validate SVN path
52 *
53 * @param object Controller
54 */
55 function SVNLib(&$controller)
56 {
57 $this->controller =& $controller;
58
59 $this->svnpath =& $this->controller->xquery->cmd($this->controller->registry->svnpath);
60
61 $access = $this->controller->xquery->exec($this->svnpath . ' --version');
62
63 if (!$access)
64 {
65 $this->controller->registry->trigger->error($this->controller->registry->lang->string('The SVN binary could not be located'));
66 }
67
68 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
69 {
70 $this->controller->registry->trigger->error($this->controller->registry->lang->string('The SVN binary does not appear to be valid (it failed our tests)'));
71 }
72 }
73
74 /**
75 * Executes the SVN binary
76 *
77 * @access private
78 *
79 * @param string Command
80 *
81 * @return array Output
82 */
83 function svn($command)
84 {
85 $output = $this->controller->xquery->exec($this->svnpath . ' ' . $command . ' 2>&1');
86
87 // make sure that we keep escaped chars
88 //$output = str_replace(array('\t', '\n', '\r'), array('\\\t', '\\\n', '\\\r'), $output);
89 //$output = preg_replace('#\\\(.)#', '\\\\\\\\' . '\1', $output);
90 //$output = str_replace('\\', '\\\\', $output);
91
92 $temp = implode("\n", $output);
93 if (strpos($temp, '(apr' . '_err=') !== false)
94 {
95 $this->controller->registry->trigger->error(nl2br($temp));
96 }
97 return $output;
98 }
99
100 /**
101 * SVN Wrapper: standard command system
102 *
103 * @access private
104 *
105 * @param string SVN command
106 * @param string Repository
107 * @param string Path
108 * @param integer Revision
109 *
110 * @return array Lines of output
111 */
112 function std($command, $repos, $path, $revision)
113 {
114 global $viewsvn;
115
116 $revision = SVNCommon::rev($revision);
117 $repospath = $viewsvn->repos->fetch_path($repos, false);
118
119 return $this->svn($command . ' ' . $repospath . $path . '@' . $revision);
120 }
121
122 /**
123 * SVN Wrapper: blame
124 *
125 * @access protected
126 *
127 * @param string Repository
128 * @param string Path
129 * @param integer Revision
130 *
131 * @return array Lines of blame output
132 */
133 function blame($repos, $path, $revision)
134 {
135 return $this->std('blame', $repos, $path, $revision);
136 }
137
138 /**
139 * SVN Wrapper: cat
140 *
141 * @access protected
142 *
143 * @param string Repository
144 * @param string Path
145 * @param integer Revision
146 *
147 * @return array Lines of cat output
148 */
149 function cat($repos, $path, $revision)
150 {
151 return $this->std('cat', $repos, $path, $revision);
152 }
153
154 /**
155 * SVN Wrapper: diff
156 *
157 * @access protected
158 *
159 * @param string Repository
160 * @param string Path
161 * @param integer Lower revision
162 * @param integer Higher revision
163 *
164 * @return array Lines of diff output
165 */
166 function diff($repos, $path, $lorev, $hirev)
167 {
168 global $viewsvn;
169
170 $hirev = $this->rev($hirev);
171 $lorev = $this->rev($lorev);
172 if ($lorev == 'HEAD')
173 {
174 $lorev = 1;
175 }
176
177 if (is_integer($hirev) AND is_integer($lorev))
178 {
179 if ($lorev > $hirev)
180 {
181 $lorev = $hirev - 1;
182 }
183 if ($lorev == $hirev)
184 {
185 $lorev = 0;
186 }
187 }
188
189 $repospath = $viewsvn->repos->fetch_path($repos, false);
190
191 return $this->svn('diff -r' . $lorev . ':' . $hirev . ' ' . $repospath . $path);
192 }
193
194 /**
195 * SVN Wrapper: log
196 *
197 * @access protected
198 *
199 * @param string Repository
200 * @param string Path
201 * @param integer Lower revision
202 * @param integer Higher revision
203 *
204 * @return array Lines of log output
205 */
206 function log($repos, $path, $lorev, $hirev)
207 {
208 global $viewsvn;
209
210 $hirev = $this->rev($hirev);
211 $lorev = $this->rev($hirev);
212 if ($lorev == 'HEAD')
213 {
214 $lorev = 0;
215 }
216
217 if (is_integer($hirev) AND is_integer($lorev))
218 {
219 if ($lorev > $hirev)
220 {
221 $lorev = $hirev - 1;
222 }
223 if ($lorev == $hirev)
224 {
225 $lorev = 0;
226 }
227 }
228
229 $repospath = $viewsvn->repos->fetch_path($repos, false);
230
231 return $this->svn('log -v -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
232 }
233
234 /**
235 * SVN Wrapper: ls (list)
236 *
237 * @access protected
238 *
239 * @param string Repository
240 * @param string Path
241 * @param integer Revision
242 *
243 * @return array Lines of list output
244 */
245 function ls($repos, $path, $revision)
246 {
247 return $this->std('list', $repos, $path, $revision);
248 }
249 }
250
251 /*=====================================================================*\
252 || ###################################################################
253 || # $HeadURL$
254 || # $Id$
255 || ###################################################################
256 \*=====================================================================*/
257 ?>