001: /*
002: * ====================================================================
003: * Copyright (c) 2004-2008 TMate Software Ltd. All rights reserved.
004: *
005: * This software is licensed as described in the file COPYING, which
006: * you should have received as part of this distribution. The terms
007: * are also available at http://svnkit.com/license.html
008: * If newer versions of this license are posted there, you may use a
009: * newer version instead, at your option.
010: * ====================================================================
011: */
012:
013: package org.tmatesoft.svn.cli.command;
014:
015: import java.io.File;
016: import java.io.InputStream;
017: import java.io.PrintStream;
018:
019: import org.tmatesoft.svn.cli.SVNArgument;
020: import org.tmatesoft.svn.cli.SVNCommand;
021: import org.tmatesoft.svn.core.SVNCommitInfo;
022: import org.tmatesoft.svn.core.SVNErrorCode;
023: import org.tmatesoft.svn.core.SVNErrorMessage;
024: import org.tmatesoft.svn.core.SVNException;
025: import org.tmatesoft.svn.core.SVNURL;
026: import org.tmatesoft.svn.core.wc.SVNCopyClient;
027: import org.tmatesoft.svn.core.wc.SVNRevision;
028:
029: /**
030: * @version 1.1.1
031: * @author TMate Software Ltd.
032: */
033: public class SVNCopyCommand extends SVNCommand {
034:
035: public void run(InputStream in, PrintStream out, PrintStream err)
036: throws SVNException {
037: run(out, err);
038: }
039:
040: public void run(PrintStream out, PrintStream err)
041: throws SVNException {
042: if (getCommandLine().hasURLs()) {
043: if (getCommandLine().hasPaths()) {
044: final String path = getCommandLine().getPathAt(0);
045: final String url = getCommandLine().getURL(0);
046: if (getCommandLine().isPathURLBefore(url, path)) {
047: if (getCommandLine().getArgumentValue(
048: SVNArgument.MESSAGE) != null) {
049: SVNErrorMessage msg = SVNErrorMessage
050: .create(
051: SVNErrorCode.CL_UNNECESSARY_LOG_MESSAGE,
052: "Local, non-commit operations do not take a log message.");
053: throw new SVNException(msg);
054: }
055: runRemoteToLocal(out, err);
056: } else {
057: runLocalToRemote(out, err);
058: }
059: } else {
060: runRemote(out, err);
061: }
062: } else {
063: if (getCommandLine().getArgumentValue(SVNArgument.MESSAGE) != null) {
064: SVNErrorMessage msg = SVNErrorMessage
065: .create(
066: SVNErrorCode.CL_UNNECESSARY_LOG_MESSAGE,
067: "Local, non-commit operations do not take a log message.");
068: throw new SVNException(msg);
069: }
070: runLocally(out, err);
071: }
072: }
073:
074: private void runLocally(final PrintStream out, PrintStream err)
075: throws SVNException {
076: if (getCommandLine().getPathCount() != 2) {
077: SVNErrorMessage msg = SVNErrorMessage.create(
078: SVNErrorCode.CL_INSUFFICIENT_ARGS,
079: "Please enter SRC and DST path");
080: throw new SVNException(msg);
081: }
082:
083: final String absoluteSrcPath = getCommandLine().getPathAt(0);
084: final String absoluteDstPath = getCommandLine().getPathAt(1);
085: if (matchTabsInPath(absoluteDstPath, err)
086: || matchTabsInPath(absoluteSrcPath, err)) {
087: return;
088: }
089:
090: getClientManager().setEventHandler(
091: new SVNCommandEventProcessor(out, err, false));
092: SVNCopyClient updater = getClientManager().getCopyClient();
093: boolean force = getCommandLine().hasArgument(SVNArgument.FORCE);
094: SVNRevision srcRevision = SVNRevision
095: .parse((String) getCommandLine().getArgumentValue(
096: SVNArgument.REVISION));
097: updater.doCopy(new File(absoluteSrcPath), srcRevision,
098: new File(absoluteDstPath), force, false);
099: }
100:
101: private void runRemote(PrintStream out, PrintStream err)
102: throws SVNException {
103: if (getCommandLine().getURLCount() != 2) {
104: SVNErrorMessage msg = SVNErrorMessage.create(
105: SVNErrorCode.CL_INSUFFICIENT_ARGS,
106: "Please enter SRC and DST URLs");
107: throw new SVNException(msg);
108: }
109: String srcURL = getCommandLine().getURL(0);
110: SVNRevision srcRevision = SVNRevision
111: .parse((String) getCommandLine().getArgumentValue(
112: SVNArgument.REVISION));
113: String dstURL = getCommandLine().getURL(1);
114:
115: if (matchTabsInURL(srcURL, err) || matchTabsInURL(dstURL, err)) {
116: return;
117: }
118:
119: String commitMessage = getCommitMessage();
120: getClientManager().setEventHandler(
121: new SVNCommandEventProcessor(out, err, false));
122: SVNCopyClient updater = getClientManager().getCopyClient();
123: SVNCommitInfo result = updater.doCopy(SVNURL
124: .parseURIEncoded(srcURL), srcRevision, SVNURL
125: .parseURIEncoded(dstURL), false, commitMessage);
126: if (result != SVNCommitInfo.NULL) {
127: out.println();
128: out.println("Committed revision " + result.getNewRevision()
129: + ".");
130: }
131: }
132:
133: private void runRemoteToLocal(final PrintStream out, PrintStream err)
134: throws SVNException {
135: final String srcURL = getCommandLine().getURL(0);
136: String dstPath = getCommandLine().getPathAt(0);
137: SVNRevision revision = SVNRevision
138: .parse((String) getCommandLine().getArgumentValue(
139: SVNArgument.REVISION));
140: if (revision == null || !revision.isValid()) {
141: revision = SVNRevision.HEAD;
142: }
143: getClientManager().setEventHandler(
144: new SVNCommandEventProcessor(out, err, false));
145: SVNCopyClient updater = getClientManager().getCopyClient();
146: updater.doCopy(SVNURL.parseURIEncoded(srcURL), revision,
147: new File(dstPath));
148: }
149:
150: private void runLocalToRemote(final PrintStream out, PrintStream err)
151: throws SVNException {
152: final String dstURL = getCommandLine().getURL(0);
153: String srcPath = getCommandLine().getPathAt(0);
154: if (matchTabsInPath(srcPath, err)
155: || matchTabsInURL(dstURL, err)) {
156: return;
157: }
158: String message = getCommitMessage();
159: SVNRevision revision = SVNRevision
160: .parse((String) getCommandLine().getArgumentValue(
161: SVNArgument.REVISION));
162: if (revision == null || !revision.isValid()) {
163: revision = SVNRevision.WORKING;
164: }
165: getClientManager().setEventHandler(
166: new SVNCommandEventProcessor(out, err, false));
167: SVNCopyClient updater = getClientManager().getCopyClient();
168:
169: SVNRevision srcRevision = SVNRevision
170: .parse((String) getCommandLine().getArgumentValue(
171: SVNArgument.REVISION));
172: updater.setEventHandler(null);
173: SVNCommitInfo info = updater.doCopy(new File(srcPath),
174: srcRevision, SVNURL.parseURIEncoded(dstURL), message);
175: if (info != SVNCommitInfo.NULL) {
176: out.println();
177: out.println("Committed revision " + info.getNewRevision()
178: + ".");
179: }
180: }
181: }
|