PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/akelos_utils/contrib/pear/VersionControl/SVN/Blame.php

https://github.com/bermi/akelos
PHP | 310 lines | 108 code | 17 blank | 185 comment | 9 complexity | 0d45003675f4a97782cb70d36989f275 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2004, Clay Loveless |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | This LICENSE is in the BSD license style. |
  10. // | http://www.opensource.org/licenses/bsd-license.php |
  11. // | |
  12. // | Redistribution and use in source and binary forms, with or without |
  13. // | modification, are permitted provided that the following conditions |
  14. // | are met: |
  15. // | |
  16. // | * Redistributions of source code must retain the above copyright |
  17. // | notice, this list of conditions and the following disclaimer. |
  18. // | |
  19. // | * Redistributions in binary form must reproduce the above |
  20. // | copyright notice, this list of conditions and the following |
  21. // | disclaimer in the documentation and/or other materials provided |
  22. // | with the distribution. |
  23. // | |
  24. // | * Neither the name of Clay Loveless nor the names of contributors |
  25. // | may be used to endorse or promote products derived from this |
  26. // | software without specific prior written permission. |
  27. // | |
  28. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  29. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  30. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  31. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  32. // | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  33. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  34. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
  35. // | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  36. // | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  37. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
  38. // | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  39. // | POSSIBILITY OF SUCH DAMAGE. |
  40. // +----------------------------------------------------------------------+
  41. // | Author: Clay Loveless <clay@killersoft.com> |
  42. // +----------------------------------------------------------------------+
  43. //
  44. // $Id: Blame.php 42 2004-04-26 09:39:59Z clay $
  45. //
  46. /**
  47. * @package VersionControl_SVN
  48. * @category VersionControl
  49. * @author Clay Loveless <clay@killersoft.com>
  50. */
  51. /**
  52. * Subversion Blame command manager class
  53. *
  54. * Outputs the content of specified files or URLs with revision and
  55. * author information in-line.
  56. *
  57. * $switches is an array containing one or more command line options
  58. * defined by the following associative keys:
  59. *
  60. * <code>
  61. *
  62. * $switches = array(
  63. * 'r [revision]' => 'ARG (some commands also take ARG1:ARG2 range)
  64. * A revision argument can be one of:
  65. * NUMBER revision number
  66. * "{" DATE "}" revision at start of the date
  67. * "HEAD" latest in repository
  68. * "BASE" base rev of item's working copy
  69. * "COMMITTED" last commit at or before BASE
  70. * "PREV" revision just before COMMITTED',
  71. * // either 'r' or 'revision' may be used
  72. * 'username' => 'Subversion repository login',
  73. * 'password' => 'Subversion repository password',
  74. * 'no-auth-cache' => true|false,
  75. * // Do not cache authentication tokens
  76. * 'config-dir' => 'Path to a Subversion configuration directory'
  77. * );
  78. *
  79. * </code>
  80. *
  81. * Note: Subversion does not offer an XML output option for this subcommand
  82. *
  83. * The non-interactive option available on the command-line
  84. * svn client may also be set (true|false), but it is set to true by default.
  85. *
  86. * Usage example:
  87. * <code>
  88. * <?php
  89. * require_once 'VersionControl/SVN.php';
  90. *
  91. * // Setup error handling -- always a good idea!
  92. * $svnstack = &PEAR_ErrorStack::singleton('VersionControl_SVN');
  93. *
  94. * // Set up runtime options. Will be passed to all
  95. * // subclasses.
  96. * $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_RAW);
  97. *
  98. * // Pass array of subcommands we need to factory
  99. * // Blame supports alternate names: blame, praise, annotate, ann
  100. * $svn = VersionControl_SVN::factory(array('praise'), $options);
  101. *
  102. * // Define any switches and aguments we may need
  103. * $switches = array('username' => 'user', 'password' => 'pass');
  104. * $args = array('svn://svn.example.com/repos/TestProject');
  105. *
  106. * // Run command
  107. * if ($output = $svn->praise->run($args, $switches)) {
  108. * print_r($output);
  109. * } else {
  110. * if (count($errs = $svnstack->getErrors())) {
  111. * foreach ($errs as $err) {
  112. * echo '<br />'.$err['message']."<br />\n";
  113. * echo "Command used: " . $err['params']['cmd'];
  114. * }
  115. * }
  116. * }
  117. * ?>
  118. * </code>
  119. *
  120. * @package VersionControl_SVN
  121. * @version @version@
  122. * @category SCM
  123. * @author Clay Loveless <clay@killersoft.com>
  124. */
  125. class VersionControl_SVN_Blame extends VersionControl_SVN
  126. {
  127. /**
  128. * Valid switches for svn blame
  129. *
  130. * @var array
  131. * @access public
  132. */
  133. var $valid_switches = array('r',
  134. 'revision',
  135. 'username',
  136. 'password',
  137. 'no-auth-cache',
  138. 'no_auth_cache',
  139. 'non-interactive',
  140. 'non_interactive',
  141. 'config-dir',
  142. 'config_dir'
  143. );
  144. /**
  145. * Command-line arguments that should be passed
  146. * <b>outside</b> of those specified in {@link switches}.
  147. *
  148. * @var array
  149. * @access public
  150. */
  151. var $args = array();
  152. /**
  153. * Minimum number of args required by this subcommand.
  154. * See {@link http://svnbook.red-bean.com/svnbook/ Version Control with Subversion},
  155. * Subversion Complete Reference for details on arguments for this subcommand.
  156. * @var int
  157. * @access public
  158. */
  159. var $min_args = 1;
  160. /**
  161. * Switches required by this subcommand.
  162. * See {@link http://svnbook.red-bean.com/svnbook/ Version Control with Subversion},
  163. * Subversion Complete Reference for details on arguments for this subcommand.
  164. * @var array
  165. * @access public
  166. */
  167. var $required_switches = array();
  168. /**
  169. * Prepare the svn subcommand switches.
  170. *
  171. * Defaults to non-interactive mode, and will auto-set the
  172. * --xml switch if $fetchmode is set to VERSIONCONTROL_SVN_FETCHMODE_XML,
  173. * VERSIONCONTROL_SVN_FETCHMODE_ASSOC or VERSIONCONTROL_SVN_FETCHMODE_OBJECT
  174. *
  175. * @param void
  176. * @return int true on success, false on failure. Check PEAR_ErrorStack
  177. * for error details, if any.
  178. */
  179. function prepare()
  180. {
  181. $meets_requirements = $this->checkCommandRequirements();
  182. if (!$meets_requirements) {
  183. return false;
  184. }
  185. $valid_switches = $this->valid_switches;
  186. $switches = $this->switches;
  187. $args = $this->args;
  188. $fetchmode = $this->fetchmode;
  189. $invalid_switches = array();
  190. $_switches = '';
  191. foreach ($switches as $switch => $val) {
  192. if (in_array($switch, $valid_switches)) {
  193. $switch = str_replace('_', '-', $switch);
  194. switch ($switch) {
  195. case 'revision':
  196. case 'username':
  197. case 'password':
  198. case 'config-dir':
  199. $_switches .= "--$switch $val ";
  200. break;
  201. case 'r':
  202. $_switches .= "-$switch $val ";
  203. break;
  204. case 'no-auth-cache':
  205. case 'non-interactive':
  206. if ($val === true) {
  207. $_switches .= "--$switch ";
  208. }
  209. break;
  210. default:
  211. // that's all, folks!
  212. break;
  213. }
  214. } else {
  215. $invalid_switches[] = $switch;
  216. }
  217. }
  218. // We don't want interactive mode
  219. if (strpos($_switches, 'non-interactive') === false) {
  220. $_switches .= '--non-interactive ';
  221. }
  222. $_switches = trim($_switches);
  223. $this->_switches = $_switches;
  224. $cmd = "$this->svn_path $this->_svn_cmd $_switches";
  225. if (!empty($args)) {
  226. $cmd .= ' '. join(' ', $args);
  227. }
  228. $this->_prepped_cmd = $cmd;
  229. $this->prepared = true;
  230. $invalid = count($invalid_switches);
  231. if ($invalid > 0) {
  232. $params['was'] = 'was';
  233. $params['is_invalid_switch'] = 'is an invalid switch';
  234. if ($invalid > 1) {
  235. $params['was'] = 'were';
  236. $params['is_invalid_switch'] = 'are invalid switches';
  237. }
  238. $params['list'] = $invalid_switches;
  239. $params['switches'] = $switches;
  240. $params['_svn_cmd'] = ucfirst($this->_svn_cmd);
  241. $this->_stack->push(VERSIONCONTROL_SVN_NOTICE_INVALID_SWITCH, 'notice', $params);
  242. }
  243. return true;
  244. }
  245. // }}}
  246. // {{{ parseOutput()
  247. /**
  248. * Handles output parsing of standard and verbose output of command.
  249. *
  250. * @param array $out Array of output captured by exec command in {@link run}.
  251. * @return mixed Returns output requested by fetchmode (if available), or raw output
  252. * if desired fetchmode is not available.
  253. * @access public
  254. */
  255. function parseOutput($out)
  256. {
  257. $fetchmode = $this->fetchmode;
  258. switch($fetchmode) {
  259. case VERSIONCONTROL_SVN_FETCHMODE_RAW:
  260. return join("\n", $out);
  261. break;
  262. case VERSIONCONTROL_SVN_FETCHMODE_ASSOC:
  263. // Temporary, see parseOutputArray below
  264. return join("\n", $out);
  265. break;
  266. case VERSIONCONTROL_SVN_FETCHMODE_OBJECT:
  267. // Temporary, will return object-ified array from
  268. // parseOutputArray
  269. return join("\n", $out);
  270. break;
  271. case VERSIONCONTROL_SVN_FETCHMODE_XML:
  272. // Temporary, will eventually build an XML string
  273. // with XML_Util or XML_Tree
  274. return join("\n", $out);
  275. break;
  276. default:
  277. // What you get with VERSIONCONTROL_SVN_FETCHMODE_DEFAULT
  278. return join("\n", $out);
  279. break;
  280. }
  281. }
  282. /**
  283. * Helper method for parseOutput that parses output into an associative array
  284. *
  285. * @todo Finish this method! : )
  286. */
  287. function parseOutputArray($out)
  288. {
  289. $parsed = array();
  290. }
  291. }
  292. /// }}}
  293. ?>