View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.local.command.list;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmFileStatus;
31  import org.apache.maven.scm.ScmVersion;
32  import org.apache.maven.scm.command.list.AbstractListCommand;
33  import org.apache.maven.scm.command.list.ListScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
36  
37  /**
38   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
39   */
40  public class LocalListCommand extends AbstractListCommand {
41      /**
42       * {@inheritDoc}
43       */
44      protected ListScmResult executeListCommand(
45              ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive, ScmVersion version) throws ScmException {
46          if (version != null) {
47              throw new ScmException("The local scm doesn't support tags.");
48          }
49  
50          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
51  
52          File root = new File(repository.getRoot());
53  
54          String module = repository.getModule();
55  
56          File source = new File(root, module);
57  
58          if (!root.exists()) {
59              throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
60          }
61  
62          if (!source.exists()) {
63              throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
64          }
65  
66          if (logger.isInfoEnabled()) {
67              logger.info("Listing files of '" + source.getAbsolutePath() + "'.");
68          }
69  
70          try {
71              if (fileSet.getFileList() == null || fileSet.getFileList().isEmpty()) {
72                  return new LocalListScmResult(null, getFiles(source, source, recursive));
73              } else {
74                  List<ScmFile> files = new ArrayList<>();
75  
76                  for (File file : fileSet.getFileList()) {
77                      files.addAll(getFiles(source, new File(source, file.getPath()), recursive));
78                  }
79  
80                  return new LocalListScmResult(null, files);
81              }
82          } catch (IOException e) {
83              return new ListScmResult(null, "The svn command failed.", e.getMessage(), false);
84          }
85      }
86  
87      private List<ScmFile> getFiles(File source, File directory, boolean recursive) throws IOException {
88          if (!directory.exists()) {
89              throw new IOException("Directory '" + directory.getAbsolutePath() + "' doesn't exist.");
90          }
91  
92          List<ScmFile> files = new ArrayList<>();
93  
94          File[] filesArray = directory.listFiles();
95  
96          if (filesArray != null) {
97              for (int i = 0; i < filesArray.length; i++) {
98                  File f = filesArray[i];
99  
100                 String path =
101                         f.getAbsolutePath().substring(source.getAbsolutePath().length());
102                 path = StringUtils.replace(path, "\\", "/");
103                 path = StringUtils.replace(path, "/./", "/");
104 
105                 files.add(new ScmFile(path, ScmFileStatus.CHECKED_IN));
106 
107                 if (f.isDirectory() && recursive) {
108                     files.addAll(getFiles(source, f, recursive));
109                 }
110             }
111         }
112 
113         return files;
114     }
115 }