View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.svn.svnexe.command.remoteinfo;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.maven.scm.CommandParameters;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand;
29  import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.svn.command.SvnCommand;
32  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
33  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
34  import org.apache.maven.scm.util.AbstractConsumer;
35  import org.codehaus.plexus.util.cli.CommandLineException;
36  import org.codehaus.plexus.util.cli.CommandLineUtils;
37  import org.codehaus.plexus.util.cli.Commandline;
38  
39  /**
40   * @author Olivier Lamy
41   * @since 1.6
42   */
43  public class SvnRemoteInfoCommand extends AbstractRemoteInfoCommand implements SvnCommand {
44  
45      private final boolean interactive;
46  
47      public SvnRemoteInfoCommand(boolean interactive) {
48          this.interactive = interactive;
49      }
50  
51      @Override
52      public RemoteInfoScmResult executeRemoteInfoCommand(
53              ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
54  
55          String url = ((SvnScmProviderRepository) repository).getUrl();
56          // use a default svn layout, url is here http://svn.apache.org/repos/asf/maven/maven-3/trunk
57          // so as we presume we have good users using standard svn layout, we calculate tags and branches url
58          url = StringUtils.stripEnd(url, "/");
59          int idx = url.lastIndexOf("/");
60          String baseUrl = url.substring(0, idx);
61  
62          Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(
63                  fileSet == null ? null : fileSet.getBasedir(), (SvnScmProviderRepository) repository, interactive);
64  
65          cl.createArg().setValue("ls");
66  
67          cl.createArg().setValue(baseUrl + "/tags" + "@");
68  
69          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
70  
71          LsConsumer consumer = new LsConsumer(baseUrl + "/tags");
72  
73          int exitCode = 0;
74  
75          Map<String, String> tagsInfos = null;
76  
77          try {
78              exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
79              tagsInfos = consumer.infos;
80  
81          } catch (CommandLineException ex) {
82              throw new ScmException("Error while executing svn command.", ex);
83          }
84  
85          if (exitCode != 0) {
86              return new RemoteInfoScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
87          }
88  
89          cl = SvnCommandLineUtils.getBaseSvnCommandLine(
90                  fileSet == null ? null : fileSet.getBasedir(), (SvnScmProviderRepository) repository, interactive);
91  
92          cl.createArg().setValue("ls");
93  
94          cl.createArg().setValue(baseUrl + "/branches" + "@");
95  
96          stderr = new CommandLineUtils.StringStreamConsumer();
97  
98          consumer = new LsConsumer(baseUrl + "/branches");
99  
100         Map<String, String> branchesInfos = null;
101 
102         try {
103             exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
104             branchesInfos = consumer.infos;
105 
106         } catch (CommandLineException ex) {
107             throw new ScmException("Error while executing svn command.", ex);
108         }
109 
110         if (exitCode != 0) {
111             return new RemoteInfoScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
112         }
113 
114         return new RemoteInfoScmResult(cl.toString(), branchesInfos, tagsInfos);
115     }
116 
117     public boolean remoteUrlExist(ScmProviderRepository repository, CommandParameters parameters) throws ScmException {
118         String url = ((SvnScmProviderRepository) repository).getUrl();
119 
120         Commandline cl =
121                 SvnCommandLineUtils.getBaseSvnCommandLine(null, (SvnScmProviderRepository) repository, interactive);
122 
123         cl.createArg().setValue("ls");
124 
125         cl.createArg().setValue(url + "@");
126 
127         CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
128 
129         LsConsumer consumer = new LsConsumer(url);
130 
131         int exitCode = 0;
132 
133         try {
134             exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
135         } catch (CommandLineException ex) {
136             throw new ScmException("Error while executing svn command.", ex);
137         }
138 
139         if (exitCode != 0) {
140             String output = stderr.getOutput();
141             // olamy: a bit ugly but....
142             // trying to parse error from svn cli which indicate no remote path
143             if (output.contains("W160013") || output.contains("svn: URL")) {
144                 return false;
145             }
146             throw new ScmException(cl + ".The svn command failed:" + stderr.getOutput());
147         }
148 
149         return true;
150     }
151 
152     private static class LsConsumer extends AbstractConsumer {
153         Map<String, String> infos = new HashMap<>();
154 
155         String url;
156 
157         LsConsumer(String url) {
158             this.url = url;
159         }
160 
161         public void consumeLine(String s) {
162             infos.put(StringUtils.stripEnd(s, "/"), url + "/" + s);
163         }
164 
165         Map<String, String> getInfos() {
166             return infos;
167         }
168     }
169 }