/plugins/SVNPlugin/tags/0.6.0/src/ise/plugin/svn/command/Info.java

# · Java · 241 lines · 185 code · 21 blank · 35 comment · 77 complexity · 0ca687535b534728882dd021bd8e189d MD5 · raw file

  1. /*
  2. Copyright (c) 2007, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package ise.plugin.svn.command;
  26. import java.io.*;
  27. import java.util.*;
  28. import java.text.BreakIterator;
  29. import java.text.DateFormat;
  30. import java.text.SimpleDateFormat;
  31. import org.tmatesoft.svn.cli.command.SVNCommandEventProcessor;
  32. import org.tmatesoft.svn.cli.SVNArgument;
  33. import org.tmatesoft.svn.cli.SVNCommand;
  34. import org.tmatesoft.svn.core.SVNException;
  35. import org.tmatesoft.svn.core.SVNLock;
  36. import org.tmatesoft.svn.core.SVNNodeKind;
  37. import org.tmatesoft.svn.core.SVNURL;
  38. import org.tmatesoft.svn.core.internal.util.SVNFormatUtil;
  39. import org.tmatesoft.svn.core.internal.util.SVNPathUtil;
  40. import org.tmatesoft.svn.core.wc.ISVNInfoHandler;
  41. import org.tmatesoft.svn.core.wc.ISVNOptions;
  42. import org.tmatesoft.svn.core.wc.SVNClientManager;
  43. import org.tmatesoft.svn.core.wc.SVNInfo;
  44. import org.tmatesoft.svn.core.wc.SVNRevision;
  45. import org.tmatesoft.svn.core.wc.SVNWCClient;
  46. import org.tmatesoft.svn.core.wc.SVNWCUtil;
  47. import org.tmatesoft.svn.core.wc.xml.SVNXMLInfoHandler;
  48. import org.tmatesoft.svn.core.wc.xml.SVNXMLSerializer;
  49. import ise.plugin.svn.data.SVNData;
  50. public class Info {
  51. public List<SVNInfo> info( SVNData data ) throws CommandInitializationException, SVNException {
  52. List<SVNInfo> results = getInfo( data );
  53. // print the results
  54. for ( SVNInfo info : results ) {
  55. handleInfo( info, data.getOut() );
  56. }
  57. data.getOut().close();
  58. return results;
  59. }
  60. public List<SVNInfo> getInfo( SVNData data ) throws CommandInitializationException, SVNException {
  61. SVNKit.setupLibrary();
  62. // validate commit data values
  63. if ( data.getPaths() == null ) {
  64. return null; // nothing to do
  65. }
  66. if ( data.getOut() == null ) {
  67. throw new CommandInitializationException( "Invalid output stream." );
  68. }
  69. if ( data.getErr() == null ) {
  70. data.setErr( data.getOut() );
  71. }
  72. // convert first path to File
  73. List<String> paths = data.getPaths();
  74. // use default svn config options
  75. ISVNOptions options = SVNWCUtil.createDefaultOptions( true );
  76. // use the svnkit client manager
  77. SVNClientManager clientManager = SVNClientManager.newInstance( options, data.getUsername(), data.getPassword() );
  78. // get a commit client
  79. SVNWCClient client = clientManager.getWCClient();
  80. // set an event handler so that messages go to the commit data streams for display
  81. client.setEventHandler( new SVNCommandEventProcessor( data.getOut(), data.getErr(), false ) );
  82. // actually fetch the info
  83. List<SVNInfo> results = new ArrayList<SVNInfo>();
  84. if ( data.pathsAreURLs() ) {
  85. for ( String path : data.getPaths() ) {
  86. SVNURL svnurl = SVNURL.parseURIDecoded( path );
  87. SVNInfo result = client.doInfo( svnurl, SVNRevision.HEAD, SVNRevision.HEAD );
  88. results.add( result );
  89. }
  90. }
  91. else {
  92. for ( String path : paths ) {
  93. File localPath = new File( path );
  94. SVNInfo result = client.doInfo( localPath, SVNRevision.HEAD );
  95. results.add( result );
  96. }
  97. }
  98. return results;
  99. }
  100. public void handleInfo( SVNInfo info, PrintStream out ) {
  101. StringBuffer sb = new StringBuffer();
  102. sb.append( "\n" );
  103. if ( !info.isRemote() ) {
  104. sb.append( "Path: " + SVNFormatUtil.formatPath( info.getFile() ) + "\n" );
  105. }
  106. else if ( info.getPath() != null ) {
  107. String path = info.getPath();
  108. path = path.replace( '/', File.separatorChar );
  109. sb.append( "Path: " + path + "\n" );
  110. }
  111. if ( info.getKind() != SVNNodeKind.DIR ) {
  112. if ( info.isRemote() ) {
  113. sb.append( "Name: " + SVNPathUtil.tail( info.getPath() ) + "\n" );
  114. }
  115. else {
  116. sb.append( "Name: " + info.getFile().getName() + "\n" );
  117. }
  118. }
  119. sb.append( "URL: " + info.getURL() + "\n" );
  120. if ( info.getRepositoryRootURL() != null ) {
  121. sb.append( "Repository Root: " + info.getRepositoryRootURL() + "\n" );
  122. }
  123. if ( info.isRemote() && info.getRepositoryUUID() != null ) {
  124. sb.append( "Repository UUID: " + info.getRepositoryUUID() + "\n" );
  125. }
  126. if ( info.getRevision() != null && info.getRevision().isValid() ) {
  127. sb.append( "Revision: " + info.getRevision() + "\n" );
  128. }
  129. if ( info.getKind() == SVNNodeKind.DIR ) {
  130. sb.append( "Node Kind: directory" + "\n" );
  131. }
  132. else if ( info.getKind() == SVNNodeKind.FILE ) {
  133. sb.append( "Node Kind: file" + "\n" );
  134. }
  135. else if ( info.getKind() == SVNNodeKind.NONE ) {
  136. sb.append( "Node Kind: none" + "\n" );
  137. }
  138. else {
  139. sb.append( "Node Kind: unknown" + "\n" );
  140. }
  141. if ( info.getSchedule() == null && !info.isRemote() ) {
  142. sb.append( "Schedule: normal" + "\n" );
  143. }
  144. else if ( !info.isRemote() ) {
  145. sb.append( "Schedule: " + info.getSchedule() + "\n" );
  146. }
  147. if ( info.getAuthor() != null ) {
  148. sb.append( "Last Changed Author: " + info.getAuthor() + "\n" );
  149. }
  150. if ( info.getCommittedRevision() != null && info.getCommittedRevision().getNumber() >= 0 ) {
  151. sb.append( "Last Changed Rev: " + info.getCommittedRevision() + "\n" );
  152. }
  153. if ( info.getCommittedDate() != null ) {
  154. sb.append( "Last Changed Date: " + formatDate( info.getCommittedDate() ) + "\n" );
  155. }
  156. if ( !info.isRemote() ) {
  157. if ( info.getTextTime() != null ) {
  158. sb.append( "Text Last Updated: " + formatDate( info.getTextTime() ) + "\n" );
  159. }
  160. if ( info.getPropTime() != null ) {
  161. sb.append( "Properties Last Updated: " + formatDate( info.getPropTime() ) + "\n" );
  162. }
  163. if ( info.getChecksum() != null ) {
  164. sb.append( "Checksum: " + info.getChecksum() + "\n" );
  165. }
  166. if ( info.getCopyFromURL() != null ) {
  167. sb.append( "Copied From URL: " + info.getCopyFromURL() + "\n" );
  168. }
  169. if ( info.getCopyFromRevision() != null && info.getCopyFromRevision().getNumber() >= 0 ) {
  170. sb.append( "Copied From Rev: " + info.getCopyFromRevision() + "\n" );
  171. }
  172. if ( info.getConflictOldFile() != null ) {
  173. sb.append( "Conflict Previous Base File: " + info.getConflictOldFile().getName() + "\n" );
  174. }
  175. if ( info.getConflictWrkFile() != null ) {
  176. sb.append( "Conflict Previous Working File: " + info.getConflictWrkFile().getName() + "\n" );
  177. }
  178. if ( info.getConflictNewFile() != null ) {
  179. sb.append( "Conflict Current Base File: " + info.getConflictNewFile().getName() + "\n" );
  180. }
  181. if ( info.getPropConflictFile() != null ) {
  182. sb.append( "Conflict Properties File: " + info.getPropConflictFile().getName() + "\n" );
  183. }
  184. }
  185. if ( info.getLock() != null ) {
  186. SVNLock lock = info.getLock();
  187. sb.append( "Lock Token: " + lock.getID() + "\n" );
  188. sb.append( "Lock Owner: " + lock.getOwner() + "\n" );
  189. sb.append( "Lock Created: " + formatDate( lock.getCreationDate() ) + "\n" );
  190. if ( lock.getComment() != null ) {
  191. sb.append( "Lock Comment " );
  192. int lineCount = getLineCount( lock.getComment() );
  193. if ( lineCount == 1 ) {
  194. sb.append( "(1 line)" );
  195. }
  196. else {
  197. sb.append( "(" + lineCount + " lines)" );
  198. }
  199. sb.append( ":\n" + lock.getComment() + "\n" );
  200. }
  201. }
  202. out.println( sb.toString() );
  203. out.flush();
  204. }
  205. private static String formatDate( Date date ) {
  206. return new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss Z (EE, d MMM yyyy)", Locale.getDefault() ).format( date );
  207. }
  208. private int getLineCount( String s ) {
  209. int count = 0;
  210. BreakIterator boundary = BreakIterator.getLineInstance();
  211. int start = boundary.first();
  212. for ( int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next() ) {
  213. ++count;
  214. }
  215. return count;
  216. }
  217. }