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