Blame stuff is done
[viewsvn.git] / includes / svnlib.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # ViewSVN [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Command line interface with the SVN commands
15 *
16 * @package ViewSVN
17 */
18
19 /**
20 * Interacts with the command line subsystem to
21 * access SVN information
22 *
23 * @package ViewSVN
24 * @version $Id$
25 */
26 class SVNLib
27 {
28 /**
29 * Path to the SVN binary
30 * @var string
31 */
32 var $svnpath;
33
34 /**
35 * Constructor: validate SVN path
36 *
37 * @param string Path to SVN binary
38 */
39 function SVNLib($svnpath)
40 {
41 global $viewsvn;
42
43 $this->svnpath = $viewsvn->shell->cmd($svnpath);
44
45 $access = $viewsvn->shell->exec($this->svnpath . ' --version');
46
47 if (!$access)
48 {
49 $viewsvn->trigger->error('svn binary could not be found');
50 }
51
52 if (!preg_match('#^svn, version (.*?)\)$#i', trim($access[0])))
53 {
54 $viewsvn->trigger->error('svn binary does not pass test');
55 }
56 }
57
58 /**
59 * Executes the SVN binary
60 *
61 * @access private
62 *
63 * @param string Command
64 *
65 * @return array Output
66 */
67 function svn($command)
68 {
69 global $viewsvn;
70
71 return $viewsvn->shell->exec($this->svnpath . ' ' . $command . ' 2>&1');
72 }
73
74 /**
75 * SVN Wrapper: standard command system
76 *
77 * @access private
78 *
79 * @param string SVN command
80 * @param string Repository
81 * @param string Path
82 * @param integer Revision
83 *
84 * @return array Lines of output
85 */
86 function std($command, $repos, $path, $revision)
87 {
88 global $viewsvn;
89
90 $revision = $this->rev($revision);
91 $repospath = $viewsvn->repos->fetch_path($repos, false);
92
93 return $this->svn($command . ' -r' . $revision . ' ' . $repospath . $path);
94 }
95
96 /**
97 * SVN Wrapper: blame
98 *
99 * @access protected
100 *
101 * @param string Repository
102 * @param string Path
103 * @param integer Revision
104 *
105 * @return array Lines of blame output
106 */
107 function blame($repos, $path, $revision)
108 {
109 return $this->std('blame', $repos, $path, $revision);
110 }
111
112 /**
113 * SVN Wrapper: cat
114 *
115 * @access protected
116 *
117 * @param string Repository
118 * @param string Path
119 * @param integer Revision
120 *
121 * @return array Lines of cat output
122 */
123 function cat($repos, $path, $revision)
124 {
125 return $this->std('cat', $repos, $path, $revision);
126 }
127
128 /**
129 * SVN Wrapper: log
130 *
131 * @access protected
132 *
133 * @param string Repository
134 * @param string Path
135 * @param integer Lower revision
136 * @param integer Higher revision
137 *
138 * @return array Lines of log output
139 */
140 function log($repos, $path, $lorev, $hirev)
141 {
142 global $viewsvn;
143
144 $hirev = $this->rev($hirev);
145 $lorev = $this->rev($hirev);
146 if ($lorev == 'HEAD')
147 {
148 $lorev = 0;
149 }
150
151 if (is_integer($hirev) AND is_integer($lorev))
152 {
153 if ($lorev > $hirev)
154 {
155 $lorev = $hirev - 1;
156 }
157 }
158
159 $repospath = $viewsvn->repos->fetch_path($repos, false);
160
161 return $this->svn('log -r' . $hirev . ':' . $lorev . ' ' . $repospath . $path);
162 }
163
164 /**
165 * SVN Wrapper: ls (list)
166 *
167 * @access protected
168 *
169 * @param string Repository
170 * @param string Path
171 * @param integer Revision
172 *
173 * @return array Lines of list output
174 */
175 function ls($repos, $path, $revision)
176 {
177 return $this->std('list', $repos, $path, $revision);
178 }
179
180 /**
181 * Generates a clean revision number
182 *
183 * @access public
184 *
185 * @param integer Revision number
186 *
187 * @return mixed Cleaned revision or HEAD
188 */
189 function rev($revision)
190 {
191 if (($revision = intval($revision)) < 1)
192 {
193 $revision = 'HEAD';
194 }
195 return $revision;
196 }
197 }
198
199 /**
200 * Annotation/blame system; constructs an array
201 * that is ready for output
202 *
203 * @package ViewSVN
204 * @version $Id$
205 */
206 class SVNBlame
207 {
208 /**
209 * Array of blame information
210 * @var array
211 */
212 var $blame = array();
213
214 /**
215 * Raw "svn blame" output
216 */
217 var $rawoutput;
218
219 /**
220 * Constructor: create blame and store data
221 *
222 * @param string Repository
223 * @param string Path
224 * @param integer Revision
225 */
226 function SVNBlame($repos, $path, $revision)
227 {
228 global $viewsvn;
229
230 $this->rawoutput = $viewsvn->svn->blame($repos, $path, $revision);
231 $this->process();
232 }
233
234 /**
235 * Returns blame for display
236 *
237 * @access public
238 *
239 * @return array Blame data
240 */
241 function fetch()
242 {
243 return $this->blame;
244 }
245
246 /**
247 * Parses the blame data
248 *
249 * @access private
250 */
251 function process()
252 {
253 $lineno = 1;
254
255 foreach ($this->rawoutput AS $line)
256 {
257 if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)\s(.*)$#', $line, $matches))
258 {
259 $this->blame[] = array(
260 'rev' => $matches[1],
261 'author' => $matches[2],
262 'line' => $matches[3],
263 'lineno' => $lineno++
264 );
265 }
266 // a blank line
267 else if (preg_match('#^\s+([0-9]+)\s+([\w\.\-_]+)$#', $line, $matches))
268 {
269 $this->blame[] = array(
270 'rev' => $matches[1],
271 'author' => $matches[2],
272 'line' => '',
273 'lineno' => $lineno++
274 );
275 }
276 }
277 }
278 }
279
280 /**
281 * Log management system; creates a complex list
282 * of SVN log information
283 *
284 * @package ViewSVN
285 * @version $Id$
286 */
287 class SVNLog
288 {
289 /**
290 * Array of logs
291 * @var array
292 */
293 var $logs = array();
294
295 /**
296 * Raw "svn log" output
297 * @var array
298 */
299 var $rawoutput;
300
301 /**
302 * Constructor: create log store for the given file
303 *
304 * @param string Repository
305 * @param string Path
306 * @param integer Lower revision
307 * @param integer Higher revision
308 */
309 function SVNLog($repos, $path, $lorev, $hirev)
310 {
311 global $viewsvn;
312
313 $this->rawoutput = $viewsvn->svn->log($repos, $path, $lorev, $hirev);
314 $this->process();
315 }
316
317 /**
318 * Returns logs for display
319 *
320 * @access public
321 *
322 * @return array Log data
323 */
324 function fetch()
325 {
326 return $this->logs;
327 }
328
329 /**
330 * Splits up the raw output into a usable log
331 *
332 * @access private
333 */
334 function process()
335 {
336 $lastrev = 0;
337
338 for ($i = 1; $i <= count($this->rawoutput) - 1; $i++)
339 {
340 $line = $this->rawoutput["$i"];
341
342 if (preg_match('#^r([0-9]*) \| (.*?) \| (....-..-.. ..:..:..) ([0-9\-]*) \((.*?)\) \| ([0-9]*) lines?$#', $line, $matches))
343 {
344 if (isset($this->logs["$lastrev"]))
345 {
346 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
347 }
348
349 $this->logs["$matches[1]"] = array(
350 'rev' => $matches[1],
351 'author' => $matches[2],
352 'date' => $matches[3],
353 'timezone' => $matches[4],
354 'lines' => $matches[6],
355 'message' => ''
356 );
357
358 $lastrev = $matches[1];
359 }
360 else
361 {
362 $this->logs["$lastrev"]['message'] .= $line . "\n";
363 }
364 }
365
366 if (isset($this->logs["$lastrev"]))
367 {
368 $this->logs["$lastrev"]['message'] = $this->strip_last_line($this->logs["$lastrev"]['message']);
369 }
370 }
371
372 /**
373 * Trims the last dash line off a message
374 *
375 * @access private
376 *
377 * @param string Message with annoying-ass line
378 *
379 * @return string Clean string
380 */
381 function strip_last_line($string)
382 {
383 return trim(preg_replace("#\n(.*?)\n$#", '', $string));
384 }
385 }
386
387 /*=====================================================================*\
388 || ###################################################################
389 || # $HeadURL$
390 || # $Id$
391 || ###################################################################
392 \*=====================================================================*/
393 ?>