001package org.apache.maven.scm.provider.svn.svnexe.command.remoteinfo;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.maven.scm.CommandParameters;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.command.remoteinfo.AbstractRemoteInfoCommand;
027import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
028import org.apache.maven.scm.log.ScmLogger;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.svn.command.SvnCommand;
031import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
032import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
033import org.apache.maven.scm.util.AbstractConsumer;
034import org.codehaus.plexus.util.cli.CommandLineException;
035import org.codehaus.plexus.util.cli.CommandLineUtils;
036import org.codehaus.plexus.util.cli.Commandline;
037
038import java.util.HashMap;
039import java.util.Map;
040
041/**
042 * @author Olivier Lamy
043 * @since 1.6
044 */
045public class SvnRemoteInfoCommand
046    extends AbstractRemoteInfoCommand
047    implements SvnCommand
048{
049    @Override
050    public RemoteInfoScmResult executeRemoteInfoCommand( ScmProviderRepository repository, ScmFileSet fileSet,
051                                                         CommandParameters parameters )
052        throws ScmException
053    {
054
055        String url = ( (SvnScmProviderRepository) repository ).getUrl();
056        // use a default svn layout, url is here http://svn.apache.org/repos/asf/maven/maven-3/trunk
057        // so as we presume we have good users using standard svn layout, we calculate tags and branches url
058        String baseUrl = StringUtils.endsWith( url, "/" )
059            ? StringUtils.substringAfter( StringUtils.removeEnd( url, "/" ), "/" )
060            : StringUtils.substringBeforeLast( url, "/" );
061
062        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet == null ? null : fileSet.getBasedir(),
063                                                                    (SvnScmProviderRepository) repository );
064
065        cl.createArg().setValue( "ls" );
066
067        cl.createArg().setValue( baseUrl + "/tags" );
068
069        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
070
071        LsConsumer consumer = new LsConsumer( getLogger(), baseUrl );
072
073        int exitCode = 0;
074
075        Map<String, String> tagsInfos = null;
076
077        try
078        {
079            exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
080            tagsInfos = consumer.infos;
081
082        }
083        catch ( CommandLineException ex )
084        {
085            throw new ScmException( "Error while executing svn command.", ex );
086        }
087
088        if ( exitCode != 0 )
089        {
090            return new RemoteInfoScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
091        }
092
093        cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet == null ? null : fileSet.getBasedir(),
094                                                        (SvnScmProviderRepository) repository );
095
096        cl.createArg().setValue( "ls" );
097
098        cl.createArg().setValue( baseUrl + "/tags" );
099
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}