/plugins/SVNPlugin/tags/1.1.0/src/ise/plugin/svn/command/Copy.java

# · Java · 144 lines · 77 code · 15 blank · 52 comment · 32 complexity · b8d636008be1640ed48cb191ddaebb26 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 org.tmatesoft.svn.cli.command.SVNCommandEventProcessor;
  29. import org.tmatesoft.svn.core.SVNCommitInfo;
  30. import org.tmatesoft.svn.core.SVNException;
  31. import org.tmatesoft.svn.core.SVNURL;
  32. import org.tmatesoft.svn.core.wc.ISVNOptions;
  33. import org.tmatesoft.svn.core.wc.SVNClientManager;
  34. import org.tmatesoft.svn.core.wc.SVNCopyClient;
  35. import org.tmatesoft.svn.core.wc.SVNRevision;
  36. import org.tmatesoft.svn.core.wc.SVNWCClient;
  37. import org.tmatesoft.svn.core.wc.SVNWCUtil;
  38. import ise.plugin.svn.data.CopyData;
  39. /**
  40. * Used to copy or move either a working file to another working file or to the
  41. * repository, or to move a repository file or directory to a working file or to
  42. * another repository location. To recap, this class can copy:
  43. * working copy -> working copy
  44. * working copy -> repository
  45. * repository -> working copy
  46. * repository -> repository
  47. */
  48. public class Copy {
  49. public SVNCommitInfo copy( CopyData data ) throws CommandInitializationException, SVNException {
  50. SVNKit.setupLibrary();
  51. // validate data values
  52. if ( data.getOut() == null ) {
  53. throw new CommandInitializationException( "Invalid output stream." );
  54. }
  55. if ( data.getErr() == null ) {
  56. data.setErr( data.getOut() );
  57. }
  58. // use default svn config options
  59. ISVNOptions options = SVNWCUtil.createDefaultOptions( true );
  60. // use the svnkit client manager
  61. SVNClientManager clientManager = SVNClientManager.newInstance( options, data.getUsername(), data.getDecryptedPassword() );
  62. // get a copy client
  63. SVNCopyClient client = clientManager.getCopyClient();
  64. // set an event handler so that messages go to the commit data streams for display
  65. client.setEventHandler( new SVNCommandEventProcessor( data.getOut(), data.getErr(), false ) );
  66. // actually do the copy
  67. PrintStream out = data.getOut();
  68. File sourceFile = data.getSourceFile();
  69. File destinationFile = data.getDestinationFile();
  70. SVNURL sourceURL = data.getSourceURL();
  71. SVNURL destinationURL = data.getDestinationURL();
  72. SVNCommitInfo results = SVNCommitInfo.NULL;
  73. if (sourceFile != null && destinationFile != null) {
  74. // copy working copy to working copy, this is a local copy or move
  75. SVNRevision revision = data.getRevision();
  76. if (revision == null) {
  77. revision = SVNRevision.WORKING;
  78. }
  79. // message on local copy
  80. out.println("source file: " + sourceFile);
  81. out.println("revision: " + revision);
  82. client.doCopy(sourceFile, revision, destinationFile, data.getForce(), data.getIsMove());
  83. }
  84. else if (sourceFile != null && destinationURL != null) {
  85. // copy working copy to repository with immediate commit, this can
  86. // be used to make a branch or tag
  87. SVNRevision revision = data.getRevision();
  88. if (revision == null) {
  89. revision = SVNRevision.WORKING;
  90. }
  91. results = client.doCopy(sourceFile, revision, destinationURL, !data.getForce(), data.getMessage());
  92. }
  93. else if (sourceURL != null && destinationURL != null) {
  94. // copy a repository file or directory to another repository file or
  95. // directory with immediate commit, this could be used to make a
  96. // branch or tag
  97. SVNRevision revision = data.getRevision();
  98. if (revision == null) {
  99. revision = SVNRevision.HEAD;
  100. }
  101. results = client.doCopy(sourceURL, revision, destinationURL, data.getIsMove(), !data.getForce(), data.getMessage());
  102. }
  103. else if (sourceURL != null && destinationFile != null) {
  104. // copy a file or directory from the repository to a local working
  105. // copy, this can be used for an undelete.
  106. SVNRevision revision = data.getRevision();
  107. if (revision == null) {
  108. revision = SVNRevision.WORKING;
  109. }
  110. // no message on copy to local
  111. client.doCopy(sourceURL, revision, destinationFile);
  112. }
  113. else {
  114. StringBuilder sb = new StringBuilder();
  115. sb.append("Invalid file and/or URL parameters:\n");
  116. sb.append("sourceFile = ").append(sourceFile).append("\n");
  117. sb.append("sourceURL = ").append(sourceURL).append("\n");
  118. sb.append("destinationFile = ").append(destinationFile).append("\n");
  119. sb.append("destinationURL = ").append(destinationURL).append("\n");
  120. throw new CommandInitializationException(sb.toString());
  121. }
  122. out.flush();
  123. out.close();
  124. return results;
  125. }
  126. }