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