001package org.apache.maven.scm.provider.svn.svnexe.command.list;
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.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmRevision;
025import org.apache.maven.scm.ScmVersion;
026import org.apache.maven.scm.command.list.AbstractListCommand;
027import org.apache.maven.scm.command.list.ListScmResult;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.svn.command.SvnCommand;
030import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
031import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
032import org.codehaus.plexus.util.StringUtils;
033import org.codehaus.plexus.util.cli.CommandLineException;
034import org.codehaus.plexus.util.cli.CommandLineUtils;
035import org.codehaus.plexus.util.cli.Commandline;
036
037import java.io.File;
038import java.util.Iterator;
039
040/**
041 * Command to list files in SVN ( <code>svn list</code> command )
042 *
043 * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
044 *
045 */
046public class SvnListCommand
047    extends AbstractListCommand
048    implements SvnCommand
049{
050    private static final File TMP_DIR = new File( System.getProperty( "java.io.tmpdir" ) );
051
052    /** {@inheritDoc} */
053    protected ListScmResult executeListCommand( ScmProviderRepository repository, ScmFileSet fileSet, boolean recursive,
054                                                ScmVersion version )
055        throws ScmException
056    {
057        Commandline cl = createCommandLine( (SvnScmProviderRepository) repository, fileSet, recursive, version );
058
059        SvnListConsumer consumer = new SvnListConsumer();
060
061        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
062
063        if ( getLogger().isInfoEnabled() )
064        {
065            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
066            getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
067        }
068
069        int exitCode;
070
071        try
072        {
073            exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
074        }
075        catch ( CommandLineException ex )
076        {
077            throw new ScmException( "Error while executing command.", ex );
078        }
079
080        if ( exitCode != 0 )
081        {
082            return new ListScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
083        }
084
085        return new ListScmResult( cl.toString(), consumer.getFiles() );
086    }
087
088    static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet, boolean recursive,
089                                          ScmVersion version )
090    {
091        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( TMP_DIR, repository );
092
093        cl.createArg().setValue( "list" );
094
095        if ( recursive )
096        {
097            cl.createArg().setValue( "--recursive" );
098        }
099
100        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
101        {
102            if ( version instanceof ScmRevision )
103            {
104                cl.createArg().setValue( "-r" );
105
106                cl.createArg().setValue( version.getName() );
107            }
108        }
109
110        Iterator<File> it = fileSet.getFileList().iterator();
111
112        while ( it.hasNext() )
113        {
114            File file = it.next();
115
116            cl.createArg().setValue( repository.getUrl() + "/" + file.getPath().replace( '\\', '/' ) );
117        }
118
119        return cl;
120    }
121
122}