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.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.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.svn.command.SvnCommand;
29  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
30  import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
31  import org.apache.maven.scm.util.AbstractConsumer;
32  import org.codehaus.plexus.util.StringUtils;
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          url = StringUtils.stripEnd( url, "/" );
58          int idx = url.lastIndexOf( "/" );
59          String baseUrl = url.substring( 0, idx );
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( baseUrl + "/tags" );
71  
72          int exitCode = 0;
73  
74          Map<String, String> tagsInfos = null;
75  
76          try
77          {
78              exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr );
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 + "/branches" + "@" );
98  
99          stderr = new CommandLineUtils.StringStreamConsumer();
100 
101         consumer = new LsConsumer( baseUrl + "/branches" );
102 
103         Map<String, String> branchesInfos = null;
104 
105         try
106         {
107             exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr );
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( url );
138 
139         int exitCode = 0;
140 
141         try
142         {
143             exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr );
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.contains( "W160013" ) || output.contains( "svn: URL" ) )
156             {
157                 return false;
158             }
159             throw new ScmException( cl + ".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<>();
169 
170         String url;
171 
172         LsConsumer( String url )
173         {
174             this.url = url;
175         }
176 
177         public void consumeLine( String s )
178         {
179             infos.put( StringUtils.stripEnd( s, "/" ), url + "/" + s );
180         }
181 
182         Map<String, String> getInfos()
183         {
184             return infos;
185         }
186     }
187 }