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.IOException;
017: import java.io.InputStream;
018: import java.io.PrintStream;
019: import java.util.Date;
020:
021: import org.tmatesoft.svn.cli.SVNArgument;
022: import org.tmatesoft.svn.cli.SVNCommand;
023: import org.tmatesoft.svn.core.SVNException;
024: import org.tmatesoft.svn.core.SVNURL;
025: import org.tmatesoft.svn.core.internal.util.SVNFormatUtil;
026: import org.tmatesoft.svn.core.wc.ISVNAnnotateHandler;
027: import org.tmatesoft.svn.core.wc.SVNDiffOptions;
028: import org.tmatesoft.svn.core.wc.SVNLogClient;
029: import org.tmatesoft.svn.core.wc.SVNRevision;
030: import org.tmatesoft.svn.core.wc.xml.AbstractXMLHandler;
031: import org.tmatesoft.svn.core.wc.xml.SVNXMLAnnotateHandler;
032: import org.tmatesoft.svn.core.wc.xml.SVNXMLSerializer;
033:
034: /**
035: * @version 1.1.1
036: * @author TMate Software Ltd.
037: */
038: public class SVNAnnotateCommand extends SVNCommand implements
039: ISVNAnnotateHandler {
040:
041: private boolean myIsVerbose;
042: private PrintStream myPrintStream;
043:
044: public void run(InputStream in, PrintStream out, PrintStream err)
045: throws SVNException {
046: run(out, err);
047: }
048:
049: public void run(PrintStream out, PrintStream err)
050: throws SVNException {
051: SVNLogClient logClient = getClientManager().getLogClient();
052: if (getCommandLine().hasArgument(SVNArgument.EXTENSIONS)) {
053: SVNDiffOptions diffOptions = new SVNDiffOptions(
054: getCommandLine().hasArgument(
055: SVNArgument.IGNORE_ALL_WS),
056: getCommandLine().hasArgument(
057: SVNArgument.IGNORE_WS_CHANGE),
058: getCommandLine().hasArgument(
059: SVNArgument.IGNORE_EOL_STYLE));
060: logClient.setDiffOptions(diffOptions);
061: }
062: myIsVerbose = getCommandLine().hasArgument(SVNArgument.VERBOSE);
063: myPrintStream = out;
064: SVNRevision[] revRange = getStartEndRevisions();
065: SVNRevision startRevision = revRange[0];
066: SVNRevision endRevision = revRange[1];
067: boolean force = getCommandLine().hasArgument(SVNArgument.FORCE);
068: ISVNAnnotateHandler handler = this ;
069: SVNXMLSerializer serializer = null;
070: if (getCommandLine().hasArgument(SVNArgument.XML)) {
071: serializer = new SVNXMLSerializer(System.out);
072: handler = new SVNXMLAnnotateHandler(serializer);
073: if (!getCommandLine().hasArgument(SVNArgument.INCREMENTAL)) {
074: ((AbstractXMLHandler) handler).startDocument();
075: }
076: }
077:
078: for (int i = 0; i < getCommandLine().getURLCount(); i++) {
079: String url = getCommandLine().getURL(i);
080: SVNRevision pegRevision = getCommandLine()
081: .getPegRevision(i);
082: if (serializer != null) {
083: ((SVNXMLAnnotateHandler) handler).startTarget(url);
084: }
085: SVNRevision endRev = endRevision;
086: if ((endRevision == null || !endRevision.isValid())
087: && (pegRevision == null || !pegRevision.isValid())) {
088: endRev = SVNRevision.HEAD;
089: }
090: try {
091: logClient.doAnnotate(SVNURL.parseURIEncoded(url),
092: pegRevision, startRevision, endRev, force,
093: handler, null);
094: } catch (SVNException e) {
095: if (e.getMessage() != null
096: && e.getMessage().indexOf("binary") >= 0) {
097: out.println("Skipping binary file: '" + url + "'");
098: } else {
099: throw e;
100: }
101: }
102: if (serializer != null) {
103: ((SVNXMLAnnotateHandler) handler).endTarget();
104: }
105: }
106: endRevision = parseRevision(getCommandLine());
107: for (int i = 0; i < getCommandLine().getPathCount(); i++) {
108: File path = new File(getCommandLine().getPathAt(i))
109: .getAbsoluteFile();
110: SVNRevision pegRevision = getCommandLine()
111: .getPathPegRevision(i);
112: if (serializer != null) {
113: ((SVNXMLAnnotateHandler) handler)
114: .startTarget(getCommandLine().getPathAt(i));
115: }
116: SVNRevision endRev = endRevision;
117: if ((endRevision == null || !endRevision.isValid())
118: && (pegRevision == null || !pegRevision.isValid())) {
119: endRev = SVNRevision.BASE;
120: }
121: try {
122: logClient.doAnnotate(path, pegRevision, startRevision,
123: endRev, force, handler);
124: } catch (SVNException e) {
125: if (e.getMessage() != null
126: && e.getMessage().indexOf("binary") >= 0) {
127: err.println("Skipping binary file: '"
128: + SVNFormatUtil.formatPath(path) + "'");
129: } else {
130: throw e;
131: }
132: }
133: if (serializer != null) {
134: ((SVNXMLAnnotateHandler) handler).endTarget();
135: }
136: }
137: if (getCommandLine().hasArgument(SVNArgument.XML)) {
138: if (!getCommandLine().hasArgument(SVNArgument.INCREMENTAL)) {
139: ((AbstractXMLHandler) handler).endDocument();
140: }
141: try {
142: serializer.flush();
143: } catch (IOException e) {
144: }
145: }
146: }
147:
148: public void handleLine(Date date, long revision, String author,
149: String line) {
150: StringBuffer result = new StringBuffer();
151: if (myIsVerbose) {
152: if (revision >= 0) {
153: result.append(SVNFormatUtil.formatString(Long
154: .toString(revision), 6, false));
155: result.append(' ');
156: } else {
157: result.append(" -");
158: }
159: result.append(' ');
160: result.append(author != null ? SVNFormatUtil.formatString(
161: author, 10, false) : " -");
162: result.append(' ');
163: if (date != null) {
164: result.append(SVNFormatUtil.formatHumanDate(date,
165: getClientManager().getOptions()));
166: } else {
167: result
168: .append(" -");
169: }
170: result.append(' ');
171: } else {
172: result.append(SVNFormatUtil.formatString(Long
173: .toString(revision), 6, false));
174: result.append(' ');
175: result.append(author != null ? SVNFormatUtil.formatString(
176: author, 10, false) : " -");
177: result.append(' ');
178: }
179: result.append(line);
180: myPrintStream.println(result.toString());
181: }
182: }
|