View Javadoc
1   package org.apache.maven.scm.provider.cvslib.command.list;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Iterator;
24  
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.list.AbstractListCommand;
29  import org.apache.maven.scm.command.list.ListScmResult;
30  import org.apache.maven.scm.provider.ScmProviderRepository;
31  import org.apache.maven.scm.provider.cvslib.command.CvsCommand;
32  import org.apache.maven.scm.provider.cvslib.command.CvsCommandUtils;
33  import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
34  import org.codehaus.plexus.util.Os;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.codehaus.plexus.util.cli.Commandline;
37  
38  /**
39   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
40   *
41   */
42  public abstract class AbstractCvsListCommand
43      extends AbstractListCommand
44      implements CvsCommand
45  {
46      /** {@inheritDoc} */
47      protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
48                                                  ScmVersion version )
49          throws ScmException
50      {
51          for ( Boolean supD = rlsSUPPORTSD;; supD = Boolean.FALSE )
52          {
53              ListScmResult res = executeListCommand1( repo, fileSet, recursive, version, supD );
54              if ( res.isSuccess() || supD != null )
55              {
56                  if ( rlsSUPPORTSD == null && res.isSuccess() )
57                  {
58                      rlsSUPPORTSD = supD == null ? Boolean.TRUE : supD;
59                  }
60                  return res;
61              }
62              // first attempt failed, support unknown
63              // rls: invalid option -- d
64          }
65      }
66  
67      // cvsnt does not support rls -d; Msys/Cygwin do.
68      private static Boolean rlsSUPPORTSD = Os.isFamily( Os.FAMILY_WINDOWS ) ? null : Boolean.TRUE;
69  
70      private ListScmResult executeListCommand1( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
71              ScmVersion version, Boolean supD )
72      throws ScmException
73      {
74          CvsScmProviderRepository repository = (CvsScmProviderRepository) repo;
75  
76          Commandline cl = CvsCommandUtils.getBaseCommand( "rls", repository, fileSet, "-n" );
77  
78          if ( version != null && !StringUtils.isEmpty( version.getName() ) )
79          {
80              cl.createArg().setValue( "-r" );
81              cl.createArg().setValue( version.getName() );
82          }
83  
84          if ( supD != Boolean.FALSE )
85          {
86              cl.createArg().setValue( "-d" );
87          }
88          cl.createArg().setValue( "-e" ); // szakusov: to fix "Unknown file status" problem
89  
90          if ( recursive )
91          {
92              cl.createArg().setValue( "-R" );
93          }
94  
95          String modulePref = repository.getModule();
96          if ( !StringUtils.isEmpty( modulePref ) )
97          {
98              modulePref += File.separatorChar;
99          }
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 }