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