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

#
Java | 257 lines | 157 code | 33 blank | 67 comment | 21 complexity | efb4c62b71745ab0d7aeeb824ec67487 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.SVNLogClient;
  33. import org.tmatesoft.svn.core.wc.SVNRevision;
  34. import org.tmatesoft.svn.core.wc.ISVNOptions;
  35. import org.tmatesoft.svn.core.wc.SVNClientManager;
  36. import org.tmatesoft.svn.core.wc.SVNWCUtil;
  37. import org.tmatesoft.svn.core.wc.SVNWCClient;
  38. import org.tmatesoft.svn.cli.command.SVNCommandEventProcessor;
  39. import org.tmatesoft.svn.core.wc.SVNInfo;
  40. import org.tmatesoft.svn.core.SVNException;
  41. import ise.plugin.svn.data.LogData;
  42. import ise.plugin.svn.data.LogResults;
  43. public class Log {
  44. private LogResults results = new LogResults();
  45. private TreeMap < String, List < SVNLogEntry >> entries = new TreeMap < String, List < SVNLogEntry >> ();
  46. private PrintStream out = null;
  47. /**
  48. * Fills a LogResults based on the given LogData.
  49. * @param data LogData containing the information necessary to fetch an svn log.
  50. */
  51. public void doLog( LogData data ) throws CommandInitializationException, SVNException {
  52. SVNKit.setupLibrary();
  53. // validate data values
  54. if ( data.getPaths() == null ) {
  55. return ; // nothing to do
  56. }
  57. if ( data.getOut() == null ) {
  58. throw new CommandInitializationException( "Invalid output stream." );
  59. }
  60. if ( data.getErr() == null ) {
  61. data.setErr( data.getOut() );
  62. }
  63. // convert paths to Files
  64. List<String> paths = data.getPaths();
  65. File[] localPaths = null;
  66. if ( !data.pathsAreURLs() ) {
  67. localPaths = new File[ paths.size() ];
  68. for ( int i = 0; i < paths.size(); i++ ) {
  69. localPaths[ i ] = new File( paths.get( i ) );
  70. // check for file existence?
  71. }
  72. }
  73. // use default svn config options
  74. ISVNOptions options = SVNWCUtil.createDefaultOptions( true );
  75. // use the svnkit client manager
  76. SVNClientManager clientManager = SVNClientManager.newInstance( options, data.getUsername(), data.getDecryptedPassword() );
  77. // get a commit client
  78. SVNLogClient client = clientManager.getLogClient();
  79. SVNWCClient wc_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. out = data.getOut();
  83. if ( data.pathsAreURLs() ) {
  84. for (String path : data.getPaths()) {
  85. SVNURL svnurl = SVNURL.parseURIDecoded(path);
  86. LogHandler handler = new LogHandler( path );
  87. // files for logs, paths, peg revision, start revision,
  88. // end revision, stop on copy, report paths, number of entries, handler
  89. client.doLog( svnurl, null, data.getPegRevision(), data.getStartRevision(),
  90. data.getEndRevision(), data.getStopOnCopy(), data.getShowPaths(), data.getMaxLogs(), handler );
  91. entries.put( handler.getPath(), handler.getEntries() );
  92. }
  93. }
  94. else {
  95. for ( File file : localPaths ) {
  96. // this feels like a kludge, I shouldn't have to do a substring
  97. // call to figure out the path of the file
  98. LogHandler handler = new LogHandler( file );
  99. SVNInfo info = wc_client.doInfo(file, SVNRevision.WORKING);
  100. results.setInfo(info);
  101. String rep_url_string = info.getRepositoryRootURL().toString();
  102. String file_url_string = info.getURL().toString();
  103. String path = file_url_string.substring(rep_url_string.length());
  104. SVNURL rep_url = SVNURL.parseURIEncoded(rep_url_string);
  105. String[] rep_paths = new String[]{path};
  106. // I should also be able to set the peg revision, but it seems that
  107. // using anything beside 0 or UNDEFINED fails.
  108. client.doLog( rep_url, rep_paths, data.getPegRevision(), data.getStartRevision(),
  109. data.getEndRevision(), data.getStopOnCopy(), data.getShowPaths(), data.getMaxLogs(), handler );
  110. entries.put( handler.getPath(), handler.getEntries() );
  111. }
  112. }
  113. results.setEntries(entries);
  114. out.flush();
  115. out.close();
  116. }
  117. public class LogHandler implements ISVNLogEntryHandler {
  118. private String path = "";
  119. private List<SVNLogEntry> logEntries = null;
  120. public LogHandler( File f ) {
  121. path = f.toString();
  122. }
  123. public LogHandler( String p ) {
  124. path = p;
  125. }
  126. public void handleLogEntry( SVNLogEntry logEntry ) {
  127. if ( logEntries == null ) {
  128. logEntries = new ArrayList<SVNLogEntry>();
  129. }
  130. logEntries.add( logEntry );
  131. Log.this.printLogEntry( path, logEntry );
  132. }
  133. public String getPath() {
  134. return path;
  135. }
  136. public List<SVNLogEntry> getEntries() {
  137. if ( logEntries == null ) {
  138. logEntries = new ArrayList<SVNLogEntry>();
  139. SVNLogEntry logEntry = new SVNLogEntry(null, 0, "---", null, "No entries found.");
  140. logEntries.add(logEntry);
  141. }
  142. return logEntries;
  143. }
  144. }
  145. /**
  146. * @return a log results object
  147. */
  148. public LogResults getLogEntries() {
  149. return results;
  150. }
  151. public void printLogEntry( String path, SVNLogEntry logEntry ) {
  152. if ( out == null )
  153. return ;
  154. out.println( "path: " + path );
  155. out.println( "revision: " + logEntry.getRevision() );
  156. out.println( "author: " + logEntry.getAuthor() );
  157. out.println( "date: " + logEntry.getDate() );
  158. out.println( "log message: " + logEntry.getMessage() );
  159. // displaying all paths that were changed in that revision, changed
  160. // path information is represented by SVNLogEntryPath.
  161. if ( logEntry.getChangedPaths().size() > 0 ) {
  162. out.println();
  163. out.println( "changed paths:" );
  164. // keys are changed paths
  165. Set changedPathsSet = logEntry.getChangedPaths().keySet();
  166. for ( Iterator changedPaths = changedPathsSet.iterator(); changedPaths.hasNext(); ) {
  167. SVNLogEntryPath entryPath = ( SVNLogEntryPath ) logEntry.getChangedPaths().get( changedPaths.next() );
  168. /*
  169. SVNLogEntryPath.getPath returns the changed path itself
  170. SVNLogEntryPath.getType returns a character describing
  171. how the path was changed ('A' - added, 'D' - deleted or
  172. 'M' - modified);
  173. If the path was copied from another one (branched) then
  174. SVNLogEntryPath.getCopyPath and
  175. SVNLogEntryPath.getCopyRevision tells where it was copied
  176. from and what revision the origin path was at.
  177. */
  178. out.println( " "
  179. + entryPath.getType()
  180. + " "
  181. + entryPath.getPath()
  182. + ( ( entryPath.getCopyPath() != null ) ? " (from "
  183. + entryPath.getCopyPath() + " revision "
  184. + entryPath.getCopyRevision() + ")" : "" ) );
  185. }
  186. }
  187. }
  188. public static void main (String[] args) {
  189. // for testing
  190. LogData data = new LogData();
  191. data.setUsername("daleanson");
  192. data.setPassword("");
  193. List<String> paths = new ArrayList<String>();
  194. paths.add("/home/danson/src/plugins/SVNPlugin/src/ise/plugin/svn/command/Log.java");
  195. data.setPaths(paths);
  196. data.setOut(new ise.plugin.svn.io.ConsolePrintStream(new ise.plugin.svn.io.LogOutputStream(null)));
  197. //long start_rev = 9795L;
  198. //long end_rev = 9810L;
  199. java.util.Calendar cal = java.util.Calendar.getInstance();
  200. cal.set(2007, 5, 1);
  201. //SVNRevision start = SVNRevision.parse(String.valueOf(start_rev));
  202. SVNRevision start = SVNRevision.create(cal.getTime());
  203. cal.set(2007, 6, 1);
  204. //SVNRevision end = SVNRevision.parse(String.valueOf(end_rev));
  205. SVNRevision end = SVNRevision.create(cal.getTime());
  206. data.setStartRevision(start);
  207. data.setEndRevision(end);
  208. Log log = new Log();
  209. try {
  210. org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory.setup();
  211. org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl.setup();
  212. org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory.setup();
  213. log.doLog(data);
  214. }
  215. catch(Exception e) {
  216. e.printStackTrace();
  217. }
  218. }
  219. }