001    package org.apache.maven.scm.provider.svn.svnexe.command.info;
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    
022    import java.io.File;
023    import java.util.Iterator;
024    
025    import org.apache.maven.scm.CommandParameters;
026    import org.apache.maven.scm.ScmException;
027    import org.apache.maven.scm.ScmFileSet;
028    import org.apache.maven.scm.ScmResult;
029    import org.apache.maven.scm.command.AbstractCommand;
030    import org.apache.maven.scm.command.info.InfoScmResult;
031    import org.apache.maven.scm.provider.ScmProviderRepository;
032    import org.apache.maven.scm.provider.svn.command.SvnCommand;
033    import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
034    import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
035    import org.codehaus.plexus.util.StringUtils;
036    import org.codehaus.plexus.util.cli.CommandLineException;
037    import org.codehaus.plexus.util.cli.CommandLineUtils;
038    import org.codehaus.plexus.util.cli.Commandline;
039    
040    /**
041     * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
042     * @version $Id: SvnInfoCommand.java 1063885 2011-01-26 21:58:10Z olamy $
043     */
044    public class SvnInfoCommand
045        extends AbstractCommand
046        implements SvnCommand
047    {
048    
049        /** {@inheritDoc} */
050        protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet fileSet,
051                                            CommandParameters parameters )
052            throws ScmException
053        {
054            return executeInfoCommand( (SvnScmProviderRepository) repository, fileSet, parameters, false, null );
055        }
056    
057        public InfoScmResult executeInfoCommand( SvnScmProviderRepository repository, ScmFileSet fileSet,
058                                                    CommandParameters parameters, boolean recursive, String revision )
059            throws ScmException
060        {
061            Commandline cl = createCommandLine( repository, fileSet, recursive, revision );
062    
063            SvnInfoConsumer consumer = new SvnInfoConsumer();
064    
065            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
066    
067            if ( getLogger().isInfoEnabled() )
068            {
069                getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
070                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
071            }
072    
073            int exitCode;
074    
075            try
076            {
077                exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
078            }
079            catch ( CommandLineException ex )
080            {
081                throw new ScmException( "Error while executing command.", ex );
082            }
083    
084            if ( exitCode != 0 )
085            {
086                return new InfoScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
087            }
088    
089            return new InfoScmResult( cl.toString(), consumer.getInfoItems() );
090        }
091    
092        //set scope to protected to allow test to call it directly
093        protected static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet,
094                                                      boolean recursive, String revision )
095        {
096            Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repository );
097    
098            cl.createArg().setValue( "info" );
099    
100            if ( recursive )
101            {
102                cl.createArg().setValue( "--recursive" );
103            }
104    
105            if ( StringUtils.isNotEmpty( revision ) )
106            {
107                cl.createArg().setValue( "-r" );
108    
109                cl.createArg().setValue( revision );
110            }
111    
112            Iterator<File> it = fileSet.getFileList().iterator();
113    
114            while ( it.hasNext() )
115            {
116                File file = (File) it.next();
117    
118                if ( repository == null )
119                {
120                    cl.createArg().setValue( file.getPath() );
121                }
122                else
123                {
124                    cl.createArg().setValue( repository.getUrl() + "/" + file.getPath().replace( '\\', '/' ) );
125                }
126            }
127    
128            return cl;
129        }
130    
131    }