View Javadoc

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