001package org.apache.maven.scm.provider.cvslib.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 java.io.File;
023import java.util.Iterator;
024
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmVersion;
028import org.apache.maven.scm.command.list.AbstractListCommand;
029import org.apache.maven.scm.command.list.ListScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.cvslib.command.CvsCommand;
032import org.apache.maven.scm.provider.cvslib.command.CvsCommandUtils;
033import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
034import org.codehaus.plexus.util.Os;
035import org.codehaus.plexus.util.StringUtils;
036import org.codehaus.plexus.util.cli.Commandline;
037
038/**
039 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
040 *
041 */
042public abstract class AbstractCvsListCommand
043    extends AbstractListCommand
044    implements CvsCommand
045{
046    /** {@inheritDoc} */
047    protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
048                                                ScmVersion version )
049        throws ScmException
050    {
051        for ( Boolean supD = rlsSUPPORTSD;; supD = Boolean.FALSE )
052        {
053            ListScmResult res = executeListCommand1( repo, fileSet, recursive, version, supD );
054            if ( res.isSuccess() || supD != null )
055            {
056                if ( rlsSUPPORTSD == null && res.isSuccess() )
057                {
058                    rlsSUPPORTSD = supD == null ? Boolean.TRUE : supD;
059                }
060                return res;
061            }
062            // first attempt failed, support unknown
063            // rls: invalid option -- d
064        }
065    }
066
067    // cvsnt does not support rls -d; Msys/Cygwin do.
068    private static Boolean rlsSUPPORTSD = Os.isFamily( Os.FAMILY_WINDOWS ) ? null : Boolean.TRUE;
069
070    private ListScmResult executeListCommand1( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
071            ScmVersion version, Boolean supD )
072    throws ScmException
073    {
074        CvsScmProviderRepository repository = (CvsScmProviderRepository) repo;
075
076        Commandline cl = CvsCommandUtils.getBaseCommand( "rls", repository, fileSet, "-n" );
077
078        if ( version != null && !StringUtils.isEmpty( version.getName() ) )
079        {
080            cl.createArg().setValue( "-r" );
081            cl.createArg().setValue( version.getName() );
082        }
083
084        if ( supD != Boolean.FALSE )
085        {
086            cl.createArg().setValue( "-d" );
087        }
088        cl.createArg().setValue( "-e" ); // szakusov: to fix "Unknown file status" problem
089
090        if ( recursive )
091        {
092            cl.createArg().setValue( "-R" );
093        }
094
095        String modulePref = repository.getModule();
096        if ( !StringUtils.isEmpty( modulePref ) )
097        {
098            modulePref += File.separatorChar;
099        }
100        else
101        {
102            modulePref = "";
103        }
104
105        for ( Iterator<File> it = fileSet.getFileList().iterator(); it.hasNext(); )
106        {
107            File target = (File) it.next();
108            String path = target.getPath();
109            if ( path.startsWith( "\\" ) )
110            {
111                path = path.substring( 1 );
112            }
113            path = modulePref + path;
114            cl.createArg().setValue( path );
115        }
116
117        if ( getLogger().isInfoEnabled() )
118        {
119            getLogger().info( "Executing: " + cl );
120            getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
121        }
122
123        return executeCvsCommand( cl );
124    }
125
126    protected abstract ListScmResult executeCvsCommand( Commandline cl )
127        throws ScmException;
128}