2 * Copyright 2000-2012 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.svn.info;
18 import com.intellij.execution.process.ProcessOutput;
19 import com.intellij.execution.process.ProcessOutputTypes;
20 import com.intellij.openapi.diagnostic.Logger;
21 import com.intellij.openapi.util.Key;
22 import com.intellij.openapi.util.text.StringUtil;
23 import com.intellij.openapi.vfs.CharsetToolkit;
24 import com.intellij.util.Consumer;
25 import com.intellij.util.containers.ContainerUtil;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28 import org.jetbrains.idea.svn.api.BaseSvnClient;
29 import org.jetbrains.idea.svn.api.Depth;
30 import org.jetbrains.idea.svn.commandLine.*;
31 import org.tmatesoft.svn.core.SVNException;
32 import org.tmatesoft.svn.core.SVNURL;
33 import org.tmatesoft.svn.core.wc.SVNRevision;
34 import org.tmatesoft.svn.core.wc2.SvnTarget;
35 import org.xml.sax.SAXException;
37 import javax.xml.parsers.ParserConfigurationException;
38 import javax.xml.parsers.SAXParser;
39 import javax.xml.parsers.SAXParserFactory;
40 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.util.Collection;
44 import java.util.List;
47 * Created with IntelliJ IDEA.
48 * User: Irina.Chernushina
52 public class CmdInfoClient extends BaseSvnClient implements InfoClient {
54 private static final Logger LOG = Logger.getInstance(CmdInfoClient.class);
56 private String execute(@NotNull List<String> parameters, @NotNull File path) throws SvnBindException {
57 // workaround: separately capture command output - used in exception handling logic to overcome svn 1.8 issue (see below)
58 final ProcessOutput output = new ProcessOutput();
59 LineCommandListener listener = new LineCommandAdapter() {
61 public void onLineAvailable(String line, Key outputType) {
62 if (outputType == ProcessOutputTypes.STDOUT) {
63 output.appendStdout(line);
69 CommandExecutor command = execute(myVcs, SvnTarget.fromFile(path), SvnCommandName.info, parameters, listener);
71 return command.getOutput();
73 catch (SvnBindException e) {
74 final String text = StringUtil.notNullize(e.getMessage());
75 if (text.contains("W155010")) {
76 // if "svn info" is executed for several files at once, then this warning could be printed only for some files, but info for other
77 // files should be parsed from output
78 return output.getStdout();
80 // not a working copy exception
81 // "E155007: '' is not a working copy"
82 if (text.contains("is not a working copy") && StringUtil.isNotEmpty(output.getStdout())) {
83 // TODO: Seems not reproducible in 1.8.4
84 // workaround: as in subversion 1.8 "svn info" on a working copy root outputs such error for parent folder,
85 // if there are files with conflicts.
86 // but the requested info is still in the output except root closing tag
87 return output.getStdout() + "</info>";
94 private static Info parseResult(@Nullable File base, @Nullable String result) throws SvnBindException {
95 CollectInfoHandler handler = new CollectInfoHandler();
97 parseResult(handler, base, result);
99 return handler.getInfo();
102 private static void parseResult(@NotNull final InfoConsumer handler, @Nullable File base, @Nullable String result)
103 throws SvnBindException {
104 if (StringUtil.isEmptyOrSpaces(result)) {
108 final SvnInfoHandler infoHandler = new SvnInfoHandler(base, new Consumer<Info>() {
110 public void consume(Info info) {
112 handler.consume(info);
114 catch (SVNException e) {
115 throw new SvnExceptionWrapper(e);
120 parseResult(result, infoHandler);
123 private static void parseResult(@NotNull String result, @NotNull SvnInfoHandler handler) throws SvnBindException {
125 SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
127 parser.parse(new ByteArrayInputStream(result.trim().getBytes(CharsetToolkit.UTF8_CHARSET)), handler);
129 catch (SvnExceptionWrapper e) {
130 LOG.info("info output " + result);
131 throw new SvnBindException(e.getCause());
132 } catch (IOException e) {
133 LOG.info("info output " + result);
134 throw new SvnBindException(e);
136 catch (ParserConfigurationException e) {
137 LOG.info("info output " + result);
138 throw new SvnBindException(e);
140 catch (SAXException e) {
141 LOG.info("info output " + result);
142 throw new SvnBindException(e);
147 private static List<String> buildParameters(@NotNull String path,
148 @Nullable SVNRevision pegRevision,
149 @Nullable SVNRevision revision,
150 @Nullable Depth depth) {
151 List<String> parameters = ContainerUtil.newArrayList();
153 CommandUtil.put(parameters, depth);
154 CommandUtil.put(parameters, revision);
155 CommandUtil.put(parameters, path, pegRevision);
156 parameters.add("--xml");
162 public Info doInfo(File path, SVNRevision revision) throws SvnBindException {
163 File base = path.isDirectory() ? path : path.getParentFile();
164 base = CommandUtil.correctUpToExistingParent(base);
167 throw new SvnBindException("Can not find existing parent file");
170 return parseResult(base, execute(buildParameters(path.getAbsolutePath(), SVNRevision.UNDEFINED, revision, Depth.EMPTY), path));
174 public Info doInfo(SVNURL url, SVNRevision pegRevision, SVNRevision revision) throws SvnBindException {
175 CommandExecutor command =
176 execute(myVcs, SvnTarget.fromURL(url), SvnCommandName.info, buildParameters(url.toString(), pegRevision, revision, Depth.EMPTY),
179 return parseResult(null, command.getOutput());
183 public void doInfo(@NotNull Collection<File> paths, @Nullable InfoConsumer handler) throws SvnBindException {
184 File base = ContainerUtil.getFirstItem(paths);
187 base = CommandUtil.correctUpToExistingParent(base);
189 List<String> parameters = ContainerUtil.newArrayList();
190 for (File file : paths) {
191 CommandUtil.put(parameters, file);
193 parameters.add("--xml");
195 // Currently do not handle exceptions here like in SvnVcs.handleInfoException - just continue with parsing in case of warnings for
196 // some of the requested items
197 String result = execute(parameters, base);
198 if (handler != null) {
199 parseResult(handler, base, result);
204 private static class CollectInfoHandler implements InfoConsumer {
206 @Nullable private Info myInfo;
209 public void consume(Info info) throws SVNException {
214 public Info getInfo() {