View Javadoc
1   package org.apache.maven.scm.provider.local.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 org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFile;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmFileStatus;
26  import org.apache.maven.scm.ScmVersion;
27  import org.apache.maven.scm.command.list.AbstractListCommand;
28  import org.apache.maven.scm.command.list.ListScmResult;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  import java.io.File;
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
40   *
41   */
42  public class LocalListCommand
43      extends AbstractListCommand
44  {
45      /** {@inheritDoc} */
46      protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
47                                                  ScmVersion version )
48          throws ScmException
49      {
50          if ( version != null )
51          {
52              throw new ScmException( "The local scm doesn't support tags." );
53          }
54  
55          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
56  
57          File root = new File( repository.getRoot() );
58  
59          String module = repository.getModule();
60  
61          File source = new File( root, module );
62  
63          if ( !root.exists() )
64          {
65              throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
66          }
67  
68          if ( !source.exists() )
69          {
70              throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
71          }
72  
73          if ( getLogger().isInfoEnabled() )
74          {
75              getLogger().info( "Listing files of '" + source.getAbsolutePath() + "'." );
76          }
77  
78          try
79          {
80              if ( fileSet.getFileList() == null || fileSet.getFileList().isEmpty() )
81              {
82                  return new LocalListScmResult( null, getFiles( source, source, recursive ) );
83              }
84              else
85              {
86                  List<ScmFile> files = new ArrayList<ScmFile>();
87                  Iterator<File> it = fileSet.getFileList().iterator();
88  
89                  while ( it.hasNext() )
90                  {
91                      File file = (File) it.next();
92  
93                      files.addAll( getFiles( source, new File( source, file.getPath() ), recursive ) );
94                  }
95  
96                  return new LocalListScmResult( null, files );
97              }
98          }
99          catch ( Exception e )
100         {
101             return new ListScmResult( null, "The svn command failed.", e.getMessage(), false );
102         }
103     }
104 
105     private List<ScmFile> getFiles( File source, File directory, boolean recursive )
106         throws Exception
107     {
108         if ( !directory.exists() )
109         {
110             throw new Exception( "Directory '" + directory.getAbsolutePath() + "' doesn't exist." );
111         }
112 
113         List<ScmFile> files = new ArrayList<ScmFile>();
114 
115         File[] filesArray = directory.listFiles();
116 
117         if ( filesArray != null )
118         {
119             for ( int i = 0; i < filesArray.length; i++ )
120             {
121                 File f = filesArray[i];
122 
123                 String path = f.getAbsolutePath().substring( source.getAbsolutePath().length() );
124                 path = StringUtils.replace( path, "\\", "/" );
125                 path = StringUtils.replace( path, "/./", "/" );
126 
127                 files.add( new ScmFile( path, ScmFileStatus.CHECKED_IN ) );
128 
129                 if ( f.isDirectory() && recursive )
130                 {
131                     files.addAll( getFiles( source, f, recursive ) );
132                 }
133             }
134         }
135 
136         return files;
137     }
138 }