/plugins/SVNPlugin/tags/1.4.0/src/ise/plugin/svn/command/Log.java

#
Java | 294 lines | 178 code | 43 blank | 73 comment | 24 complexity | ae7123f98c805b18aebc244c07d47fbd MD5 | raw file

✨ Summary
  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 org.tmatesoft.svn.core.ISVNLogEntryHandler;
  29. import org.tmatesoft.svn.core.SVNLogEntry;
  30. import org.tmatesoft.svn.core.SVNLogEntryPath;
  31. import org.tmatesoft.svn.core.SVNURL;
  32. import org.tmatesoft.svn.core.wc.SVNInfo;
  33. import org.tmatesoft.svn.core.wc.SVNLogClient;
  34. import org.tmatesoft.svn.core.wc.SVNRevision;
  35. import org.tmatesoft.svn.core.wc.ISVNOptions;
  36. import org.tmatesoft.svn.core.wc.SVNClientManager;
  37. import org.tmatesoft.svn.core.wc.SVNWCUtil;
  38. import org.tmatesoft.svn.core.SVNException;
  39. import ise.plugin.svn.data.LogData;
  40. import ise.plugin.svn.data.LogResults;
  41. public class Log {
  42. private LogResults results = new LogResults();
  43. // <file path, list of log entries for the file>
  44. private TreeMap < String, List < SVNLogEntry >> entries = new TreeMap < String, List < SVNLogEntry >> ();
  45. private PrintStream out = null;
  46. /**
  47. * Fills a LogResults based on the given LogData.
  48. * @param data LogData containing the information necessary to fetch an svn log.
  49. */
  50. public void doLog( LogData data ) throws CommandInitializationException, SVNException {
  51. SVNKit.setupLibrary();
  52. // validate data values
  53. if ( data.getPaths() == null ) {
  54. return ; // nothing to do
  55. }
  56. if ( data.getOut() == null ) {
  57. throw new CommandInitializationException( "Invalid output stream." );
  58. }
  59. if ( data.getErr() == null ) {
  60. data.setErr( data.getOut() );
  61. }
  62. // convert paths to Files
  63. List<String> paths = data.getPaths();
  64. File[] localPaths = null;
  65. if ( !data.pathsAreURLs() ) {
  66. localPaths = new File[ paths.size() ];
  67. for ( int i = 0; i < paths.size(); i++ ) {
  68. localPaths[ i ] = new File( paths.get( i ) );
  69. // check for file existence?
  70. }
  71. }
  72. // use default svn config options
  73. ISVNOptions options = SVNWCUtil.createDefaultOptions( true );
  74. // use the svnkit client manager
  75. SVNClientManager clientManager = SVNClientManager.newInstance( options, SVNWCUtil.createDefaultAuthenticationManager( data.getUsername(), data.getDecryptedPassword() ) );
  76. // get log client
  77. SVNLogClient client = clientManager.getLogClient();
  78. // set an event handler so that messages go to the streams for display
  79. client.setEventHandler( new SVNCommandEventProcessor( data.getOut(), data.getErr(), false ) );
  80. out = data.getOut();
  81. LogHandler handler = new LogHandler();
  82. if ( data.pathsAreURLs() ) {
  83. // get the repository url, then trim the paths to be relative to
  84. // the repository url so they can be passed all at once to the svn
  85. // server.
  86. SVNURL repositoryUrl = getRepositoryURL( data );
  87. if ( repositoryUrl == null ) {
  88. out.println( "ERROR: repository URL is null" );
  89. out.flush();
  90. out.close();
  91. return ;
  92. }
  93. int length = repositoryUrl.toString().length();
  94. Set<String> pathsToCheck = new HashSet<String>();
  95. for ( String path : data.getPaths() ) {
  96. String toCheck = path.substring( length );
  97. pathsToCheck.add( toCheck );
  98. }
  99. for ( String path : pathsToCheck ) {
  100. out.println( "Log:" );
  101. out.println( "\trepository url: " + repositoryUrl );
  102. out.println( "\t path: " + path );
  103. out.println( "\t peg revision: " + data.getPegRevision() );
  104. out.println( "\tstart revision: " + data.getStartRevision() );
  105. out.println( "\t end revision: " + data.getEndRevision() );
  106. out.println( "\t stop on copy: " + data.getStopOnCopy() );
  107. out.println( "\t show paths: " + data.getShowPaths() );
  108. out.println( "\t max logs: " + data.getMaxLogs() );
  109. handler.setPath( path );
  110. String[] pathToCheck = { path };
  111. // Get log message for each path one at a time. While it is possible
  112. // to get the log messages for several paths at once, it is impossible
  113. // to tell which message goes with which file.
  114. // doLog(SVNURL url, String[] paths, SVNRevision pegRevision, SVNRevision startRevision,
  115. // SVNRevision endRevision, boolean stopOnCopy, boolean discoverChangedPaths, long limit, ISVNLogEntryHandler handler)
  116. client.doLog( repositoryUrl, pathToCheck, data.getPegRevision(), data.getStartRevision(),
  117. data.getEndRevision(), data.getStopOnCopy(), data.getShowPaths(), data.getMaxLogs(), handler );
  118. }
  119. }
  120. else {
  121. for ( File path : localPaths ) {
  122. out.println( "Log:" );
  123. out.println( "\t file: " + path.getAbsolutePath() );
  124. out.println( "\t peg revision: " + data.getPegRevision() );
  125. out.println( "\tstart revision: " + data.getStartRevision() );
  126. out.println( "\t end revision: " + data.getEndRevision() );
  127. out.println( "\t stop on copy: " + data.getStopOnCopy() );
  128. out.println( "\t show paths: " + data.getShowPaths() );
  129. out.println( "\t max logs: " + data.getMaxLogs() );
  130. handler.setPath( path.getAbsolutePath() );
  131. File[] pathToCheck = new File[] { path };
  132. // svnkit method signature:
  133. // doLog(File[] paths, SVNRevision pegRevision, SVNRevision startRevision,
  134. // SVNRevision endRevision, boolean stopOnCopy, boolean discoverChangedPaths, long limit, ISVNLogEntryHandler handler)
  135. client.doLog( pathToCheck, data.getPegRevision(), data.getStartRevision(),
  136. data.getEndRevision(), data.getStopOnCopy(), data.getShowPaths(), data.getMaxLogs(), handler );
  137. }
  138. }
  139. results.setEntries( entries );
  140. out.flush();
  141. out.close();
  142. }
  143. public class LogHandler implements ISVNLogEntryHandler {
  144. private String path = null;
  145. public void setPath( String path ) {
  146. this.path = path;
  147. }
  148. public void handleLogEntry( SVNLogEntry logEntry ) {
  149. List<SVNLogEntry> messages = entries.get(path);
  150. if (messages == null) {
  151. messages = new ArrayList<SVNLogEntry>();
  152. entries.put(path, messages);
  153. }
  154. messages.add( logEntry );
  155. }
  156. }
  157. /**
  158. * @return a log results object
  159. */
  160. public LogResults getLogEntries() {
  161. return results;
  162. }
  163. public void printLogEntry( String path, SVNLogEntry logEntry ) {
  164. if ( out == null ) {
  165. return ;
  166. }
  167. out.println( "path: " + path );
  168. out.println( "revision: " + logEntry.getRevision() );
  169. out.println( "author: " + logEntry.getAuthor() );
  170. out.println( "date: " + logEntry.getDate() );
  171. out.println( "log message: " + logEntry.getMessage() );
  172. // displaying all paths that were changed in that revision, changed
  173. // path information is represented by SVNLogEntryPath.
  174. if ( logEntry.getChangedPaths().size() > 0 ) {
  175. out.println();
  176. out.println( "changed paths:" );
  177. // keys are changed paths
  178. Set changedPathsSet = logEntry.getChangedPaths().keySet();
  179. for ( Iterator changedPaths = changedPathsSet.iterator(); changedPaths.hasNext(); ) {
  180. SVNLogEntryPath entryPath = ( SVNLogEntryPath ) logEntry.getChangedPaths().get( changedPaths.next() );
  181. /*
  182. SVNLogEntryPath.getPath returns the changed path itself
  183. SVNLogEntryPath.getType returns a character describing
  184. how the path was changed ('A' - added, 'D' - deleted or
  185. 'M' - modified);
  186. If the path was copied from another one (branched) then
  187. SVNLogEntryPath.getCopyPath and
  188. SVNLogEntryPath.getCopyRevision tells where it was copied
  189. from and what revision the origin path was at.
  190. */
  191. out.println( " "
  192. + entryPath.getType()
  193. + " "
  194. + entryPath.getPath()
  195. + ( ( entryPath.getCopyPath() != null ) ? " (from "
  196. + entryPath.getCopyPath() + " revision "
  197. + entryPath.getCopyRevision() + ")" : "" ) );
  198. }
  199. }
  200. }
  201. private SVNURL getRepositoryURL( LogData data ) {
  202. try {
  203. Info info = new Info( );
  204. List<SVNInfo> infos = info.getInfo( data );
  205. if ( infos.size() == 0 ) {
  206. return null;
  207. }
  208. SVNInfo svn_info = infos.get( 0 );
  209. return svn_info.getRepositoryRootURL();
  210. }
  211. catch ( Exception e ) {
  212. return null;
  213. }
  214. }
  215. public static void main ( String[] args ) {
  216. // for testing
  217. LogData data = new LogData();
  218. data.setUsername( "daleanson" );
  219. data.setPassword( "" );
  220. List<String> paths = new ArrayList<String>();
  221. paths.add( "/home/danson/src/plugins/SVNPlugin/src/ise/plugin/svn/command/Log.java" );
  222. data.setPaths( paths );
  223. data.setOut( new ise.plugin.svn.io.ConsolePrintStream( new ise.plugin.svn.io.LogOutputStream( null ) ) );
  224. //long start_rev = 9795L;
  225. //long end_rev = 9810L;
  226. java.util.Calendar cal = java.util.Calendar.getInstance();
  227. cal.set( 2007, 5, 1 );
  228. //SVNRevision start = SVNRevision.parse(String.valueOf(start_rev));
  229. SVNRevision start = SVNRevision.create( cal.getTime() );
  230. cal.set( 2007, 6, 1 );
  231. //SVNRevision end = SVNRevision.parse(String.valueOf(end_rev));
  232. SVNRevision end = SVNRevision.create( cal.getTime() );
  233. data.setStartRevision( start );
  234. data.setEndRevision( end );
  235. Log log = new Log();
  236. try {
  237. org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory.setup();
  238. org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl.setup();
  239. org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory.setup();
  240. log.doLog( data );
  241. }
  242. catch ( Exception e ) {
  243. e.printStackTrace();
  244. }
  245. }
  246. }