PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/bermi/akelos
PHP | 300 lines | 104 code | 16 blank | 180 comment | 9 complexity | d4d12d6a1d97a26d95acb9dd2b73fe75 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: Revert.php 106 2004-06-08 07:00:10Z clay $
  45. //
  46. /**
  47. * @package VersionControl_SVN
  48. * @category VersionControl
  49. * @author Clay Loveless <clay@killersoft.com>
  50. */
  51. /**
  52. * Subversion Revert command manager class
  53. *
  54. * Restore pristine working copy file (undo most local edits).
  55. *
  56. * Note: This subcommand does not require network access, and resolves
  57. * any conflicted states. However, it does not restore removed directories.
  58. *
  59. * $switches is an array containing one or more command line options
  60. * defined by the following associative keys:
  61. *
  62. * <code>
  63. *
  64. * $switches = array(
  65. * 'targets' => 'ARG',
  66. * // pass contents of file ARG as additional args
  67. * 'R' => true|false,
  68. * // descend recursively
  69. * 'recursive' => true|false,
  70. * // descend recursively
  71. * 'q [quiet]' => true|false,
  72. * // prints as little as possible
  73. * 'config-dir' => 'Path to a Subversion configuration directory'
  74. * );
  75. *
  76. * </code>
  77. *
  78. * Note: Subversion does not offer an XML output option for this subcommand
  79. *
  80. * The non-interactive option available on the command-line
  81. * svn client may also be set (true|false), but it is set to true by default.
  82. *
  83. * Usage example:
  84. * <code>
  85. * <?php
  86. * require_once 'VersionControl/SVN.php';
  87. *
  88. * // Setup error handling -- always a good idea!
  89. * $svnstack = &PEAR_ErrorStack::singleton('VersionControl_SVN');
  90. *
  91. * // Set up runtime options. Will be passed to all
  92. * // subclasses.
  93. * $options = array('fetchmode' => VERSIONCONTROL_SVN_FETCHMODE_RAW);
  94. *
  95. * // Pass array of subcommands we need to factory
  96. * $svn = VersionControl_SVN::factory(array('revert'), $options);
  97. *
  98. * // Define any switches and aguments we may need
  99. * $switches = array('R' => true);
  100. * $args = array('/path/to/working/copy/TestProj/trunk');
  101. *
  102. * // Run command
  103. * if ($output = $svn->revert->run($args, $switches)) {
  104. * print_r($output);
  105. * } else {
  106. * if (count($errs = $svnstack->getErrors())) {
  107. * foreach ($errs as $err) {
  108. * echo '<br />'.$err['message']."<br />\n";
  109. * echo "Command used: " . $err['params']['cmd'];
  110. * }
  111. * }
  112. * }
  113. * ?>
  114. * </code>
  115. *
  116. * @package VersionControl_SVN
  117. * @version @version@
  118. * @category SCM
  119. * @author Clay Loveless <clay@killersoft.com>
  120. */
  121. class VersionControl_SVN_Revert extends VersionControl_SVN
  122. {
  123. /**
  124. * Valid switches for svn revert
  125. *
  126. * @var array
  127. * @access public
  128. */
  129. var $valid_switches = array('R',
  130. 'recursive',
  131. 'quiet',
  132. 'q',
  133. 'targets',
  134. 'config-dir'
  135. );
  136. /**
  137. * Command-line arguments that should be passed
  138. * <b>outside</b> of those specified in {@link switches}.
  139. *
  140. * @var array
  141. * @access public
  142. */
  143. var $args = array();
  144. /**
  145. * Minimum number of args required by this subcommand.
  146. * See {@link http://svnbook.red-bean.com/svnbook/ Version Control with Subversion},
  147. * Subversion Complete Reference for details on arguments for this subcommand.
  148. * @var int
  149. * @access public
  150. */
  151. var $min_args = 1;
  152. /**
  153. * Switches 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 array
  157. * @access public
  158. */
  159. var $required_switches = array();
  160. /**
  161. * Prepare the svn subcommand switches.
  162. *
  163. * Defaults to non-interactive mode, and will auto-set the
  164. * --xml switch (if available) if $fetchmode is set to VERSIONCONTROL_SVN_FETCHMODE_XML,
  165. * VERSIONCONTROL_SVN_FETCHMODE_ASSOC or VERSIONCONTROL_SVN_FETCHMODE_OBJECT
  166. *
  167. * @param void
  168. * @return int true on success, false on failure. Check PEAR_ErrorStack
  169. * for error details, if any.
  170. */
  171. function prepare()
  172. {
  173. $meets_requirements = $this->checkCommandRequirements();
  174. if (!$meets_requirements) {
  175. return false;
  176. }
  177. $valid_switches = $this->valid_switches;
  178. $switches = $this->switches;
  179. $args = $this->args;
  180. $fetchmode = $this->fetchmode;
  181. $invalid_switches = array();
  182. $_switches = '';
  183. foreach ($switches as $switch => $val) {
  184. if (in_array($switch, $valid_switches)) {
  185. $switch = str_replace('_', '-', $switch);
  186. switch ($switch) {
  187. case 'targets':
  188. case 'config-dir':
  189. $_switches .= "--$switch $val ";
  190. break;
  191. case 'recursive':
  192. case 'quiet':
  193. if ($val === true) {
  194. $_switches .= "--$switch ";
  195. }
  196. break;
  197. case 'R':
  198. case 'v':
  199. case 'q':
  200. if ($val === true) {
  201. $_switches .= "-$switch ";
  202. }
  203. break;
  204. default:
  205. // that's all, folks!
  206. break;
  207. }
  208. } else {
  209. $invalid_switches[] = $switch;
  210. }
  211. }
  212. $_switches = trim($_switches);
  213. $this->_switches = $_switches;
  214. $cmd = "$this->svn_path $this->_svn_cmd $_switches";
  215. if (!empty($args)) {
  216. $cmd .= ' '. join(' ', $args);
  217. }
  218. $this->_prepped_cmd = $cmd;
  219. $this->prepared = true;
  220. $invalid = count($invalid_switches);
  221. if ($invalid > 0) {
  222. $params['was'] = 'was';
  223. $params['is_invalid_switch'] = 'is an invalid switch';
  224. if ($invalid > 1) {
  225. $params['was'] = 'were';
  226. $params['is_invalid_switch'] = 'are invalid switches';
  227. }
  228. $params['list'] = $invalid_switches;
  229. $params['switches'] = $switches;
  230. $params['_svn_cmd'] = ucfirst($this->_svn_cmd);
  231. $this->_stack->push(VERSIONCONTROL_SVN_NOTICE_INVALID_SWITCH, 'notice', $params);
  232. }
  233. return true;
  234. }
  235. // }}}
  236. // {{{ parseOutput()
  237. /**
  238. * Handles output parsing of standard and verbose output of command.
  239. *
  240. * @param array $out Array of output captured by exec command in {@link run}.
  241. * @return mixed Returns output requested by fetchmode (if available), or raw output
  242. * if desired fetchmode is not available.
  243. * @access public
  244. */
  245. function parseOutput($out)
  246. {
  247. $fetchmode = $this->fetchmode;
  248. switch($fetchmode) {
  249. case VERSIONCONTROL_SVN_FETCHMODE_RAW:
  250. return join("\n", $out);
  251. break;
  252. case VERSIONCONTROL_SVN_FETCHMODE_ARRAY:
  253. case VERSIONCONTROL_SVN_FETCHMODE_ASSOC:
  254. // Temporary, see parseOutputArray below
  255. return join("\n", $out);
  256. break;
  257. case VERSIONCONTROL_SVN_FETCHMODE_OBJECT:
  258. // Temporary, will return object-ified array from
  259. // parseOutputArray
  260. return join("\n", $out);
  261. break;
  262. case VERSIONCONTROL_SVN_FETCHMODE_XML:
  263. // Temporary, will eventually build an XML string
  264. // with XML_Util or XML_Tree
  265. return join("\n", $out);
  266. break;
  267. default:
  268. // What you get with VERSIONCONTROL_SVN_FETCHMODE_DEFAULT
  269. return join("\n", $out);
  270. break;
  271. }
  272. }
  273. /**
  274. * Helper method for parseOutput that parses output into an associative array
  275. *
  276. * @todo Finish this method! : )
  277. */
  278. function parseOutputArray($out)
  279. {
  280. $parsed = array();
  281. }
  282. }
  283. // }}}
  284. ?>